fvilmos / power_meter_mqtt

esp32 power meter for sensors with pulse output. Implements MQTT for status reporting. Accurate and easily can be integrated with Home Automation systems.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Power Meter with mqtt-for devices with pulse output

About

This project presents a simple solution for measuring the energy consumption of a house. The key element is a cheap energy monitor with pulse output, connected to an ESP32 WROOM 32 board. This, over WiFi using mqtt protocol can be easily connected to a home automation system to report:

  • instant power (in Watt);
  • total power consumption (in kWh);
  • Device temperature (in Celsius) using a DS18b20 temperature sensor.

OTA support is enabled, after the first software upload, the ESP will available for updates on the local network over Arduino IDE.

Hardware Components

ESP 32 board

ESP32 WROOM 32 board

The Power Meter

The power meter adeleq_02-553_DIG operating range is 0.05A-45A, and (the most important part) provide 2000 impulses / kWh. Emitted pulses for detections have ~90 ms pulse width. The output is open collector, operating voltage, from 5-24V. In this case, the ESP32 supply provides 3.3V, but this is not a limitation. Other variants are available on the market, usually, the difference is the accuracy (i.e. pulses / kWh are only 1000 instead of 2000).

300 300

Below a sample picture for the output, where a light bulb (~53W) is connected to the power meter. The measured pulse between the two detections is about 30.71s, which leads to a value measured of ~58.61 Watts.
Conversion from kWh to Watts, measured with a sensor of 2000 pulses/ kWh:
(<kilo Watt to Watt>*<Hour in secounds>/<impuse number>)/<time measured between pulses> 
=>(1000*3600/2000)/31.71 => 58.71W

The temperature sensor

DS18b20 is a fast digital thermometer using 1-Wire protocol. The usual connection diagram is with a 4.7 kOhm pull-up resistor.

Connection diagramm

Below the wiring diagram, made with Easyeda tool. In noisy environments, the recommendation is to add a 33pf condensator between ground and energy sensor input pins (to avoid sporadic interrupts).

Final integration

400 400

How to use

Clone the repository, update the .ino file in Arduino IDE with your local WiFi / mqtt configuration.

Note: you need to install the following libraries before compile: PubSubClient - for mqtt client, OneWire - for 1-wire communication and DallasTemerature - for DS18b20 temperature sensor.

Note: to enable serial debugging, uncomment the following line: #define DEBUG

Note: once flashed the the ESP32, after a succesfull connect to your WiFi, OTA update become available. This means the device can be mounted and the software can be flashed / updated over the your local network (see the new port under ArduinoIDE->tools->port->network ports).

/*mqtt declarations*/
const char* mqtt_server = "<IP OF THE MQTT SERVER>";
const char* mqtt_user = "<USER NAME>";
const char* mqtt_password = "<PASSWORD>";
...
/* SSID and Password of WiFi router */
const char* ssid = "<ROUTER SSID>";
const char* password = "<ROUTER PASSWORD>";

Upload the code in the ESP32 WROOM 32 board over USB, wire the board accordingly to the schematics. The output will be something similar on the mqtt topics:

400

Additionally the following mqtt commands can be used:
- topic: /pulseenergymonitor/cmd
- payload: c / r
  c - clear kWh information
  r - reset the device

Home Assistant integration

Home Assistant is an open source automatization system, with a high number of component integratios. Is a good choise for complex IOT and DIY smart home atomatization. Below a panel component and a log file which shows the sensor information and energy consumption.

400 400

Paste the following lines in the configuration.yaml file:

sensor:
  - platform: mqtt
    state_topic: "/pulseenergymonitor/watts"
    name: "Pulse Energy Monitor"
    icon: mdi:gauge
    unit_of_measurement: "W"
  - platform: mqtt
    state_topic: "/pulseenergymonitor/kWh"
    name: "PEM kWh"
    icon: mdi:gauge
    unit_of_measurement: "kWh"

Energy consumption measurement has a specific component in Home Assistant, the utility meter, past into configuration.yaml file.

##################################
# Utility meter
##################################

utility_meter:
  daily_energy:
    source: sensor.pem_kwh
    cycle: daily
  weekly_energy:
    source: sensor.pem_kwh
    cycle: weekly
  monthly_energy:
    source: sensor.pem_kwh
    cycle: monthly

/Enjoy.

About

esp32 power meter for sensors with pulse output. Implements MQTT for status reporting. Accurate and easily can be integrated with Home Automation systems.

License:GNU General Public License v3.0


Languages

Language:C++ 100.0%