#include <PZEM004Tv30.h>
#if !defined(PZEM_RX_PIN) && !defined(PZEM_TX_PIN)
  #define PZEM_RX_PIN 16
  #define PZEM_TX_PIN 17
#endif

#if !defined(PZEM_SERIAL)
  #define PZEM_SERIAL Serial2
#endif
#if defined(ESP32)
  PZEM004Tv30 pzem(PZEM_SERIAL, PZEM_RX_PIN, PZEM_TX_PIN);
#elif defined(ESP8266)
#else
  PZEM004Tv30 pzem(PZEM_SERIAL);
#endif

void setup()
{
  Serial.begin(115200);
  delay(100);
}

void read_pzem()
{
  Serial.print(“Custom Address:”);
  Serial.println(pzem.readAddress(), HEX);
  // Read the data from the sensor
  float voltage = pzem.voltage();
  float current = pzem.current();
  float power = pzem.power();
  float energy = pzem.energy();
  float frequency = pzem.frequency();
  float pf = pzem.pf();

  if(isnan(voltage)){
      Serial.println(“Error reading voltage”);
  } else if (isnan(current)) {
      Serial.println(“Error reading current”);
  } else if (isnan(power)) {
      Serial.println(“Error reading power”);
  } else if (isnan(energy)) {
      Serial.println(“Error reading energy”);
  } else if (isnan(frequency)) {
      Serial.println(“Error reading frequency”);
  } else if (isnan(pf)) {
      Serial.println(“Error reading power factor”);
  } else {
      Serial.print(“Voltage: “);      Serial.print(voltage);      Serial.println(” V”);
      Serial.print(“Current: “);      Serial.print(current);      Serial.println(” A”);
      Serial.print(“Power: “);        Serial.print(power);        Serial.println(” W”);
      Serial.print(“Energy: “);       Serial.print(energy,3);     Serial.println(” kWh”);
      Serial.print(“Frequency: “);    Serial.print(frequency, 1); Serial.println(” Hz”);
      Serial.print(“PF: “);           Serial.println(pf);
  }  
  Serial.println();
  delay(2000);
}

void loop() {
  read_pzem();
}

*หมายเหตุ ใน Arduino IDE คอมไพล์ไม่ผ่านให้แก้เครื่องหมาย  ” ” ให้ถูกต้อง