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:
- Monitoring the electrical network via the Internet. I have instant control of network parameters - in the corridor, there is an energy monitor PZEM061 in the dashboard, you can see the voltage, current and power on the screen. But at the place of the display of energy consumed - some kind of abstraction, too few discharges. But in the hallway is not convenient. I want on the phone screen.
- Schedule of electricity consumption. I would like to know when excessive consumption occurs?
- Meter readings via the Internet. This pain is the transfer of meter readings to energy sales. It is necessary to transfer testimony to them from the 15th to the 25th day of the month. I often forget about this and they start calling the robot and writing spam. At the same time, when they remind of themselves - I am usually at work, and the counter is on my pole on the street. I want on the phone screen.
- Stabilizer temperature monitoring. In our village in the winter more than 200v at the entrance to the house does not happen, comes to 140v. Therefore, I do not have a stabilizer for 12 kW, but with such parameters and a long load of 2 kW and taking into account the location of the stabilizer in the wall niche, the stabilizer overheats and shuts down, I had to add a couple of fans (with them the temperature remains within acceptable limits) - they were previously turned on constantly, now under the dough - put the thermostats KSD9700 for 65g, waiting for the winter. I do not want to monitor this parameter, since I can not influence it. But after adding thermostats - you need to control the result.
Iron
To solve the tasks was selected:
')
- 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, consumed energy, as well as the frequency and power factor I do not need. Used with measuring transformer 1: 1000.
- ESP8266 NodeMCU - a universal microcontroller with WiFi, it fits in well with the PZEM004T in size - can be connected in racks using the holes on the boards. Also on the NodeMCU board there is a useful Flash button (connected to GPIO0) - it is convenient to use it to control the mode of operation - for example, to enable SoftAP.
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:
Codestatic PZEM004Tnb::flags flags = PZEM004Tnb::flags::all; static unsigned long lastReadEnergyTime = 0; if (Pzem004t.isDataUpdated()) { setLedState(3);
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:
Module NodeMCU (optimal in size - those that c soldered module ESP12 and not mini) - Power Module for NodeMCU - this board understands power supply 3.3v, 5v, 6..12b - at different inputs.
- Wires, connectors, racks, housing, buttons, ... - to taste
- DS18B20 temperature sensors - from 0 to 20pcs
What to solder:
On the PZEM004T:
- we modify the resistor - so that the module understands the 3.3v logic, the resistor R17 1 kOhm should be soldered on top of another one the same, reducing the resistance to 0.5 kOhm (or you can replace R17 with 470 Ohm).
On NodeMCU:
- Power supply of the module - depending on the voltage of the power supply: 3.3V to 3V3, 5V to VU, everything above 5V to Vin
- Rx and Tx for communication with the power monitor are D6 and D7 respectively. If it does not work (see below about the light bulb) - swap places
- DS18B20 sensors - on D5, GND and 3V3 (parasitic power was not tested)
What does the light bulb say:
- At the start - a series of three long flashes and then a pause, and so on until the first data is received.
- After a couple of seconds, data reception should start - 3 ... 4 short flashes, then it goes out - this is the normal mode. So about every 2 sec. Rare single flashes are possible.
- Regular single flashes - data is not accepted, some kind of trouble with an energy monitor or communication channel.
- Constantly on, or constantly off - some kind of trouble, such blinking modes are not provided.
What does the FLASH button do (when pressed, the bulb counts down the seconds):
- Up to 4 seconds - nothing is done
- 4 ... 10 seconds - the access point is turned on without a password. Connecting to this access point and opening the address http://192.168.4.1/wifi in the browser - you can connect to the router. The list of available networks can be obtained at http://192.168.4.1/wifi-scan - the first call starts scanning, repeated in 3 ... 5 seconds (no need to hurry) returns the list of networks.
- More than 10 seconds - reboot.
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).