#include <PZEM004T.h>

// Pzem004 variables
float v = 0;
float i = 0; 
float p =0;
float e = 0;
HardwareSerial hwSerial(UART0);     // Use hwSerial1 UART0 at pins GPIO1 (TX) and GPIO3 (RX)
PZEM004T pzem(&hwSerial);           // Attach PZEM to hwSerial1
IPAddress ip(192,168,1,1);
bool pzemrdy = false;

void setup () {
//Communication Setup
  Serial1.begin(57600);

// PZEM004T Setup
  while (!pzemrdy) {
      Serial1.println("Connecting to PZEM...");
      pzemrdy = pzem.setAddress(ip);
      delay(1000);
   }

}

void loop () {
//PZEM Part
  v = pzem.voltage(ip);
  if (v < 0.0) v = 0.0;
   Serial1.print(v);Serial1.print("V; ");
   i = pzem.current(ip);
   if(i >= 0.0){ Serial1.print(i);Serial1.print("A; "); }
  p = pzem.power(ip);
   if(p >= 0.0){ Serial1.print(p);Serial1.print("W; "); }
   e = pzem.energy(ip);
   if(e >= 0.0){ Serial1.print(e);Serial1.print("Wh; "); } 
   Serial1.println();
}



