søndag den 21. april 2013

Super cheap web-enabled power measurement solution for your house or apartment

I was looking around the net to find a cheap device to measure my energy consumption in my apartment and make the results readable from my smartphone and computer. I found some solutions and figured out that they can be quite costly, so I built one myself instead for under a dollar plus a Raspberry Pi. In this post I'm going to show how you can make one yourself.




How it works in short

In 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 parts

1.  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)


4.  1uF capacitor (not a Flux Capacitor - we're not building a time-machine today)
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.

1uF capacitor



Let's get connected

I 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.

  1. 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. 
  2. 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.
  3. 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. 
  4. 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.
https://github.com/comzone/cheappowermeter
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:
$ /var/www/cheappowermeter-Python/./debug


PS: The code is available at GitHub please contribute.
https://github.com/comzone/cheappowermeter

Happy Geekend!



Handy links:
http://learn.adafruit.com/basic-resistor-sensor-reading-on-raspberry-pi/basic-photocell-reading
http://raspberrypi-projects.com/how-to-connect-a-photocell-to-a-raspberry-pi-6/


This blog post has been proofread by a Technical Writer before being published.

onsdag den 27. februar 2013

Why not have a 2.5" TFT display attached directly to your Raspberry Pi?

This post describes how I added a 2.5" colour display to my Raspberry Pi. The benefits are that the display is mounted directly onto the cabinet, which makes it looks pretty cool, and all cables are inside the box. Another cool feature is that the Raspberry Pi provides the power for the display so no extra power source is needed.


This is how it looks when the machine boots with Rasbian, which is a modified version of Debian linux


And a Raspbmc Boot




First of all you need some parts

1.  Raspberry Pi (SD-card, Power adaptor), which can be purchased from Farnell (Delivery 1-2 days)


The Pi in all its glory


2.  A shell. I recommend this Stealth Black Case for Raspberry Pi from RaspberryBits as I know the display controller fits without touching the other components. A black box is also recommended because the display leaks some back-light, which would make a white or transparent case light up.



Stealth Black case - good at hiding light and adding a touch of ninja to the weekend


3.  A display which works with Composite video. I used the NTSC/PAL (Television) TFT Display - 2.5" from Adafruit (A025DL02 TFT Display). They exist in different sizes but 2.5" fits the box quite well.


The screen and controller-board from Adafruit


Optional: 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


Optional: You might also need a little RCA coupler, Male-to-Male for the prototype / testing, also available through Adafruit. An external power supply, 4.5-15VDC or a 9 voltage battery for the prototype / testing, would also be a good addition.

You also need soldering equipment so you can solder it all together.


Let's get started

First thing I did was to make a prototype without soldering, in case it for some reason didn't work properly.

1. Connect the display and the Raspberry Pi with the RCA coupler. The connectors should fit together so no modification is needed yet.

2.  Connect the power supply or the battery to the black and red power wire for the display. Ensure to connect (+) to the red wire and (-) to the black, otherwise you will damage your display.

3.  Then power on the Raspberry Pi with a pre-installed OS. It could be Raspbian or Raspbmc or another OS for your Raspberry Pi.

4.  I mounted the display on top of the case simply with some double-sided tape on the back side. Be careful when you remove it again as you could damage the components on the screen's controller-board.


This is how my prototype looked


Let's build the real thing

1.  First I de-mounted the screen from the screen controller board - Be careful, it can break quite easily.

2.  Next I put the display where I wanted it to be (right bottom corner of the top of the case),  to see where the flat screen cable should go through.

3.  Then I sawed, with a small hacksaw, a 2 cm long crack from the bottom right corner and up the shorter side, (see red line in the image below).


2cm long crack for the flat cable to pass through (red line)


4.  I then tried to temporarily put it together to see that is fits and that the flat cable could still go through the crack and into the screen-controller board.

5.  After that I glued the screen onto the box. Ensure the screen is flush with the case and the right and bottom sides. Fast drying glue is recommended.  Don't use tools to keep it together as you could damage the screen.

6.  After the glue dried I connected the screen to the screen controller board again with the flat cable.

7.  After which I glued the screen controller to the inside, as shown in this picture, below.


Screen controller glued to the inside of the shell


8.  To protect the Raspberry Pi and the screen controller from short-circuiting each other, I glued a little piece of plastic on the the screen controller board. Remember to only glue on the plastic parts and not the components!

*It's a good idea to check if it's still working throughout the process, so you know where, in the process, you might have done something wrong.


Here it is, connected up again


9.  The screen and the screen controller are now mounted and it's time to lose the external cables.

     9a.  First of all, cut off the Composite plug from the white and the yellow cable, then put them through the hole on the Raspberry Pi print-card.

     9b.  Next, solder the cable to the underside of the yellow Composite plug on the backside of the Raspberry Pi, as shown below. It's very important that you do it exactly as shown in the picture, as faulty soldering could damage both boards. It's always a good idea to put some solder on the exposed cable before heating, as this will make it easier to solder, because two objects with solder on them are easier to solder together.


The cable to the underside of the yellow Composite plug on the backside of the Raspberry Pi


10.  Now, when the video cable is connected I recommend that you try to power-on the two boards, just to see if it still works.

11.  It's now time for connecting the power wire for the screen board controller to the GIPO interface on the Raspberry Pi. The screen needs 4.5 volts, and that is exactly what we can get from pin 2 (+) and pin 6 (-) on the GIPO.

12.  I took two Jumper Wires and cut them in the middle. Then crimped the plastic in the middle with a pair of pliers, so as to bend it into an "L" shape, (this way it won't protrude too much and take up too much space).


Jumper Wire cut in the middle


13.  You then have to solder the two Jumper Wires to the two power cables from the screen controller board. Ensure that the colour fits, you wouldn't want to hook the power up wrongly, as that will damage the boards.

14.  After soldering, put some tape around the exposed solder to ensure that it won't touch any other electric parts.


Pin guide for the Raspberry Pi



It should like like this.


15.  Now it's time to finally put it all together! Fit all of the cables in carefully and don't use too much force when you close the box.


It's time for a test!

Connect the mouse, keyboard and network cable (if you need it), and then connect the power. You should see something like this:


Jubiii, this is what success looks like!


Happy Geekend!


This blog post has been proofread by a Technical Writer before being published.