There is a blog post explaining this project in depth at: https://pablocruz.io/iot-energy-monitor/
The idea of this sensor is to monitor the current level (in Amps) of an electric line and report its values at 5 minute intervals.
The purpose of the project was to monitor the electricity consumption of my home throughout the day and estimate the amount of solar panels I would need to produce that amount of energy.
I chose to use 3 sensors to monitor the electrical consumption in different circuits of the main panel. This allowed me to check if the loads were propperly balanced (they were not) and if electricity consumption was within a saftey range.
Afterwards I integrated the sensor with a Raspberry Pi. Using Mosquitto MQTT, Node-RED, InfluxDB and Grafana, I was able to store and visuallize the information, as well as adding some Telegram, alerts for when power consumption is very high.
With the sensor and this setup I could vidualize several interesting things, such as the massive spikes that microwave ovens produce when they are turned on, or that most of my electric consumption comes from my refrigerator.
The setup uses a SCT-013 non invasive current sensor, connected to an ESP32 and an electrical outlet to read and send the data through Wi-Fi and MQTT. The ESP32 is needed for the 3 sensor version, but for a 1 sensor version, an ESP8266 will also work. (See the image of the first prototype below).
The microcontroller takes measurements every 5 minutes and transforms the sensor value in mA to its estimate in Amps. Voltage is hardcoded as 120V but can be susbstituted by a voltage sensor if more precise calculations if needed.
Measurements are then sent through WIFI to an MQTT broker. With a Raspberry Pi and using Mosquitto MQTT, Node-Red, influxDB and Grafana a home monitoring system can be set up.
Mosquitto MQTT is an MQTT broker, which allows Node-Red to read the MQTT messages from the sensor and store them in a database (influxDB). Grafana is a data visualization tool that can extract the information from InfluxDB and display it in a visually apealing and interactive way. Node Red can also be integrated with Telegram, which allows message alarms to be sent in case the electricity level rises above a threshold value.
(Electric power consumption in Mexico is estimated in Kwh (Kilo Watt/hour), If your country does it differently, this may not apply).
- We asume Voltage = 120V (or 220V depending whey you live)
- We take our average Amperage reding from the sensor. Say 10 Amps (as an example)
- Our Power value, in Watts, would be: P = 120V x 10A = 1,200W
- Our Energy value would be: E = P x T, where T = time (expressed in hours).
- Our Energy value would thus be: E = 1,200W x 1 = 1,200 Watt/hour
- Finally, we divide by 1,000 to get KiloWatt/hour values = 1.2 Kwh
To use this script, you must have the following:
Hardware:
- 1 ESP32
- 3 SCT-013 current sensor
- 3 10uF Capacitors
- 9 10k Resistors
- 3 20k Resistors
- An Electrical connection to power microcontroller and sensor
- A breadbord, protoboard or PCB
- Optional: 3D Printed Sensor Case
Software:
- ESP8266 Boards installed from the Boards Manager
- Arduino Library: EspMQTTClient.h
- Emonlib Library: EmonLib.h
The sensor requires 3V input to work and so can be powered directly from the microcontroller.
-
Open the 3CurrentSensorMQTT.ino file:
- Add your Wi-Fi username & password
- Define your MQTT port (1883 is conventionally used)
- Add your MQTT broker Username & Password
- Define a name to identify your sensor
- Define the MQTT topic the sensor will publish the data into
-
Verify and upload your code
-
Check with the serial monitor that the values are being reported
-
Check with your MQTT broker that values are being reported correctly through Wi-Fi.
Output should be something similar to the following:
In this example, we are suscribing to a single sensor topic named "Cordilleras/medidor_corriente1", not all three.
The purple nodes read the MQTT information.
The orange function nodes save the information of each sensor as a variable.
Green Nodes are for debuging and testing.
The blue node executes automatically every 5 minutes.
The Orange node saves all three variables as a single payload.
Finally, the brown node saves the information into InfluxDB.
Below is an image of how the information looks when written into influxdb and then displayed in Grafana. The graph shows a 24 hour period.
You can see how the first electric line (medidor 1) uses more electricity than the second or the third line.
You can also see periodic spikes of about 5 amps. This is the refrigerator motor turning on. Finally, you can see some massive spikes at 8am, 4pm and 8.30pm. This is the microwave turning on during breakfast, lunch and dinner.
If you compare the electricity consumption of line 1, 2 and 3, it is clear that loads are unbalanced, since most of the electric consumption is going on in line 1.
Once the hardware is setup, I used the following procedure to calibrate the sensors:
- Split an extention cord so that the line and neutral wires are separated.
- Hook up the extention cord to the main power.
- Connect a heater, blow drier or any device that uses a significant amount of electricity.
- Hook up the sensors to one of the extension cord lines.
- Hook up a professional current sensor to the same extension cord line (borrow or rent one if you dont have one)
- Turn the heater on.
- Compare the values in your sensors to the current sensor.
- Adjust the calibration value in the code.
- Upload the code and try again until readings match.
Since all three sensors are hooked up to the same line, you can also check if readings are consistent.
The line of code where the sensors can be calibrated is:
energyMonitor1.current(ADC_INPUT_1, 0.145);
Here is a gif of the procedure: