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
- windforcast
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.
Windforcast (WindFor.php):
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
Home Automation
13 Comments
Question 5 years ago
Hi, could you please tell me the value of the resistor and the photo resistor for the voltage divider?
Thank you very much
7 years ago
Hi,
Could you tell me how i could do this for a digital meter.I think my meter has a pulse output.
Reply 7 years ago
Voila, you answerd the question :)
If the pulse outlet is an LED blinking for every X Watt, you just put a little photoresitor over a voltage devider on it going to the GPIO pin. Then write a program that handles the pulse. When ON, you 'arm' the counter, when OFF, you fire a sequence. Be aware of a kind of bouncing effect when the light turns off. The value will not allways be 0 straight away, but more ripple out. So build in a little delay in your program (20-50ms, anyway shorter than a pulse).
Reply 7 years ago
This is great.Will try it out and see how it goes.Thanks alot :)
7 years ago
Hi, could you put the php files in zip or a txt file.
I've tried different ways but you can't download php files here.
The project is interesting but still would be interesting to test it.
regards
7 years ago
Hi,
I really like your way to catch the rotation of the 'power' disc.
But in my case, i got solar panel, so , sometime the disc is going backward....
Any idea ???
Great project.
Reply 7 years ago
hi
Maybe you can use 2 sensors reading the disk to know which direction it is running (though no actual reading can be done this way) ? Or is there a way to measure the production of the solar panel ? Or use an induction clip on the main power line between the meter and the main switchboard ?
Reply 7 years ago
Hi,
Very interesting problem :-). Depends a bit on what your goal is : just monitor the meter, of read acctual consumption or production.
Let's asume you want to do the second option (most difficult). As far as I understand solar panel systems and their connection to the electricity net I have some ideas. You have 2 producers (the net and your panels) and one consumer (your house). The net meter runs forwards and backwards and is the direct result of your acctual consumption minus your panel production. So with a lightbulb of 100W and a production of 80W on the panels the meter will run forward at a rate of 20W. Switch of the light and the meter will run backward at a rate of 80W. So you need to measure your 2 systems.
Asuming you have a 'disc meter', just take the basic design of my project but with 2 IR sensors. Depending on which sensors triggers first, you know which direction it is running. You will have to use a ESP8266-12 because you need more pins.
For your solar panel you have to find out a way to monitor the system. Is there some way to hack into it ? Or does it puls an LED or so for each x Wh ? If it gives a puls for a rahter small current, you can get a relative accurate acctual production. If not, you can use a current sensor (in circuit or inductive). It will be more expensive this way but very real time.
Collect the data on a small server (for example a Raspberry Pi) and process the data of both systems to calculate to the accctual consumption and production.
If you are not interested in the acctual production of the panels, just hook up a current sensor (in circuit of inductive) on the incoming wire(s) just before the main fuse box.
inductive current sensor : https://www.sparkfun.com/products/11005
in circuit : https://www.instructables.com/id/How-to-Measure-AC...
Hope some of my ideas may help. Good luck.
7 years ago
Hi,
Can you put all the PHP files into a zip file?
We cannot download a PHP file extentions from here
a nice tutorial
8 years ago
really cool
8 years ago on Introduction
Hello! pls update php files in zip, it dont downloadable.
8 years ago on Introduction
That's a good project. I'm trying to make something similar myself, but on the smaller scale (smart surge protector with power monitoring for my PC).
There's one thing you can use to make power measurements more elegant: there are very cheap Hall-effect current sensors based on ASC712 chip. Depending on the model, they provide fairly good range for current readings (IC itself lets you measure up to 31A in both directions). Combine that with an ATTiny and you have an independent digital power meter!
I haven't had a chance to poke around these ESP8266 modules yet, but from what I've read there might be a way to use its computing capabilities to poll weather forecast service and parse JSON data directly, circumventing the RPi middleman.
8 years ago on Introduction
Nice work ! Can you lplease provide the files as ZIP, on MAC there is an issue downloading php files...