Bicycle from the energy monitor PZEM004T and ESP8266, with People's Monitoring

    I wondered - where does the current from the wires go? It seems like we drown the house with gas, in the house all the lamps are diode, we turn on the dishwasher at night, there are no baths with an electric stove yet, and the electricity goes off all the time. The mess. It would be necessary to follow him.
    Welcome to the cat…

    The first step is general monitoring of consumption


    I decided to start a hunt for a herd of hares. Hares were selected:

    • Electricity monitoring via the Internet. I have instant monitoring of the network parameters - in the corridor, in the panel there is an energy monitor PZEM061, on the screen you can see the voltage, current and power. But at the place of display of the consumed energy - some kind of abstraction, too few discharges. But the corridor is not comfortable. I want on the phone screen.
    • Graph of electricity consumption. I would like to know when does excessive consumption occur?
    • Meter readings over the Internet. This pain is the transfer of meter readings to energy sales. It is necessary to give evidence to them from the 15th to the 25th day of the month. I often forget about it and they start to call the robot and write spam. Moreover, when they remind of themselves - I usually at work, and I have a counter on a post on the street. I want on the phone screen.
    • Monitor stabilizer temperature. In our village in winter there are no more than 200v at the entrance to the house, it reaches 140v. Therefore, I do not have a 12kW stabilizer, but with such parameters and a long load of 2kW and taking into account the location of the stabilizer in the wall niche, the stabilizer overheats and turns off, I had to add a couple of fans (with them the temperature remains within acceptable limits) - they were previously turned on constantly, now kneading - put thermostats KSD9700 on 65g, we are waiting for the winter. I do not want to monitor this parameter, because I can’t influence him. But after adding thermostats - you need to control the result.


    To solve the tasks it was chosen:

    • PZEM004T - energy monitor with UART. It allows you to get the parameters of the power grid - one parameter every 0.6 seconds: voltage, current, power, energy consumed, as well as the frequency and power factor that I do not need. Used with measuring transformer 1: 1000.
    • ESP8266 NodeMCU - a universal microcontroller with WiFi, it fits well with the PZEM004T in size - can be connected by racks using the available holes on the boards. There is also a useful Flash button on the NodeMCU board (connected to GPIO0) - it is convenient to use it to control the operating mode - for example, to enable SoftAP.

    Given that the device will be placed in the metal housing of the stabilizer - soldered an external antenna to the ESP. I tried to power the ESP from the PZEM004T (soldering wires to the round capacitor - it’s about 7V on it) - it didn’t work, when I connected the ESP, the voltage drops to 2V. But the stabilizer already has a 5V power supply - for fans, which means it will be used (I thought it was 12v, so I suffered for a long time to connect the ESP to it) - it didn’t work in any, I tried a bunch of DC-DC converters until I turned it over BP and did not read the inscription on it).


    Looked at available on a network. As usual, I did not find a suitable one and decided to write my own.
    I took my own project for the Sonoff relay as the base (the simplest functionality, it turns on and off via HTTP and by a button, can’t do anything else; it is used in conjunction with MacroDroid to sparingly power the phone with a constantly on screen - the previous battery was blown up by a constant charge and squeezed out the screen ) But in addition to the relay functionality, the assembly has an http server, WiFi, NTP settings, works with the GPIO0 button - different actions from the duration of pressing, blinks in any light (for example, counting the seconds of pressing the button, reflecting the state of the relay and WiFi) ...

    Naturally, I modified the settings a little:

    I looked at the existing library for working with PZEM004T - I did not like it. She sends a request, and then in a closed loop waits for a response. This is not right. I wrote my library, asynchronous - I tell it from the main program what parameters I want to get, and then periodically check if the required data is received:

    The code
    static PZEM004Tnb::flags flags = PZEM004Tnb::flags::all;
    static unsigned long lastReadEnergyTime = 0;
    if (Pzem004t.isDataUpdated()) {
    	setLedState(3); // радостно поморгаем по поводу получения данных
    	// ...
    	unsigned long currentTime = millis();
    	if ((currentTime - lastReadEnergyTime) > 6000) { // раз в 1 мин читаем счетчик
    		flags = PZEM004Tnb::flags::all;
    		lastReadEnergyTime = currentTime;
    		flags = (PZEM004Tnb::flags)(PZEM004Tnb::flags::all ^ PZEM004Tnb::flags::energy);

    I took into account that the PZEM004T considers a maximum of 9999 kW * h, then it is reset - I implemented the overflow accounting. Implemented a two-rate account. I also implemented the accounting of the average values ​​of the parameters - the readings are read about once every 2 seconds, and on the People’s Monitoring it is necessary to transmit data every 5 minutes, naturally average.

    Added to the system work with an array of sensors DS18B20. Data is read in turn with a period of 2 seconds per sensor. Those. we are looking for a sensor, we have found - we are receiving data, after 2 seconds we are looking for the next one, etc. Ended sensors - start from the beginning. Those. when using only one sensor, its polling period is 4 seconds. For these sensors, the average values ​​were also calculated.

    The current energy monitor data can be obtained via HTTP:

    All data is stored in integers when necessary (for example, when transferred to People’s Monitoring ) - a dot is added to the desired position.

    Implemented the publication of data using the MQTT / UDP protocol . I added support for this protocol and the PZEM004T sensor to my monitor :

    This is my failed temperature controller project ( Fiasco. The story of one IoT homemade product ), which I decided to leave as the only monitor.

    Implemented the publication of data on People’s Monitoring :

    Children from People’s Monitoringgreat respect! Transferring data to the service is elementary, there is a means of seeing the incoming data to debug the interaction, you can simply manage the sensor data.
    The system can build test graphs (below is a mess from the graphs, just an example):

    It is also possible to notify about the status of sensors (temporarily disabled data transfer for the test):

    Naturally, I added the settings for publishing data:


    As a result of real-time monitoring, he already turned off one of the two constantly turned on minicomputers, configured hibernation on the baby’s computer, and reconfigured sleep mode in the BD player (used only for karaoke).

    When statistics are collected for the charts, I will take further steps.

    Who wants to get hold of such an energy monitor - in a personal account for firmware (Freebie, sir!).


    When developing the device, I ran into mysticism - when ESP is powered from a USB computer, from any phone charge, everything works. When powered by an embedded PSU - does not work. I used a logic analyzer and a simplescope for investigation - the power from the blue block seems to be in order, the signals from the ESP are correct, and back - silence. Another power supply - everything works fine.

    By the method of scientific poking, I realized that when I use the built-in PSU, I connect it to the PZEM004T power supply, that is, in this case two devices start simultaneously (with other PSUs, simultaneous switching on is not possible). And I use hardware UART for communication, on which ESP throws a lot of garbage at startup. PZEM004T cannot digest this at startup and freezes. If PZEM004T is already on, it will start ESP and garbage in the port without any problems.
    The solution was to use SoftwareSerial, everything works fine with it.


    For those who want to make such a device for themselves (and there are such heroes!):
    Description in the catalog of devices of National Monitoring

    Also popular now: