Ever wanted to check your own power consumption? If the answer is yes - this project may just spark your interest!

A friend of mine wanted to keep logs of his power consumption.

He also wanted to make sure the wattage of appliances is what they state they are.

As he has not got a smart meter (even if he did, there would not be a log every second), he asked if I could come up with a solution.

He would prefer it to be Wi-Fi, so the P.C. does not have to be moved next to the power monitor.

So, the Mains Power Monitor was developed.

Hardware:

ESP32 Wroom-32 Module with integrated Wi-Fi. (Other ESP32 modules can be used). I am using a Wroom version, but the Lolin will work just as well.

Solderless Breadboard

Micro USB LEAD

4x Male / Female Dupont Jumper Wires

Mains rated twin-core cable protected with a maximum 3 amp fuse.

Power monitor PZEM-004T.

The version I bought has a plastic enclosure (Vital to prevent mains shock) and a 100A Split transformer:

Picture1.png
Capture.PNG

IMPORTANT HEALTH WARNING: Mains can kill. Only attempt this project if you are competent at mains wiring!

I would recommend the mains of PZEM-004T to be wired to a 3 amp fused mains plug.

Only purchase the PZEM-004T that has a plastic casing 

Note: The split current transformer only clips onto a single wire of the consumer unit:

plug.jpg

You will notice that 5V of PZEM-004T connects to 3V3 of ESP32.

That is not a typo!

The Wi-Fi board supplies the power to the PZEM-004T, not the other way around.

Since the ESP32 pins are only 3V3 tolerant, the PZEM-004T works just as well at 3V3.

As there is less hardware involved, you will not need to level shift.

I need to find a nice simple solution to get the ESP32 programmed as I am not an expert in C or MODBUS.

So not only does the hardware work, but it can also send the data via Wi-Fi to a P.C.

The P.C. can save logfiles of power consumption and display all the data in an eye-pleasing format.

I decided Flowcode would suit this application perfectly!

With Flowcode, you just create the flowcharts of what you want to do, and it does all the rest for you.

You can get a free 30 day fully unlocked version from this website.

If you already had the free trial for the 30 days, then to get this project working will cost a one-off cost of £25

This is because you will need to purchase comms pack C for the Modbus communication.

If you look here, you will see what other communication protocols comms pack C will cover.

I will not be going into the basics of Flowcode for a few reasons.

1. There are great beginners guides on how to use Flowcode.

2. Both flowcharts (Firmware and App developer) for the Mains Power Monitor have already been created for you.

3. I do not want to give you too much to read before you can start using the Mains Power Monitor

This is the end result using Flowcode App Developer:

Picture2.png

Below is a small sample of what the logs look like that is saved on the P.C.:

Example from the generated CSV logs:

Picture3.png

From Row 46 is when I switched my kettle on.

There is also a Console view (View Tab, Console):

cons.png

Also Console Writer View:

conso.png

Steps to program ESP32 module.

1. You will need to download the ESP32 firmware flowchart from here.

Scroll down to the Mains Power monitor App, then click on the Firmware link.

Save the Firmware - ESP32Wroom Wifi.zip to your P.C., then open the zip file & extract the firmware file (at the time of writing, its name is Mains PMon v1.0-ESP32 Wroom Firmware.fcfx) to a folder of your choice that does not require elevated accesses permission.

You will need the firmware for step 8.

2. Register for a Flowcode account.

3. After receiving a confirmation email, download the ESP32 toolchain and Flowcode.

4. Install the ESP32 tool by carefully following these instructions.

5. Download and install the correct drivers for the ESP32 board you have.

Depending on which ESP module you have, either the CP210x Universal Windows Driver or CH340 Driver will require installing first.

I’m using ESP32-Wroom-32, which uses CP210x.

I would recommend restarting your P.C. after drivers are installed.

6. Make sure you have the ESP32 board connected to your P.C., then look in the Device Manager (Right select window icon and select Device Manager).

Expand Ports (COM & LPT) and note which port the ESP32 board is connected to.

In my case, It is COM4:

Picture4.png

7. Run Flowcode and select the 30-day trial, then Enter Username and password.

8. Restart Flowcode, select File, Open Project & Browse.

Browse where you saved Mains PMon v1.0-ESP32 Wroom Firmware.fcfx), Then select open.

9. Select View, Component Properties:

Picture5.png

Or you can select View, 2D panel and right-click on a blank area, choose Properties.

Replace SSID and Password with the ones for your router:

Picture6.png

10. Select Build Ribbon, Project Options, select the correct Programmer Port:

Picture7.png

Select Modify.

The board we are using is ESP32-Wroom 32.

If that is the board you have, then skip to 13.

If you have a different board, e.g. Lolin Light, then while you are within Project Options, you will see ESP32-Wroom 32 highlighted.

Right-click and select search & enter a keyword, e.g

Picture42.png

lite or loli etc.

Select Find Next, and the new board/Microcontroller will be highlighted.

targ.png

Select X or Cancel Then Modify.

11. Staying within the Build ribbon, Select Compile to Target.

If, when compiling to target, you see error messages about not a recognised command, try restarting your P.C.

As This will be the first time of compiling, the time taken to compile may take up to a couple of minutes:

Picture8.png

All being well, the Esp32 board should be programmed:

Picture9.png

With the ESP32 running, it will connect to your Wi-Fi network.

You can confirm this by using a free small utility called Wireless Network Watcher.

The assigned I.P. address for your ESP32 board will be automatic.

Therefore it will be different on each setup.

Here is what it looks like for me:

Picture10.png

I have blanked most information and outlined the two I.P addresses we will need to know.

12. Download the App developer zip, and extract all the files to a suitable folder on your P.C.

Run: Open Mains Power Monitor v1.0.bat file.

Select the drop-down arrow and keep selecting different network interface numbers until you see your P.C.’s I.P. Address:

Picture11.png

Sometimes I have found this only works on P.C.’s that have a connection to Wi-Fi.

My main P.C. is only Ethernet.

I have tried with Ethernet connections and can only see 0.0.0.0 for the I.P. Address.

What I did was order a cheap USB Wi-Fi adapter, and that worked perfectly!

When you see the correct I.P. address of your P.C., enter the I.P. details of your ESP32 board (Espressif Inc.) from The Wireless Network Watcher

Picture12.png

Now you can run the App developer.

When you enable CSV logging, the X.Y. charts will also start logging.

So the CSV file does not get too large and unmanageable, a new CSV file with an incremented number will be automatically generated after every 86400 rows.

When you click on the Reset switch, the number will reset back to 1.

So make sure you have all the CSV files backed up first.

If you have any technical issues, questions or App suggestions, then post a question on the Flowcode forums

Troubleshooting.

PZEM-004T, none, one LED flashing or both constantly rapidly flashing.

There are a few different causing for this.

No LED’s are flashing, then check:

1. 3V3 & GND supply is correct and going to 5V & GND of PZEM-004T.

2. Pin Connections within MODBUS properties and hardware match.

3. ESP32 is correctly programmed with the correct firmware.

Only LED nearest +ve supply connection is flashing:

1. MODBUS is at the wrong frequency.

Check those MODBUS properties are set for 9600 Baud.

Within properties, select ModbusMaster1 from the drop-down arrow 

Picture62.png

You can check the frequency of ESP32 is correct by doing a one second Flash test.

2. Mains live/neutral not connected.

When the mains is connected, there is an LED that lights between the mains terminal block and X type capacitor.

App Developer not picking up the ESP32 Wi-Fi

Use the previously mentioned Wireless Network Watcher.

If Espressif Inc is not on the list, then ESP32 is not connected to the Wi-Fi

1. Make sure that the correct router SSID and password is correctly entered within the properties: 

Picture83.png

Select the drop-down arrow then 2D Dashboard Panel on the popup window to enter SSID and Password.

2. ESP32 has not been programmed with the correct firmware.

3. ESP32 board out of range or Wi-Fi signal is being blocked.

This can be eliminated by placing the ESP32 near the router to see if communications have been established.

4. You could be selecting the wrong Espressive Inc.

For example, you could have more than one ESP32 board or have smart Light bulbs or sockets that use ESP32.

If multiple Espressive Inc is displayed, then unplug the board without refreshing Wireless Network Watcher.

You may need to wait a minute, but the Wi-Fi board you unplugged will show as a red X:

2.png
Alfie Smith