olehs / PZEM004T

Arduino communication library for Peacefair PZEM-004T Energy monitor
MIT License
225 stars 114 forks source link

3 PZEM 004T Software Serial Issue, output Truncated #46

Closed asking23 closed 6 years ago

asking23 commented 6 years ago

Hi,

I am trying to Fetch 3 Different PZEM 004T Module Data using Software Serial My Code is as below, Please check if my way of Fetching data is correct ?

Problem : I don't understand why Output data is truncated randomely in Serial Monitor.

#include <SoftwareSerial.h> // Arduino IDE <1.6.6
#include <PZEM004T.h>

PZEM004T pzem(5,4);  // (RX,TX) connect to TX,RX of PZEM1
IPAddress ip(192,168,1,1);
PZEM004T pzem1(0,2);  // (RX,TX) connect to TX,RX of PZEM2
IPAddress ip1(192,168,1,2);
PZEM004T pzem2(14,12);  // (RX,TX) connect to TX,RX of PZEM3
IPAddress ip2(192,168,1,3);

void setup() {
  Serial.begin(115200);
  pzem.setAddress(ip);
  pzem1.setAddress(ip1);
  pzem2.setAddress(ip2);

}

void loop() {
  float v = pzem.voltage(ip);
  if (v < 0.0) v = 0.0;
  Serial.print(v);Serial.print("V; ");

  float i = pzem.current(ip);
  if(i >= 0.0){ Serial.print(i);Serial.print("A; "); }

  float p = pzem.power(ip);
  if(p >= 0.0){ Serial.print(p);Serial.print("W; "); }

  float e = pzem.energy(ip);
  if(e >= 0.0){ Serial.print(e);Serial.print("Wh; "); }

  float v2 = pzem1.voltage(ip1);
  if (v2 < 0.0) v2 = 0.0;
  Serial.print(v2);Serial.print("V2; ");

  float i2 = pzem1.current(ip1);
  if(i2 >= 0.0){ Serial.print(i2);Serial.print("A2; "); }

  float p2 = pzem1.energy(ip1);
  if(p2 >= 0.0){ Serial.print(p2);Serial.print("Wh2; "); }

  float e2 = pzem1.energy(ip1);
  if(e2 >= 0.0){ Serial.print(e2);Serial.print("Wh2; "); }

  float v3 = pzem2.voltage(ip2);
  if (v3 < 0.0) v3 = 0.0;
  Serial.print(v3);Serial.print("V3; ");

  float i3 = pzem2.current(ip2);
  if(i3 >= 0.0){ Serial.print(i3);Serial.print("A3; "); }

  float p3 = pzem2.power(ip2);
  if(p3 >= 0.0){ Serial.print(p3);Serial.print("W3; "); }

  float e3 = pzem2.energy(ip2);
  if(e3 >= 0.0){ Serial.print(e3);Serial.print("Wh3; "); }

  Serial.println();

 // delay(3000);
}

image

asking23 commented 6 years ago

image

Okay i have remove if condition and now i can see i am getting -1 sometimes and everytime for W & WH ? what could be reason ?

olehs commented 6 years ago

I guess the problem is that one PZEM can only send 2 readings at once and then becomes busy for 1 second.

Try cрanging the order of your requests to different PZEM, something like this:

V1;A1; V2;A2; V3;A3; P1;E1; P2;E2; P3;E3;

asking23 commented 6 years ago

Finally problem solved by adding delay of 20ms between each serial Transactions..