#include <PZEM004Tv30.h>

// Pzem004 variables
float v = 0;
float i = 0; 
float p =0;
float e = 0;
float freq;
float pf;
HardwareSerial hwSerial(UART0);     // Use hwSerial1 UART0 at pins GPIO1 (TX) and GPIO3 (RX)
PZEM004Tv30 pzem(&hwSerial);           // Attach PZEM to hwSerial1
bool pzemrdy = false;

void setup () {
//Communication Setup
  Serial1.begin(57600);
//PZEM
pzem.setAddress(0x42);
}

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

