⬆️ ⬇️

Bicycle power monitor PZEM004T and ESP8266, with people's monitoring

I decided to keep my unsuccessful temperature controller project ( Fiasco. The story of one IoT homemade ) as a monitor only. And with the next step I screwed up the PZEM004T energy monitor, for one I implemented the sending of data to the People's Monitor .

Welcome under the category…



I asked the question - where does the current go from the wires? It seems to drown the house with gas, in the house all the diode lamps, turn on the dishwasher in the night, the bath with an electric stove yet, and the electricity all the time goes somewhere. Disorder. We should follow him.



The first step is a general monitoring of consumption.



Tasks



I decided to start hunting a herd of hares. Hares were chosen:





Iron



To solve the tasks was selected:

')



Taking into account that the device will be placed in the metal case of the stabilizer - the external antenna is soldered on the ESP. I tried to power the ESP from the PZEM004T (having soldered the wires to the round capacitor - about 7v on it) - it did not work, when the ESP is connected, the voltage drops to 2v. But in the stabilizer there is already a power supply for 5v - for fans, it means it will be used (I thought it was for 12v, so I suffered for a long time to connect ESP to it - it didn't work in any, I tried a bunch of DC-DC converters until it turned BP and did not read the inscriptions on it).







Firmware



Looked available on the net. As usual, I did not find the right one and decided to write my own.

He took his project for the Sonoff relay for the base (the simplest functionality, on HTTP and on the button, turns on and off, doesn’t know anything else; is used in conjunction with MacroDroid to gently power the phone with the screen constantly turned on - the previous one had a swelling battery from the constant charge ). But besides the functionality of the relay, the assembly has an http server, WiFi settings, NTP, works with the GPIO0 button - different actions on the duration of the press, blinks like a light bulb (for example, counting down the seconds of pressing the button, reflecting the relay status and WiFi) ...



Naturally somewhat modified the settings:







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



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; }else{ flags = (PZEM004Tnb::flags)(PZEM004Tnb::flags::all ^ PZEM004Tnb::flags::energy); } } Pzem004t.updateData(flags); 




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



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



The actual data of the power monitor can be obtained via HTTP:







All data is stored in integers when necessary (for example, when transmitted to People's Monitor ) - a point is added to the desired position.



Implemented data publishing using MQTT / UDP protocol . Added support for this protocol and sensor PZEM004T to your monitor :







Implemented the publication of data on public monitoring :







The guys from the Popular Monitoring have a big respect! Data transfer to the service is elementary, there is a means to see incoming data for interaction debugging, you can simply manage the sensor data.

The system can build test charts (below - porridge from charts, just an example):





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





Naturally, added data publishing settings:





Results



As a result of monitoring in real time, one of two constantly switched on minicomputers has already been turned off, has configured child hibernation on the computer, has re-tuned the sleep mode in the BD player (used only for karaoke).



When the statistics for schedules are typed - I will take further steps.



Whoever wants to have such an energy monitor for himself - well in person for the firmware (Freebie, sir!).



PS



When developing a device, I ran into mysticism - when powering ESP from a USB computer, from any phone charging - everything works. When powered from an embedded BP - does not work. He drew a logical analyzer and a simplescope for investigation - the power supply from the blue block seems to be in order, the signals from ESP are correct, and the reverse is silence. Another power supply - everything works fine.



By scientific method, I realized that when I use an embedded power supply, I connect it to the PZEM004T power supply, that is, in this case, two devices start simultaneously (with other power supplies, simultaneous switching is not possible). And I use a hardware UART for communication, on which ESP throws out a bunch of garbage when it starts. PZEM004T can not digest it at startup and freezes. If the PZEM004T is already on, ESP and the garbage in the port start it without any problems.

The solution was to use SoftwareSerial, everything works fine with it.



Pps



For those who want to make such a device for themselves (and there are such heroes!):

You will need:





What to solder:

On the PZEM004T:

On NodeMCU:





What does the light bulb say:





What does the FLASH button do (when pressed, the bulb counts down the seconds):



Everything else should be intuitive - just go to the ESP browser and read the list of available services to run through them.

Just in case: MAC for National monitoring can be found in wifi-info.

Perhaps a somewhat non-obvious point - the power monitor settings:



The time is indicated without separators (the time is above 7:00 and 23:00), and the correction of the counters is indicated in Wh (usually in life used for Wh).

Source: https://habr.com/ru/post/453472/



All Articles