danpeig / ESP32EnergyMonitor

Multiple phase DIY energy consumption monitor using ESP32 and ESPHome
GNU Affero General Public License v3.0
142 stars 24 forks source link
energy-consumption energy-monitor esp32 esphome open-source openenergymonitor

ESP32 + ESPHome Open Source Energy Monitor

Introduction

For months I've been looking for a non-intrusive energy monitoring solution for my home. The residence receives more than one phase from the utility company and the available solutions in the market based on open protocols are extremely expensive for this setup. Cheap alternatives always rely on cloud services and I don't believe any of them will remain online for too long.

The solution I present here is a DIY project built on the top of Open Energy Monitor and ESPHome framework. I selected the ESP32 controller because it has several analog ports to measure voltage and current.

Features

Hardware

Part list

N = number of phases to be measured (1, 2 or 3)

Parts and final assembly Parts and preliminary assembly of the prototype board

Using a different model of the current sensor

There are currently two types of current sensors under the SCT013 family:

The default circuit used for this project uses a 100A range transformer with a 50mA output. If you are going to use another range of sensor (10A, 15A, 30A, etc...) but still with mA output, you need to recalculate the burden (22Ω) resistor. Open Energy Monitor has a nice tutorial on how to do that.

If you decide to use a 0-1V output sensor, the burden resistor (22Ω) will not be required because it is already embedded in the probe. Later, in the software configuration, you need to adjust the ADC attenuation to improve the accuracy in this reading range of your probe.

Multiple voltage measurements

If the energy monitor is going to be installed at the inlet of a property, the phases should be balanced and there is no reason for having more than one voltmeter. I understand that for some rare scenarios, the user would prefer to have different voltage measurements one per phase. Fortunately both the hardware and the software are prepared for that.

ESP32 analog input pins

The ESP32 DEV Board has two ADC (Analog to Digital Converter) registers but due the Wifi communication, only one is available for this project. Therefore, sensors can only be connected to the following GPIOs: 32, 33, 34, 35, 36, 37, 38 and 39.

Diagram and assembly

This diagram illustrates the typical connection of a single current probe and a single voltage probe. Multiple current probes require a set of resistors and capacitors for each one. The 22Ω burden resistor is not required for current probes that output 1V.

The voltage meter must be installed between the neutral line and one phase, not between phases.

The current clamp must be installed in a specific direction according to the current flow. Usually, the arrows in the clamp housing point to the right direction but don't hesitate to change if you note that the power factor and real power readings are too low.

ESP32 Energy Monitor Schematic

Software

The project repository contains 3 firmware sources for the ESP32:

  1. The voltage sensor potentiometer calibration tool. It is used only once to set the position of the potentiometer screw.
  2. The voltage and current sensor calibration tool. It provides a quick way to determinate all calibration coefficients. You could do this directly from the ESPHome firmware but it takes much longer to compile, boot and measure. This tool can also be used to build your own firmware without using ESPHome.
  3. The final ESPHome firmware source.

All tools require editing some configuration files before uploading to the ESP32 board.

NOTE: The EmonLib used in the package was modified by Savjee to support the ESP32 board.

Requirements

Adding support for the ESP32 to Arduino IDE

Before we can compile ESP32 firmware using the Arduino IDE, we need to install the support for this board.

Add the following URL to the Aditional Boards Manager in Arduino IDE preferences: https://dl.espressif.com/dl/package_esp32_index.json

For more information, there is a detailed tutorial in the reference links below.

Calibrating the ZMPT101B potentiometer

The ZMPT101B has a potentiometer (blue component with a screw on top) that needs to be adjusted only once. The calibrate-pot.ino sketch was developed to be used with the Arduino IDE serial plotter for this task.

  1. Set up the hardware
  2. Plug the ZMPT101B to an AC voltage source. This voltage should be close to the voltage you will eventually read during the operation of this device. Do not connect to AC sources that generate square waves (no-break, inverters, etc...).
  3. Edit calibrate-pot.ino and change the GPIO pin according to your hardware.
  4. Compile and upload the code from Arduino IDE.
  5. Open the Serial Plotter view, set baud rate to 115200bps.
  6. Turn the potentiometer screw clockwise or counterclockwise until you see that the bottom and top of the curves transition from flat to curved. Do not exceed too much the transition zone (between flat top and curved top) because it decreases the sensibility of the sensor.

Potentiometer out of calibration Potentiometer not calibrated (flat top)

Potentiometer properly calibrated Potentiometer calibrated (round top)

Calibrating the current and voltage sensors

The sketch calibrate-vi.ino should be used to calibrate the current voltage sensors. It outputs the calibrated values every couple of seconds for each one of the sensors. The ESPHome firmware can also be used but the entire calibration process will take much longer since the device has a longer initialization cycle.

This piece of code can also be used to create your own Energy Monitoring solution without relying on ESPHome at all.

  1. Set up the hardware, the potentiometer of the voltage sensor should be already adjusted.
  2. Find an "almost pure" resistive load (no motors, no reactors, no electromagnets, no LEDs). Examples: heater, boiler, electric shower, electric oven, kettle...
  3. Install a voltmeter and ammeter to use as reference.
  4. Connect the voltage measurement and current measurement sensors. Please note the direction of the current clamp is important.
  5. Edit the sketch calibrate-vi.ino and set the correct GPIO pins for the sensors.
  6. Set the calibration coefficients CV1, CV2, CV3, CI1, CI2 and CI3 to 1000 in the same file.
  7. Compile and update the code from Arduino IDE.
  8. Watch the values in the serial terminal and wait for them to stabilize. Use 115200bps as baud rate.
  9. Take a note of the measured current (I) and voltage (V) from the ESP32 and the current and voltage from the reference voltmeter (Vr) and ammeter (Ir).
  10. Calculate the calibration factors: CVnew = Vr x CVold/V, CInew = Ir x CIold/I where CVold and CIold are the previous calibrations from the sketch (initially 1000).
  11. Change the values under the "Calibration" section of the code to the calculated ones (CInew and CVnew).
  12. Compile and upload the code again, watch the serial monitor until the data stabilizes and then check if the measurements are correct.
  13. Repeat steps 8 to 12 if necessary.

TIP: You can later fine tune the calibration by applying ESPHome filters to each sensor. Note that the "Apparent Power" and "Real Power" will also require calibration because they are computed before the current and voltage filters kick in.

Configuring the ESPHome firmware

esp32emon.h configuration file

Minimum settings

Advanced/optional settings

secrets.yaml configuration file

esp32emon.yaml configuration file

Minimum settings

Advanced/optional settings

Flashing ESPHome firmware to the ESP32 board

Please refer to ESPHome website for detailed instructions.

TIP: If you want to go fast, avoid Docker version of ESPHome, use manual installation instead. The connection to hardware devices from the manual installation is much easier than from a container.

Home Assistant Integration

Home Assistant integration is enabled by default. The device should be automatically discovered if in the same network as the Home Assistant server (it can take a up to 30 minutes). If not, add the integration manually using the IP address of ESP32 as host address.

If you have a complex Home Assistant setup, with multiple networks or servers and the default integration does not work, you can fall back to the Home Assistant MQTT mode. Just enable MQTT and the discovery mode in esp32emon.yaml.

Home Assistant Integration Screenshot Home Assistant Integration after automatic detection

MQTT Integration

MQTT integration is disabled by default. To enable, uncomment the lines in the yaml configuration file and adjust the settings in the secrets.yaml file.

Frequently asked questions

Download

Version history

Further reading and references