Introduction: 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
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)
Step 4: In the Browser
In your browser you can connect to 4 pages :
- actual consumpion of electricity
- selection page on which date you want more detailed info
- daily consumption, per hour
actual consumption (meter.php + meterstand.php)
the meterstand.php program calls 2 SQL commands in the ElectMeterMeter table in the SenisHome database on the Raspberry Pi.
- the latest entry in the table, which is the acctual consumption
- the total number of spins of the wheel, multiplied by 375 (375 spins = 1 kWh)
The meter.php acctualy runs an ajax command to refresh. The main advantage of this method is you don't realy refresh the page, but only the data. So the meter.php page gets it's data from meterstand.php and updates every 5 seconds.
note : the data is updated every 5 seconds, but this does not mean there is new data ! In my case, 1 kWh = 375 spins. The sensor only fires a message to the database after a full turn. So on high consumption, there is new data in less than 5 seconds, however on low consumption it can take several minutes before an update.
With the 2 buttons on the bottom you can go the windforcast page or the daily consumption
Selection page + daily consumption(datepicker.php + dagverbruik.php)
First you get the selection page, where you pick a date for the more detailed info you want to see.
From there you are redirected to a Google Chart, with the consumption per hour.
This is the windforcast + actual production. It is a 3 day forcast, in a simple Google Chart. The lightblue line is the forcasted production, the darkblue is the actual/hystoric production.
(open the files in a notepad program)
Participated in the