Latest Work...

Harry Potter / Weasley Clock Part I - Internet Connected LED

The RPi GPIO Python Libraries provide a good way of accessing the GPIO pins on the Raspberry Pi in Python. There are other ways such as the wiringpi project which allows you to program the Pi in a similar way to an Arduino but I chose this one because it was the first and simplist way I found of controling access to the pins, here's a simple python script for doing so:

#! /usr/bin/python
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.OUT)

GPIO.output(7,GPIO.HIGH)

In order to access the GPIO pins on the RPi, this script must be run as root (sudo python myscript.py). 

wvdial

Although the Raspberry Pi has the drivers already for a huge number of USB mobile broadband modems - the E160 included, Linux actually needs to know what to do with it! That is, what to dial, what information to send, and how to establish a connection. A great utility for doing nearly all of this is wvdial. This, when used with the pppd, will establish a working GPRS connection as a network interface. When the Pi first came out - getting this working was a bit of a nightmare, however now things have vastly improved. I'll post some information about how I got it all connected shortly - but there are some excellent tutorials on this a quick Google search away!

The above script will do nothing other than light up the LED as connected in the configuration shown in the image above. If we'd like to control it remotely, there are two options, either someone of something pushes a command to the RPi, or the RPi pulls this comand from somwhere.

Push Commands

This would certainly be the most efficient way to control the LED. All we'd need to do would be to set up a server on the RPi and have it wait for something to connect and tell it what to do. Unfortunately, that last bit was the hard part with the current set-up. Whilst the USB modem reported an IP address to the Raspberry Pi, it turns out that it may not be the only thing with that address. Some research confirms that phone networks do tend to use NAT (similar to that on a home modem - except you can't configure it!) to share out one address between multiple devices. Another way could be to have the RPi make a connection to a server somewhere and that server keep the socket open, but I didn't want to set up a dedicated VPS somewhere in order to do this (my webhost would, understandably, not support this). 

I also originally planned on having the Pi listen out for SSH connections and setting up a dynamic dns daemon on it so I could connect to it remotely. Unfortunately the situation with NAT rules this out too, which is a pain.

Pull Commands:

This is the method I ended up using for all of the communications with the clock. The idea is that every minute, the RPi polls a web server for a file, and depending on the contents of said file, the light is turned off or on. This is simple as it only requires a few lines of Python code, however has the downside of requiring additional traffic which will mean more expense from the point of view of network traffic and data usage. The file on the webserver could be a php script or at its simplest a plain text file. Here is how I did it for the LED test:

#!/usr/bin/python

import RPi.GPIO as GPIO
import urllib2, time
PIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.OUT)

timenow = str(time.clock())
urlstring = "http://www.a-barber.com/lightserver/light.txt?rand="+timenow
response = urllib2.urlopen(urlstring)
command = response.read()

print command

if 'ON' in command:


        GPIO.output(7,GPIO.HIGH)
else:
        GPIO.output(7,GPIO.LOW)

Here you'll notice that I've appended "?rand=+timenow" to the URL. This is becuase I use a caching service on my website, so this will mean the data returned will be constant. The timestamp forces the file to actually be read from disk every time it's called. Because of the relative expense of mobile data, a lot of networks will also cache and compress data before sending it to you. This is worth bearing in mind should you use this method in any projects.

In order to get this to run every minute, we can simply use the Cron utility in Unix/Linux to schedule this script to be run every minute. This is very easy, simply sudo crontab -e in order to edit the cron file (sudo as this job needs to run as root) and add the following line

*/1 * * * *  python /path/to/file/myScript.py

where myScript.py is the name of the python script.

So, now we can light an LED up depending on what we read from a webserver, we need to figure a way out how to move a hand depending on what's read, which is somewhat more complicated!

Next - Making stuff move

 

comments powered by Disqus

<< Go back to the previous page