Tuesday, May 14, 2013

19. Back to the Raspberry Pi - WebIOPi

You might remember in Post 1 that this all started because of the Raspberry Pi.  I then got a Raspberry Pi extension board, the Gertboard, which happens to have an Arduino chip, which can be programmed using the Arduino API on the RasPi.  Having gone off on a tangent following electronics combined with the Arduino, I then concentrated on just breadboard projects.

Now it's time to get back to the Raspberry Pi.  A couple of MagPi editions ago (http://issuu.com/themagpi/docs/issue_9_final?mode=window) I came across WebIOPi, by Eric Ptak.  This uses the web, to control Input/Output pins on the Pi.  A truly amazing piece of technology!  Just imagine that you can have a web page on any PC or mobile device - anywhere - and by clicking on the web page buttons, you can control things connected to your remotely-situated RasPi.

All kinds of applications come to mind - from controlling the security or heating systems in your home, over the internet, to operating robots remotely with your mobile phone.

The Pi becomes a web server, and that part of the coding is written in Python, while the client (webpage) is written in HTML and JavaScript.

The first thing you need to do is find out what your Pi's IP address is (NB this changes periodically), so on the RasPi terminal, you need to do a:

sudo ifconfig  (your Pi has to be on-line of course)

to find out what your particular Pi's address is.  The Pi's terminal pours out a lot of stuff, but in the wlan0 section, it should list inet addr: 192.168.1.68   My particular address is a Dynamic IP (internet protocol) address, as opposed to a Static IP address, meaning that it will change each time my router is restarted:

My Pi is running Raspbian Wheezy available for free download from http://www.raspberrypi.org/downloads   At the time of writing this post, the latest version is 2013-02-09 ie 9-Feb-13.  You need to keep an eye on this download page to update to the latest version whenever one is released.

To freshen up my version, I did a: 

sudo apt-get update && sudo apt-get install raspi-config libraspberrypi* 

and it does no harm to do a: 

sudo apt-get upgrade

before going any further.  Then I followed the instructions to install and use the standard web page - and it came up in my browser (on my desktop and on my iPod Touch):

I wired pins P0, P1 and P4 on my Gertboard (remember I keep the Gertboard connected to my Pi all the time) to a mini breadboard with an RGB LED (through suitable resistors).  Don't forget to run a GND from the Gertboard to the common cathode (in my case) of the RGB LED.  These 3 pins correspond to RasPi pins GPIO 0, GPIO 1 and GPIO 4, which are illustrated on the web page, whose address is http://192.168.1.68:8000/webiopi/




If you click on the grey buttons, you can change the function of that pin to OUT (output) or IN (input).  You can see from the image above, that all the GPIO pins are initially set to IN.  The GPIO pins are shown by rectangles with respective numbers in them, and are either black (inactive) or orange (active) to indicate whether they are in high state or low state.

My GPIO 8, GPIO 9, GPIO 10 and GPIO 11 are still connected to the Gertboard, and are used for programming the ATMEGA 328).  Be careful with GPIO pin numbers - the latest version of the Pi is different!  As I'm using GPIO 0, 1 and 4, I clicked on the grey buttons beside these to make them outputs:

and when I clicked on the 3 to make it orange, the LED on my desktop beside my Pi lit up red, when I clicked to make 5 only orange, the blue element of the RGB LED lit up, and with 7 only orange, green lit up.  You can make any combination of these 3 buttons active, and get any one of the primary colours, or with any two active, the secondary colours cyan, magenta and yellow, or with all 3 active, white.

I thought this was really cool - being able to control the colour of my LED from anywhere in the world (providing you have port forwarding on your router).  Even Col Chris Hadfield on the International Space Station (he's just landed safely in Kazakhstan today) could control my LED, and therefore any other Raspberry Pi circuit, for example, motors, servos, or any other devices that spring into life when they get an electrical pulse!



I went on to fiddle about with some Javascript and made my own page:


In this case, the LED is glowing Red.

And here's my Pi with the RGB LED's red and blue active, making a bright magenta:


Magic!!



No comments:

Post a Comment