ESP8266 + Raspberry Pi Electricity Monitor
I have always been monitoring my utility meters in the old fashioned way. First I wrote them down in a little ledger (yep, I’m that old), later I wrote a spreadsheet for it and the last few years I’m using the ECAS smartphone app to do the trick.
I always wanted a live monitor, but they are rather expensive and do not give all the information I want.
So a few months back I stumbled up on the Arduino, quickly learned about the ESP8266 wifi controller and the amazing Raspberry Pi. A whole new world opened up and I finally found a sollucion for many ideas.
My first little project, a live Electricity Monitor, took me a lot of time. First of all I had to learn the basic electricity laws, then a bit of Arduino programming, later I had to go deep into the ESP8266 and the Lua language, and finally setting up a little Raspberry Pi home server and a mySql Database.
The initial goal for this project is to monitor electricity consumption, log the data and being able to analyse the collected data.
Our electricity suplier is a local wind farm. Of course we are just connected to the regular grid, but in a way I never thought it to be fare I could just switch on the dishwasher on windless days. So I integrated a live feed for predicted wind energy supply from the national grid manager (Elia, Belgium). With this information I can decide when I can switch on the dishwasher. At this time I am waiting for the components to build a smart wifi power plug to switch the dishwasher at the best calculated time in function of the available wind energy.
The ESP8266 is not only a wifi module, it basicaly can do most of the tricks an Arduino can. Unfortunately it was a bit hard to handle. I had a lot of issues with memory which wouldn’t clear and crash the module. But I learned to write lean and finally got in under controle. I used the ESP8266-1, which has only 2 GPIO pins, but enough to build the electricity meter.
Step 1: The Basic Components
The project is build around 5 basic components
- a wifi Electricity Meter sensor
- a wifi Raspberry Pi
- XML Wind Energy feed form the Grid Manager
- a browser
- The Great Electricity Gulping Diswasher
How it works :
The Electricity Meter Sensor is an infrared sensor, focused on the rotating wheel from the electricity meter. Every time the red line passes, the emitted infrared light is absorbed and reflects a bit less light than normal. The sensor is set at this treshhold and changes I/O.
The I/O from the sensor is connected to the ESP8266’s GPIO pin 0. When the pin changes from I to O, it is programmed to “charge”, and when it changes back form O to I it “fires” a short message over wifi to the MySql database on the Raspberry Pi.
Every 5 minutes the Raspberry Pi downloads the latest wind energy forcast XML from the Grid Manager’s website. The XML is translated into an SQL command and the data is stored in a mySQL table on the Raspberry Pi.
The first page on the browser is the actual electricity consumption. On the next page you can check out the daily use per hour. The third page gives the wind energy forcast and actual production, displayed in a Google chart.
And finaly the magic happens : I check out the acutal electricity consumption on the browser. If we don’t use too much at the time, I check out the wind forcast. And when there is enought wind I switch on the dishwasher with my right index finger.
The right index finger part I would like to automate in my next IoT project.
Step 2: The Monitor Sensor
The sensor is actualy pretty easy. The emitter sends an infrared signal out. The signal is reflected on the metal wheel of the electricity meter. When the red line passes, the infrared light is absorbed a bit more than normal. The infrared sensor is set to detect the change in brightness and switches form on to off. The ESP8266 is running a simple programs to “charge” and “fire” a message to the database.
What you need for the sensor :
- infrared transciever. I used the Keyence PZ-51. It was a leftover from a dismanteled machine at work and is very sensitive. The electricitymeter is build into a plexi sealed box, with a 5 cm gap between the window and the wheel. This sensor is perfect for this inconvenient position.
- step down voltage regulator. The ESP8266 needs 3,3V. In the steep learning curve to control this little wild horse, I burned 4 of them ! Be carefull !
- LM317 voltage regulator
- 400 ohm resitor
- 240 ohm resitor
- 1µF polarised capacitor
- 0.1µF polarised capacitor
- photo resitor + resistor (to make a voltage devider). The reflecte signal from the transmitter to the sensor was to “dirty”, so no O/I signal was given. However the sensor has a green and red led on top of it. Normally if the signal is O, the red light turns on, otherwise the green one. But on a “dirty” signal, only the geen led turns of. The photo resistor monitors the green light. The voltage devider gives a signal to the GPIO on the ESP8266. Simple and effective.
- ESP8266 wifi controller, NodeNCU installed. If you’re new to the ESP8266 > start digging the internet 😉
- An Arduino. Used to upload the Lua script to the ESP8266. The Arduino is not actually part of the final setup.
- 10V DC power supply (the sensor rates up to 24V)
- Ductape : every good project needs Ductape ! I had to use it to attatch the sensor to the electricity meter box.
- soldering stuff, connectors, some wires, stripboard, …
How to make it :
- Test the electronical circuit with a breadboard. Finding the right sensor to fit my electricity meter was quite an adventure.
- Programm the ESP8266 (see file init.lua below, open with notepad)
- Solder the components to the stripboard
- connect the sensor to your electricity meter.
Note : the sensor will work and send data, but of course you need a database to store the data (see next steps)
Step 3: Raspberry Pi + Apache + MySql
I was already running Apache and MySql on my laptop for web development use. But having my laptop run full time for an energy saving project was not very smart. The Raspberry Pi is a stand alone mini computer. It does not need a screen and consumes a maximum about 7W. The dimensions of the Pi might be small, compared to my very first computer it preforms about 10^12 times better. I was so impressed by it’s preformance and possibilities that my kids bought me one for my birthday (maybe I pushed them a bit in the right direction).
Anyway, setup the Pi with Wifi, load Apache and MySql, create a database and start recording.
There are lots of tutorial on the internet on how to setup the Raspberry Pi with an Apache webserver and MySql.
What it does :
The MySql Datebase stores date in 2 very simple tables. 1 table for the electricity meter data, the other for de wind energy forcast.
The electricity meter :
- after every full turn of the wheel in the electricity meter, the ESP8266 connects to the server and calls the page electriciteitsmeter.php.
- the php program uploads the data in the table ElectMeterMeter in the SensiHome database
Simple as that 🙂
XML from the Elia server (Belgium’s National Electricity Grid Manager) :
The Pi calls the page WindForcastXML.php every 5 minutes with a cronjob
The XML and the Elia server are very specific for the data I need. Check your local Grid Managers website for the data you need (for example forcasts about solar electricity)
For more detail: ESP8266 + Raspberry Pi Electricity Monitor