How it works in shortIn the hall of our apartment there's a fusebox and a little box which has a little LED which blinks each time 1 watt hour has been used. A record is saved to the database every time there's a blink and the statistics are then published on a little website which is running from the Raspberry Pi.
This is what the statistics and the graph look like on my smart phone.
This is how the installation itself looks like.
First of all you need some parts1. A Raspberry Pi with an SD-card and power adaptor, which can be purchased from Farnell (Delivery 1-2 days). Optional: You can also buy a WiFi dongle and a shell with it.
A slice of Pi :) (version B)
2. If you don't want to solder directly on the Raspberry Pi's GPIO interface, I would recommend buying some Female-to-Female Jumper Wires.
Lesbian Jumper Wires
3. Light-dependent resistor
Photocells are sensors that detects light. They are small, inexpensive, low-power, easy to use. They are often called CdS cells or photoresistors.
Light-dependant resistor (CdS cell / photoresistor)
This component works like a very precise battery, which is good to convert voltages to time as we know how long it will take to discharge.
Let's get connectedI use coloured Jumper Wires to connect it all up as it's easier to hook up and I can refer to the Jumper Wire by colour in these instructions.
- Connect a red Jumper Wire to the 3.3 VDC power (P1, see below) on the Raspberry Pi and the other end to the Light-dependent resistor.
- Connect a green Jumper Wire to the other leg on the Light-dependent resistor and to the plus (+) side (long leg) of the 1uF capacitor.
- Connect a blue Jumper Wire from the same (long leg, (+ side)) of the 1uF capacitor to the GPIO (P12) on the Raspberry Pi. Connecting two Jumper Wires to one leg of a component can be done by jumping a 1cm long leg from another component or by making a make-shift Jumper Wire.
- Connect a black Jumper Wire from the minus (-) side (short leg) of the 1uF capacitor to the Ground (P6) on the Raspberry Pi. Use some tape to cover exposed parts around the capacitor
The GPIO interface for pin placement on the Raspberry Pi (eg P1)
It should now look like this.
Before you power on the Raspberry Pi, check everything is connected in the right way, otherwise you might damage your Pi.
5. Place the Light-dependent resistor so it faces towards the blinking LED on the power meter.
6. Cover the LED and Light-dependent resistor with black tape or simply make the tape black so light from the surrounding environment wouldn't interfere with the system.
If it doesn't kinda look like this when it's all hooked up then you've done it wrong son!
7. Connect the Raspberry Pi to your local network with a wireless USB dongle or simply with a network cable.
That was the hardware part, now it's time for a cuppa!
The software!1. The solution can run on any Linux distribution but some specific modules are needed, so it's easier just to install the Adafruit Raspberry Pi Educational Linux-distro
2. When the operating system is installed into the SD-card, put it in the Raspberry Pi, connect the screen and keyboard then turn it on. Run $ sudo raspi-config to change timezone, change locale, enable ssh, change password, and expand the filesystem.
Then log in to your Raspberry Pi through SSH and install the required modules and applications:
$ sudo apt-get install python-dev
$ sudo apt-get install python-setuptools
$ sudo easy_install rpi.gpio
$ sudo apt-get install mysql-server-5.5
$ sudo apt-get install python-mysqldb
$ sudo apt-get install phpmyadmin
$ sudo apt-get install apache2
$ sudo apt-get install nano
3. Then download and install the Cheappowermeter application
$ wget https://www.dropbox.com/s/xcb2ntrt2d3xlkd/Cheappowermeter.tar.gz
$ tar -xvvzf Cheappowermeter.tar.gz
$ sudo cp Cheappowermeter/www/* /var/www -R
$ sudo rm /var/www/index.html
PS: The code is available at GitHub please contribute.
If you want to see if the light-sensor is working then you can run:
$ sudo /var/www/cheappowermeter-Python/./debug
This script outputs the light values and is very handy if you need to know the light values for calibration later on.
4. Now you have to set up the database: Open a web-browser and go to your Raspberry Pi's MySQL interface (http://<the ip address>/phpmyadmin) or use $ mysql direct from the console.
If you don't know the IP-address simply run $ ifconfig | grep inet
5. Run the following SQL sentences to add a database, table and a user.
Remember to change "yourpasswordhere" to your own password.
CREATE DATABASE measurepower;
CREATE USER 'measurepower'@'localhost' IDENTIFIED BY 'yourpasswordhere';
GRANT ALL ON measurepower.* TO 'measurepower'@'%';
Log off and log on again with your new username measurepower and your new password and do:
CREATE TABLE IF NOT EXISTS `watthours` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`datetime` datetime NOT NULL,
`lightvalue` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=0 ;
6. We now have to change the passwords in the application so it fits the ones you've just made. Run this in your console:
$ sudo nano /var/www/cheappowermeter-Python/readlight
Then find this line and change the password.
con = mdb.connect('localhost', 'measurepower', 'yourpasswordhere', 'measurepower');
Next, run this in your console: $ sudo nano /var/www/index.php
Then find this line and change the password:
$link = mysql_connect('localhost', 'measurepower', 'yourpasswordhere');
7. The last thing to do is to ensure that the logging application starts when the Raspberry Pi starts:
Run this in your console: $ sudo nano /etc/rc.local
Then add this line as the 2nd last line: /var/www/cheappowermeter-Python/./readlight &
so the line will end with exit 0
Your are done!Go to http://<the ip address> to see the result!
This is how your power consumption statistics look like in a browser
If the logger saves too many or too few watt hours, you might try to adjust the light-value:
$ nano /var/www/cheappowermeter-Python/readlight
Look for this line: if result<=150:
You can read the light-value by running:
PS: The code is available at GitHub please contribute.
This blog post has been proofread by a Technical Writer before being published.