Smart Arduino Energy Monitor | ESP32 + Raspberry Pi + Home Assistant

by mcmchris in Circuits > Microcontrollers

14517 Views, 56 Favorites, 0 Comments

Smart Arduino Energy Monitor | ESP32 + Raspberry Pi + Home Assistant

MCM-Meter.jpg

Hello to everyone, today I am going to show you a project that I've been working on for a while, it consist on an ESP32 based Smart Energy Monitor that takes current and voltage samples through sensors in our power grid and shares the data to a local server made with Home Assistant and Raspberry Pi.

If you are a visual learner I know that a video worth more than 1000 words, so here is a tutorial video. (I am a Spanish speaker, so please consider turning on English subtitles):


Skills Required

Captura de pantalla (964).png
Captura de pantalla (905).png
Captura de pantalla (981).png

This project may seem difficult or very complex, but it is definitely not completely, since you will have all the guidance for the construction, it was difficult for me to design it to make life a little easier for you. Any conceptual doubt, you are free to ask it without problems.

You should have an understanding of:


  • Basic electronics. (We are going to be using some electronics principles that are good to know to understand how this meter works).
  • Handling of cutting tools and drills.
  • How to solder. (To place the components we need).
  • How to use a meter intruments as multimeters and clamp meters.
  • Writing code in C. (if you want to make any modification to the main code that is provided).

Components and Parts List

Captura de pantalla (899).png
Captura de pantalla (901).png
Captura de pantalla (904).png
Captura de pantalla (902).png

The electronics discrete components as resistors and transistors will be attached in a BOM file in the PCB Step.

🛒Materials:

  1. -Current sensor (CT)
  2. -Voltage transformer (PT) YHDC PE2012-M 230-9v
  3. -OLED Display
  4. -Adafruit ESP32 Feather
  5. -Audio Jack 3.5mm
  6. -SIL connector
  7. -Varistor, 240v-200ma fuse,choke coil.

Circuit Diagram

Schematic_ESP32-EnergyMeter_2021-09-09.png

Here is the Circuit Diagram of our project:

It has all the internal connections of the circuit that will allow us to create the PCB design later.

I also attached the PDF of the Schematics so you can see it better.

PCB Design, Gerber, BOM and Pick & Place Order

Captura de pantalla (897).png
Captura de pantalla 2021-09-10 114259.png

For the implementation of a good project we need a reliable assembly for the circuit that makes it up, and there is no better way to do it than with a good PCB.

Here you can download the Gerber, BOM and Pick & Place Files, the ones you need to order your PCB on your PCB manufacturing company.

I suggest JLCPCB:

📦$2 for 1-4 layers PCBs ⚡ + 🎫 SMT Coupons

Download Free Gerber, BOM and Pick & Place files

Un Zip de .rar file with a software like WinRar or any other.

Project Enclosure (TAKACHI)

pff_01.jpg

I request a free sample of this TAKACHI enclosure for my project, you can instead 3D print yours with the dimensions shown in the PDF.

Downloads

Assembly of the Board

Captura de pantalla (906).png
Captura de pantalla (907).png
Captura de pantalla (908).png
Captura de pantalla (903).png

To start taking everything together follow these steps:

  1. Connect the push button to the PCB
  2. Connect the OLED display to the PCB, the code is made for the SPI configuration.
  3. Connect the AC cable in the PCB terminal block (strip wires)
  4. Connect the voltage selector switch

Programming the ESP32 (GitHub)

Captura de pantalla (912).png
Captura de pantalla 2021-09-10 121906.png
Captura de pantalla (910).png
Captura de pantalla (911).png

Go to my Github repository and follow these steps:

  1. Install the Arduino IDE
  2. Install the ESP32 card package by copying this URL in IDE preferences: https://dl.espressif.com/dl/package_esp32_index.j...
  3. Deploy your server in Home Assistant
  4. Clone this repository
  5. Install the attached ZIP libraries
  6. Open the Arduino code
  7. Modify the **Clavis** variable with a long-term Token generated in your Home Assistant
  8. Modify the variable **serverName** with "http://(IP of your Raspberry Pi):8123/api/states/sensor.meter_ai", Ex: "http://192.168.100.7:8123/api/states/sensor.meter_ai "
  9. Connect your ESP32 and load the program on the board

Configure the Home Assistant Enviroment

Captura de pantalla (913).png
Captura de pantalla (914).png
Captura de pantalla (915).png
Captura de pantalla (916).png
Captura de pantalla (917).png
Captura de pantalla (918).png
Captura de pantalla (919).png
Captura de pantalla (920).png
Captura de pantalla (940).png
Captura de pantalla (922).png
Captura de pantalla (923).png
Captura de pantalla (924).png
Captura de pantalla (925).png
Captura de pantalla (926).png
Captura de pantalla (927).png
Captura de pantalla (928).png
Captura de pantalla (929).png
Captura de pantalla (930).png
Captura de pantalla (933).png
Captura de pantalla (939).png

To make everything works, follow these steps in your Home Assistant system:

  1. Open your home assistant by searching it in your browser.
  2. Go to Supervisor> Adds on and install InfluxDB, Grafana and File Editor.
  3. Open the File Editor and search for the *Configuration.yaml* file
  4. Paste the configurations you found in the Github repository
  5. Reset your server after validating the configuration.
  6. Open InfluxDB> InfluxDB Admin (the crown)
  7. Create a database called "homeassistant"
  8. Create a user called "homeassistant" and add the same password you wrote in the "configuration.yaml"
  9. Open Grafana > Configuration > Data sources > Add data source
  10. Search for InfluxDB and select it
  11. Modify this things:
  • URL: http://a0d7b954-influxdb:8086
  • Database: homeassistant
  • User: homeassistant
  • Password: the same as in the configuration.yaml

12. Go to + symbol and create a new dashboard

  • Select the type of graph.
  • Select mesurement -> V
  • Entity-ID -> voltaje
  • Select -> last()

Fix the System to Your Electric Box

Captura de pantalla (979).png
Captura de pantalla (964).png
Captura de pantalla (980).png
Captura de pantalla (981).png
Captura de pantalla 2021-09-12 002418.png
  1. Round your two main lines with the (current sensor) CT, one directly and other inversed as the diagram shown.
  2. Power the system connecting the AC cables to 120v, line and neutral of your electric box.

System Final Configuration

Captura de pantalla (983).png
Captura de pantalla (984).png
Captura de pantalla (985).png
Captura de pantalla (986).png
Captura de pantalla (987).png
Captura de pantalla (988).png

As you can see in the first picture, the system is up and working but not connected to the wifi yet, for that follow these steps:

  1. Press the config button for 1 second.
  2. A wifi network called ESP32-MCMeter will show up, select it in your phone.
  3. A portal will appear, click on config wifi and enter your wifi credentials and historic KWh usage.
  4. Click on save
  5. Finally press the config for 7 seconds to reset the system.
  6. Now it should be connected to your WiFi and sending data to the server in Home Assistant

Final Test and Enjoy

Captura de pantalla (989).png
Captura de pantalla (990).png
Captura de pantalla (991).png
Captura de pantalla (982).png

Now you will be able to see your power usage statistics in your Home Assistant dashboard.

Here you can see that I compared the measured data with the data shown by the system and they almost match perfectly, so the system is precise enough.