Sunday, 31 July 2016

Near real-time remote monitoring of home electric consumption with a Raspberry Pi

Just out of curiosity, I wanted to get a better and more precise idea of the amount of electricity I use at home every day. Which are the periods of the day I'm using a lot? Are there any particularly power-hungry appliances?

Just for the fun of it, I wanted a system as real-time as possible, that makes it easy to access the stored data, and while we're at it, that lets me do so remotely!

So I came up with this Raspberry Pi based setup:

The first thing was to measure the consumption at the electric meter. A safe way was to simply detect the flashes that my meter emits for each watt hour consumed.

I found a nice article explaining how to read the value of a photoresistor connected to the Raspberry Pi GPIO in Python. After logging these values and analyzing them, I came up with a little function that identifies the sudden changes in value that correspond to a flash.

To make sure it was working decently, I connected a LED to the Pi and flashed it when I detected a flash on the electric meter (that's the green one, the blue one is the WiFi dongle :) )

The next step was simply to count these flashes by periods of say 5 seconds and send the results to a Google Spreadsheet using the gspread Python library.

It turns out that modifying the spreadsheet isn't super quick (a couple of seconds) so I found that doing that every 5 seconds was a good compromise for my totally overkill real-time requirement.

Once everything was in place, I did some more tests like this one:

And then I let the system run for a few days...

Here is a 3 day graph where I collapsed the data points into watt hours per minute. Working with per-5-second data was too heavy for the grapher (that'd be 17280 points per day!). I also discovered that a Google Spreadsheet has limits on the number of cells in a document (400 000, I think), how disappointing :) !

I tried to keep notes of what I was doing during these 3 days. Here my attempt at explaining the different bits:

The Python code is available on github. It's pretty rough around the edges but maybe it can give people some ideas!