søndag den 9. februar 2014

Virtual servers on a Raspberry Pi with the light weight OS virtualization system Docker! Updated!

Virtual servers on a Raspberry Pi with the light weight OS virtualization system, Docker!


I find my self reinstalling the OS on my Raspberry pi all the time because I use my Pi to develop software, and install a lot of software packages, all the time. Sometimes you do experiments where you just can't go back and sometimes you need a clean slate for trying something out. Therefore I was looking for a way to install software, try it out and discard it, in a way that's fast and easy. I also want to isolate my installations sometimes, and sometimes run more than one copy of one piece of software which of course requires some virtual machines.

The solution is Docker!

Docker is a light weight OS virtualization system which makes it possible to run  more than one OS on the same hardware and makes it extremely easy to install a new OS, use it and discard it. Another cool feature is that you can download, install, and start an OS with preinstalled software like Owncloud with just one command! Docker also makes your installations portable so you can easily move or clone your installations.


Let's install Docker on your Raspberry Pi!

This is what you need:
  • A Raspberry Pi, of course! 
  • A keyboard
  • Network connection
  • A screen
  • and a fast SD-card, mine can transfer 60 M/s which is recommended.

Install Arch Linux on the SD-card


We will now install Arch Linux on your SD-card.

If you are preparing your SD-card on a Win or Mac machine, the instructions for installing Arch Linux on the SD-card can be found here:

http://archlinuxarm.org/platforms/armv6/raspberry-pi

1.  Download the disk images:

$ wget http://archlinuxarm.org/os/ArchLinuxARM-rpi-latest.zip

2.  Unpack ArchLinuxARM-rpi-latest.zip

$ unzip ArchLinuxARM-rpi-latest.zip

3.  Find out the path for you SD-card. At my system it's /dev/sdb
Note: You can use Gparted to find the path. We will also use this application later on in this tutorial.  

$ sudo dd bs=1M if=ArchLinuxARM-2014.06-rpi.img of=/dev/sdb

Be patient, it will take 10+ minutes and there will be no sign of life while it's going on.
When it's done it will write a few statistics, which also tells you that it's done.

1960837120 byte (2,0 GB) kopieret, 338,551 s, 5,8 MB/s

4.  Run this command to ensure that everything have been written to the SD-card:

$ sudo sync

By default, the partition doesn’t fill the entire SD-card which means that you can't use all the space. Therefore we will extend the partitions to make more space for our data.

5.  Start Gparted:

$ sudo gparted

Install if necessary...

  1. Choose the right storage in the top right menu. Mine is /dev/sdb.
  2. Right click on the partition /dev/sdb2 (extented) and choose modify/move. 
  3. Move the black arrow pointing right over to the right and press modify/move.
  4. Do the same with /dev/sdb5 (ext4).
  5. Press the green okay icon on the top to apply all the changes.
  6. After a little while it all done and you can close the application at take out the SD-card.

Let's get the power on!


Now it's time to start your installation for the first time!
  • Pop the SD-card in the the Pi. 
  • Connect keyboard, network-cable and the HDMI-cable. Note:When everything is connected you connect the power.

1.  At this time we don't know the IP-address of the Raspberry Pi so we will log on locally. Log on with the user root and the password root.

Run this command to find the IP of your Pi:

$ ifconfig

Sometime it's easier to look the IP op through your routers webinterface.
Now when we know the IP we logout by type exit and press [Enter] And use our laptop instead.

2.  Start and terminal and connect to the Pi:

$ ssh root@192.168.1.123

Password is still root! It's a good idea as you might want to expose your Raspberry Pi to the internet at some point.

3.  Change the password by running:

$ passwd root

Write down the password.. Maybe on the back of the Pi ;-) If you're a numpty and forget to do this, then at some point you'll probably forget it. Then you should check out this post on Happy Geekend for changing a password if you've completely forgotten it :)

And then the installation of Docker


1.  Download and install Kernel modules and Docker by:

$ curl https://raw.github.com/resin-io/docker-install-script/master/install.sh | sh

2.  We want each of the containers (the virtual machines) to have access to the network and internet and therefore we have to enable IP forwardingby running this command:

$ echo 'net.ipv4.ip_forward=1' >> /etc/sysctl.d/40-ip-forward.conf


3. To ensure that Docker will start automatically when the Raspberry Pi is turned on:

$ systemctl enable docker.service

4.  The system has to be rebooted before we can use it, as the kernel has been modified and these modifications will only be loaded doing boot.

Run:

$ systemctl reboot


It's time for testing!

 

1.  If, for example, you want an Owncloud installation (it's like Dropbox), up and running then connect through ssh as we did before, and run:

docker run -d -i -t -p 443:443 -p 80:80 comzone/rpi-owncloud6

2.  When it's up and running, open you webbrowser and go to the IP of your Raspberry pi!



To find many other ready to run installations, search for RPI at: https://index.docker.io/


Only installations which are made for the Raspberry Pi will run!

Everything with docker is managed with the Docker command.

Here are some hints:

  • To see all of your containers and their IDs:

$ docker ps
  • To attach to a container run:

$ docker attach ID

You have to replace the ID with the id number of the container you cant to manage.

  • To delete an container:

$ docker stop ID
$ docker rm ID


Tuning

You might want to consider tuning your Raspberry Pi so your CPU runs faster.
Be aware the some SD-cards can become corupted when the Raspberry Pi is tuned. If that happens then you have to reinstall the SD-card again.

It's easy to do!
1.  Install an editor, I prefer Nano:

$ pacman -S nano

2.  Edit the configurationfile for the Pi:

$ nano /boot/config.txt

3.  Uncomment the configuration-group you like. 1Gz is a good speed:


arm_freq=1000
core_freq=500
sdram_freq=500
over_voltage=6

4.  Press [CTRL]+x to save and exit.

The change will take place after next reboot so run:

$ sudo systemctl reboot


Happy Geekend!

Handy links:
http://resin.io/blog/docker-on-raspberry-pi-in-4-simple-steps/
http://docker.io
http://archlinuxarm.org/platforms/armv6/raspberry-pi
http://happygeekend.blogspot.dk

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.