Sketch using PZEM004Tv30 library stopped working (Arduino Web Editor)

Hi @ptillisch,
I am having he same exact kind of problem as reported here:

but with the PZEM004Tv30 library. A few time ago I could work with this library with no problem at all. Now I can't even be able to compile the exact same sketches I made previously or, when I can compile them, they just don't work whereas in the past they worked perfectly well.
I am using the Arduino Web Editor and already tried everything to solve the problem but nothing works.
A few days ago I received an email from Arduino saying the platform was going to undergo some modifications.
Can there be a relation between those modifications and the trouble we are having now with libraries?

Thanks!

Hi @jarriaga

I'll be happy to take a look. Please provide the following information:

Which board do you have selected in Arduino Web Editor?

Please provide a minimal sketch I can compile to produce the error.

I don't think so. More likely it was caused by new libraries or new library releases being added to Arduino Web Editor. New libraries are constantly being added and new releases of existing libraries constantly being made, so a sketch that compiles one minute might error the next minute!

Thanks a lot for your concern about my issues.
I am using a ESP dev kit and in the Arduino Web Editor I am using "ESP32 Wrover Kit (all versions)" as I always do in all projects with this board.
For the sketch, even the ones provided as examples by the library don't work. I can give you the example "PZEMHardSerial". Here is the code:

/*
Copyright (c) 2021 Jakub Mandula

Example of using one PZEM module with Hardware Serial interface.
================================================================

If desired, a HardwareSerial handle can be passed to the constructor
which will then be used for the communication with the module.

Note that ESP32 HardwareSerial must also be provided with the RX and TX
pins.

*/

#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)
/*************************
 *  ESP32 initialization
 * ---------------------
 * 
 * The ESP32 HW Serial interface can be routed to any GPIO pin 
 * Here we initialize the PZEM on Serial2 with RX/TX pins 16 and 17
 */
PZEM004Tv30 pzem(PZEM_SERIAL, PZEM_RX_PIN, PZEM_TX_PIN);
#elif defined(ESP8266)
/*************************
 *  ESP8266 initialization
 * ---------------------
 * 
 * Not all Arduino boards come with multiple HW Serial ports.
 * Serial2 is for example available on the Arduino MEGA 2560 but not Arduino Uno!
 * The ESP32 HW Serial interface can be routed to any GPIO pin 
 * Here we initialize the PZEM on Serial2 with default pins
 */
//PZEM004Tv30 pzem(Serial1);
#else
/*************************
 *  Arduino initialization
 * ---------------------
 * 
 * Not all Arduino boards come with multiple HW Serial ports.
 * Serial2 is for example available on the Arduino MEGA 2560 but not Arduino Uno!
 * The ESP32 HW Serial interface can be routed to any GPIO pin 
 * Here we initialize the PZEM on Serial2 with default pins
 */
PZEM004Tv30 pzem(PZEM_SERIAL);
#endif

void setup() {
    // Debugging Serial port
    Serial.begin(115200);

    // Uncomment in order to reset the internal energy counter
    // pzem.resetEnergy()
}

void loop() {
    // Print the custom address of the 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();

    // Check if the data is valid
    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 {

        // Print the values to the Serial console
        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);
}

In the monitor I only receive the "Error reading ...." messages.
Another thing I note is the following:
On the PZEM board there are two leds indicating the reception and transmission of data. These leds flicker when data is being transferred. In my case they both stay all the time solid on.

Just in case you suspect that my hardware connections are not right, if a try to run the following code:

Serial2.begin(9600, SERIAL_8N1, 16, 17);
Serial2.println("Hello World");

the leds on the PZEM module do flicker and when the transfer is over they remain off as it should be.

So, there has to be a problem related to the library. The library that the compiler/linker is using cannot be the right one. The one developed by Jakub Mandula. But I have no idea why this is happening.....

New Information:
I have followed your procedure in Sketch compiles on Arduino laptop IDE but not web IDE - library issue? - #4 by ptillisch with a slight change in order for it to work.
Instead of creating a file named PZEM004Tv3Copy.h I had to rename the existing one with this new name. Otherwise the compiler tells me that the class PZEM004Tv3 (declared in PZEM004Tv3.h) doesn't name a type.
After doing this I had the compile error: "collect2: error: ld returned 1 exit status". To my experience this means the compiler/linker is using another library where there is a class with the same name "PZEM004Tv30".
The complete verbose of the compilation is so long that is impossible to include it all here. But I include some parts including PZEM004T mentions.

/usr/local/bin/arduino-cli compile --fqbn esp32:esp32:esp32wrover --libraries /home/builder/opt/libraries/latest --build-cache-path /tmp --output-dir /tmp/878335840/build --build-path /tmp/arduino-build-6D6E79CE9752EEEB93B02802EE6D5F1D -v --library /mnt/create-efs/webide/e9/f7/e9f7ba4ce375bbab3975fce7ba844c1e:jarriaga/libraries_v2/DS3231 --library /mnt/create-efs/webide/e9/f7/e9f7ba4ce375bbab3975fce7ba844c1e:jarriaga/libraries_v2/PZEM004Tv30 --library /mnt/create-efs/webide/e9/f7/e9f7ba4ce375bbab3975fce7ba844c1e:jarriaga/libraries_v2/URTouch --library /mnt/create-efs/webide/e9/f7/e9f7ba4ce375bbab3975fce7ba844c1e:jarriaga/libraries_v2/UTFT /tmp/878335840/PZEMHardSerial_copy
(...)
bash -c "[ ! -f "/tmp/878335840/PZEMHardSerial_copy"/partitions.csv ] || cp -f "/tmp/878335840/PZEMHardSerial_copy"/partitions.csv "/tmp/arduino-build-6D6E79CE9752EEEB93B02802EE6D5F1D"/partitions.csv"
(...)
bash -c "[ -f "/tmp/878335840/PZEMHardSerial_copy"/bootloader.bin ] && cp -f "/tmp/878335840/PZEMHardSerial_copy"/bootloader.bin "/tmp/arduino-build-6D6E79CE9752EEEB93B02802EE6D5F1D"/PZEMHardSerial_copy.ino.bootloader.bin || ( [ -f "/home/builder/.arduino15/packages/esp32/hardware/esp32/2.0.5/variants/esp32"/bootloader.bin ] && cp "/home/builder/.arduino15/packages/esp32/hardware/esp32/2.0.5/variants/esp32"/bootloader.bin "/tmp/arduino-build-6D6E79CE9752EEEB93B02802EE6D5F1D"/PZEMHardSerial_copy.ino.bootloader.bin || python3 "/home/builder/.arduino15/packages/esp32/tools/esptool_py/4.2.1"/esptool.py --chip esp32 elf2image --flash_mode dio --flash_freq 80m --flash_size 4MB -o "/tmp/arduino-build-6D6E79CE9752EEEB93B02802EE6D5F1D"/PZEMHardSerial_copy.ino.bootloader.bin "/home/builder/.arduino15/packages/esp32/hardware/esp32/2.0.5"/tools/sdk/esp32/bin/bootloader_qio_80m.elf )"
(...)
Alternatives for PZEM004Tv30Copy.h: [PZEM004Tv30@1.1.2]
ResolveLibrary(PZEM004Tv30Copy.h)
-> candidates: [PZEM004Tv30@1.1.2]
(...)
Alternatives for PZEM004Tv30.h: [pzem004tv30_1_1_2@1.1.2]
ResolveLibrary(PZEM004Tv30.h)
-> candidates: [pzem004tv30_1_1_2@1.1.2]
(...)
Using library PZEM004Tv30 at version 1.1.2 in folder: /mnt/create-efs/webide/e9/f7/e9f7ba4ce375bbab3975fce7ba844c1e:jarriaga/libraries_v2/PZEM004Tv30
Using library pzem004tv30_1_1_2 at version 1.1.2 in folder: /home/builder/opt/libraries/pzem004tv30_1_1_2
(...)
/home/builder/.arduino15/packages/esp32/tools/xtensa-esp32-elf-gcc/gcc8_4_0-esp-2021r2-patch3/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld: /tmp/arduino-build-6D6E79CE9752EEEB93B02802EE6D5F1D/libraries/pzem004tv30_1_1_2/PZEM004Tv30.cpp.o: in function `PZEM004Tv30::~PZEM004Tv30()':

/home/builder/opt/libraries/latest/pzem004tv30_1_1_2/src/PZEM004Tv30.cpp:135: multiple definition of `PZEM004Tv30::~PZEM004Tv30()'; /tmp/arduino-build-6D6E79CE9752EEEB93B02802EE6D5F1D/libraries/PZEM004Tv30/PZEM004Tv30.cpp.o:/mnt/create-efs/webide/e9/f7/e9f7ba4ce375bbab3975fce7ba844c1e:jarriaga/libraries_v2/PZEM004Tv30/src/PZEM004Tv30.cpp:135: first defined here

Oh! I did the same error....

I have now followed exactly your procedure and didn't have any compile errors but the problem with the PZEM004T board remains....
It seams that a bad library is still being used.....

I just tried another thing:
Instead of downloading the library from the Arduino Web Editor, I went to Github to get it and done all the remaining steps of your procedure as of post #4.
But the problem remains exactly as before....

I have yet tried another thing.....
Before importing the library from the my computer, I changed all mentions of PZEM004v30 to PjacZ. This includes the name of the library in "library.properties", the ".cpp" and ".h" files, and even the name of the Class in the code.
With these modifications a compile with no errors and in the verbose there is not a single mention to "PZEM004Tv30". Still the code doesn't work.......
I don't know what to do anymore.....

OK, so it is a runtime error. This is more challenging for me to assist with than a compilation error as discussed in Sketch compiles on Arduino laptop IDE but not web IDE - library issue? because I don't own a PeaceFair PZEM-004T module. Maybe one of the other forum members has one and can give it a look.

Runtime errors can also be caused by a new release of a library dependency that either contains a bug or else a change in behavior that requires a modification to your sketch or even circuit. However, it doesn't appear that is the case here because I can see from the compilation output you shared that there is only one candidate library on the Arduino Cloud servers, so there is no chance of ambiguity in which library was used:

This is the "PZEM004Tv30" library used to compile your sketch:

The 1.1.2 version of the library being used was released in 2021 so, unless it was years ago that the sketch was working, the problem was also not introduced through a new release of the "PZEM004Tv30".

The remaining dependency to consider is the ESP32 boards platform, which is also periodically updated on the Arduino Cloud servers. I don't know when that happened last.

I recommend checking over your circuit. I know you did some validation with that simple "hello world" test sketch, but it is still possible an intermittent connection (which are common when using solderless breadboards) could result in a degradation of communication with the module.

Hi @ptillisch, after seeing many things in internet and doing many many different tries to solve the problem, I could finally find a solution.
Instead of selecting board "ESP32 Wrover Kit (all versions)", I tried "ESP32 Dev Module" and it worked. I never had to do this before and I believe this is caused by the library itself.
I still have some minor issues but again, I think they are also related to the library.
Thanks a lot for your time and help!!!

You are welcome. Thanks for taking the time to post an update with your solution.

Regards,
Per

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.