mandulaj / PZEM-004T-v30

Arduino library for the Updated PZEM-004T v3.0 Power and Energy meter
MIT License
256 stars 108 forks source link

Reset comunication #75

Open DavidAntonin opened 2 years ago

DavidAntonin commented 2 years ago

Hello, can you implement reset comunication over serial port? Once every few days the communication with PZEM freezes and I read nothing but zeros. After restarting ESP8266 everything is fine. I use HW serial.

mandulaj commented 2 years ago

You might actually have discovered quite a serious bug! I think the problem might be with the updateValues function which rate limits the number of actual update calls.

There is basically a piece of code that makes sure we only update once ever 200ms. And I happen to be doing it in the completely wrong way! Basically when the timer rolls over after about 50 days, the check can become invalid. It's a real shame I haven't caught it earlier. Thanks, I am going to push a fix soon

DavidAntonin commented 2 years ago

I don't think communication ends in 50 days, I would say it's less, sometimes just a few days

mandulaj commented 2 years ago

That is strange then... Could you verify if the values you get are actually 0 or Nan. Nans represent an error in the communication. But if the library returns a 0, that's the value that was reported by the PZEM chip (CRC checked and all) In such case the problem could be with the PZEM chip itself. Do you have a second PZEM module to test this with?

DavidAntonin commented 2 years ago

I have 2 PZEM 3.0 on the same UART port. If communication fail, I get Nan from both of them.

chrbcd commented 2 years ago

If there is a parasite on the reception line, during the next request, there are 26 characters in the reception buffer and since only the first 25 bytes are read, the frame is incorrect and there is always a character in the buffer. All subsequent frames will be incorrect. Simple solution: empty the buffer before making a request: In the sendCmd8 function, just before the line "_serial-> write (sendBuffer, 8); // send frame", insert this line: "while (serial-> available ()) serial-> read ();"

hdimon commented 2 years ago

Hi @mandulaj! First, thank you very much for your library and your efforts maintaining that. Second, thanks for this particular fix:). I've been living with that about 1,5 years and didn't have time to investigate it myself (and I even was not sure where is error - in library or something else) so I just manually reset Mega2560 every 1,5 months. Today I've updated one of my controllers with fixed library and if it's ok during today then likely tomorrow I'll also update the second one. I also manually added line as @christianBricaud suggested: "while (serial-> available ()) serial-> read ();".

But I want to mention that previos month I also faced case when one microcontroller stopped getting data from PZEM not after ~50 days but after 24 days. So my question is if I can do something else in additional to your and christianBricaud fixes in order to increase reliability? For me it's quite important, my other microcontrollers have been working more than year without resetting so I try to achieve reliability when microcontrollers are reset only due to power off rather than issues with sensors:).

iwas108 commented 1 year ago

Any solution to this problem? Me too have a connection lost after a few minutes.

mandulaj commented 1 year ago

Hey @iwas108, could you give me more details on your problem? What error do you get after a few minutes? It could be a result of any number of issues. Does the program simply stop? Or do you start receiving garbage data, NaNs?

jeromefischer commented 1 year ago

I am facing the same issue at the moment. Suddenly, one of the three phase measurement I have implemented with an ESP8266 returns "Nan" for some readings (e.g. power, voltage). I then publish the measured 3phase quantities an an HTML page:

Voltage V1: nan [V]
Power   P1: nan [W]
Energy  E1: nan [Wh]
Voltage V2: nan [V]
Power   P2: nan [W]
Energy  E2: 0.369000 [Wh]
Voltage V3: 236.500000 [V]
Power   P3: 1.400000 [W]
Energy  E3: 0.364000 [Wh]

Any idea, why the reading gives Nan? The reading interval is only every 10 seconds.

mandulaj commented 1 year ago

yes this is strange indeed. during the readout, can you see both RX and TX LEDs blinking on all of the boards?

iwas108 commented 1 year ago

I fixed the problem by moving the PZEM instantiation to the local function. May be there is an issue with my code, because I was using an async libs with global object/variable.

mandulaj commented 1 year ago

Ok, weird. could you perhaps share the full sketch you were using before?

iwas108 commented 1 year ago

[edited]

The problem still persist after running the sensor with real load. The sensor loosing communication with ESP32 after an hour.

thelightings commented 11 months ago

@christianBricaud Thank you alot. It is the problem.