Power measurement - technical details

I had a few requests for a little more technical detail around this project. For now I will post only a few photos, but will extend it with circuit diagrams and source code in a little while.

The hardware

The light sensor stuck to the front of the prepaid electricity meter covering the meter's flashing LED:
sensor

The Arduino with ethernet shield counting the flashes and uploading the count every minute to a computer in my house. The Arduino is currently powered by USB from an old netbook, but this will be changed as soon as I acquire the hardware to build a PoE extracter.
arduino

To sense when the LED flashes, I initially wanted to use a light dependant resistor (LDR). Through experimentation it was seen that this would work, but will be very sensitive to other ambient light sources. A friend told me that he thinks a normal LED would work too. Experimenting with a normal red LED did not work at all. However I had some clear plastic LED's in my toolbox. I tried a random one and miraculously it did work. Even better than the LDR. I think it is a UV LED. The theory behind the use of an LED to sense light is that a photon falling in on the semiconductor substrate causes a small current to flow. This consequently causes a voltage drop at the junction between the LED and the resistor. This is also the point at which I measure with my analog-to-digital converter (ADC). In the schematic below note the polarity of the LED. Through experimentation it was seen that the setup works with the LED connected both ways around, but the voltage drop is much more noticable when it is connected as depicted in the diagram (wrong way around for turning the LED on).

voltage dividerFirmware
The Arduino libraries make everything a breeze. One thing it however does not support is interrupts. So one needs to enable them by hand.

Through tests I saw that if I sample the ADC every 10ms, I will get three samples while the LED is on. As 10ms is the time between ADC samples in the Arduino examples, I also use this value. I set a timer to cause an interrupt every 10ms, check if the LED state changed, and if it did change from off to on, increment a counter.

In the main loop, I check the amount of time that passed since the Arduino started up. If this is equal to 60 seconds, or the difference between the last data upload time and now is 60 second, I upload the flash count.

The code also contains a few error checking conditions.

Communication

As the most network libraries support the download of websites, this is normally the communication method I use. When fetching a website one specifies the url followed by a few parameters.
For example: www.myserver.com/page_name?paramter1=value1&parameter2=value2

So to fetch a website, you actually send data to the web server. Some people might call this a RESTful API. I just call it URL parameters.

The Arduino libraries supports this and handling the parameters and values in PHP on the server's side is trivial.

So to summarise. The Arduino "visits" a web page on a computer in my house, giving the flash count as an URL parameter. On the computer I extract this parameter/value from the URL, convert the flash count to Watts and then save the data with a time stamp in a MySQL database.

Database and statistics

The database only has one table containing the time stamp and electricity usage at that time in Watt. It is actually an average usage of the minute before that time, but minutely averages are accurate enough for my use.

Using the jpgraph PHP library I select the last 24 hours of data and plot it on a graph. I do the same for 7 days too. These two graphs are then displayed on a webpage that refreshes every 10 minutes.

Also displayed on this page I have the total amount of electricity units I used per day in the past week (summarise the wattage for the day and then divide by 60), and also the average amount of units I used per day.

Future plans

Future plans include computing how much the electricity should have cost me every month. I also want to compare my electricity usage between days I leave my hot water cylinder on for the entire day, versus days I only have it on for 1.5 hours in the morning.

Building a meter I can plug in between my computer and the wall socket will be nice to log my computer's electricity usage.

Miniaturising and simplifying the hardware I use at the moment will reduce cost and enable wider application. A future version might also be Wifi-enabled.

AttachmentSize
Binary Data Arduino Source Code4.39 KB