PZEM-004T ESP8266 software

Following up the home energy meter post based on an ESP8266 and PZEM-004T hardware, this post describes succinctly the software for using the energy meter.

There are at least two components to the solution:

  1. ESP8266 software for driving the power meter and make the measurements.
  2. The backend software for receiving and processing data.

The ESP8266 software:
The power meter software for the ESP8266 available on this GitHub repository, uses an available PZEM-004T library for accessing the power meter, and sends the collected data through MQTT to any subscribers of the power meter topic.
I’m using the convention that is also used on Thingsboard, namely an MQTT attributes topic to publish the device status, and a telemetry topic to post the data in JSON format.
Around lines 80 on main.cpp of PowerMeter sources, the topics are defined as:

  1. Attributes: “iot/device/” + String(MQTT_ClientID) + “/attributes”
  2. Telemetry: “iot/device/” + String(MQTT_ClientID) + “/telemetry”

MQTT_ClientID is defined on the secrets.h file, where we also define a list of available WIFI connections for our ESP8266. The attributes topic periodically sends the current device status (RSSI, HEAP, wifi SSID), while the data on the telemetry topic is fed into a timeseries database such as InfluxDB where then a Grafana Dashboard shows and allows to see the captured data across time.

As also my previous post regarding framework and libraries versions, I needed to block the ESP8266 framework version and the SoftwareSerial library because the combination of these with the PZE-004T library was (is ?) broken of more recent versions. As is currently defined on the platformio.ini file, the current set of versions, work fine.

A lot of people had problems working with the use of SoftwareSerial library for the PZEM library to communicate with the hardware. The issue, that I accidentally found out, are related with timing issues to communicate with the PZEM hardware. There are periods of time that the PZEM is not responsive, probably because is making some measurement.

The solution to this issue is at start up to try the connection during some time, at 3 seconds interval until it succeeds. After the connection is successful, we need to keep an interval around one minute between reads to encounter no issues/failures . If this interval is kept, the connection to the PZEM hardware works flawlessly, at least with the hardware that I have.

So the connection phase is checked and tried several times to synchronize the ESP8266 with the PZEM, and them every single minute there is a data read. If the interval is shorter, lets say, 30s, it will fail, until the elapsed time to one minute is completed.

The firmware solves the above issue, and after reading the data, it posts it to a MQTT broker. The firmware also makes available a web page with the current status and measurements:

Power Meter Web Page

Then there are other bits, namely since the meter will be on the electric mains board, an UDP logging facility that allows on the computer to run an UDP server and see what is going on.

The back-end software:
I’ve not done much on this area, since most of it is just standard stuff. An MQTT broker and Node-Red flow. The flow just receives the data, saves it into an InfluxDB database and creates a Node-Red UI dashboard.

Power Meter Node-Red UI

This screenshot shows some of the information that was collected on the last minute and it is updated in real time as soon the PowerMeter information arrives to the MQTT broker.

Future work:
Basically what is missing is two things:

  1. Grafana Dashboard based on the InfluxDB data (Already done, to be described in a future post).
  2. Some kind of exporter to CSV or Spreadsheat to allow further data analysis such as the daily power consumption totals.

11 thoughts on “PZEM-004T ESP8266 software

  1. I’m having problems compiling the code. What env are you using to compile with? I was trying to use visualstudio code and am getting lots of .pio related errors with define.

    1. Di you pull the latest version of the code from the GitHub repository? I’ve corrected some time ago the libraries versions so that it could compile.
      Also make sure that you clean any older libraries lingering around under the .pio directories.

      1. Latest pull from Git
        Getting several warnings about signed/unsigned from sources files in .pio but one error
        .platformio/packages/framework-arduinoespressif8266@2.20500.190223/tools/sdk/libc/xtensa-lx106-elf/include/sys/pgmspace.h:76:81: error: ‘const void*’ is not a pointer-to-object type

        Also getting error about missing pgmspace.h later in the compile but supplying that file from the internet doesn’t change anything.

        Google hasn’t been much help. I have a fresh install of visualStudio but could have missed a step. It looks like it grabbing the right platform files. I’ve tried deleting and forcing updates to the .pio files always with the same final results. These are base files that should just work. I tried on both my Linux host and windows box with the same results.

      2. On platformio.ini remove the locked version of the framework:
        platform = expressif8266@2.0.3


        platform = expressif8266

        Remove the .pio directory ( rm -rf .pio) and try again to compile: pio run.

        It should compile now.

  2. THANKS!!! That did it. Still have the compiler warnings about signed/unsigned and needed to change the default upload speed for the USB/serial to 115200 before it would upload.
    I was a little surprised that the built in webpage does not come start until after MQTT is established–meaning I had to actually go configure my thingboard server to view it. Now time to go find a soldering iron to actually connect the PZEM module and see how well this will work for what I have in mind for a remote well site power monitoring.
    Thanks again for your write up and support.

    1. Glad it worked.

      To solve the issue with the web page, swap the MQTT_Connect(); of main.cpp line 250 with function webServer.Setup() on line 256. On other words make the webServer.Setup() function to appear first before the MQTT_Connect.

      It should solve your issue.

  3. works fine, but my issue is always have to upload code when ever my Wi-Fi password changers, any easy way to change wifi password, maybe via web interface

    1. I do not have that functionality planned in the near future.

      But the code supports several access points and connects to one of the available ones, so you can put there some access points, including the same with different passwords.

  4. Yes, its really cool, i love this project, hoping to see web page with SSID & password change feature, plus MQTT password as well.

    Best Wishes,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.