At the Stevenage Raspberry Jam in October, I built and demonstrated a simple method of connecting hardware over the network, using two Raspberry Pi computers.
What is “The Internet of Things”
The “Internet of Things” is the next wave of the internet. Basically, if any device can connect to the internet, it can take part in a worldwide network of sensors and output devices. Using this Internet of Things, it will be possible for tiny devices to be connected together into much larger systems. In February 2011, Ericsson predicted there will be 50 billion connected devices by 2020.
Basically, any tiny connected device can produce data that is uploaded to the internet. This data can then be analysed on large servers, presented on mobile apps, or even used to send messages to other internet connected devices that make things happen in the real world.
For a full coverage of some of the possibilities, please listen to Peter Day’s BBC Radio4 broadcast on the subject, linked in the resources section.
My demonstrator is very simple. It uses two Raspberry Pi computers. The first monitors a switch, and the second controls an LED. Both are connected via a local area network (but they could just as well be connected over the internet at opposite sides of the planet). Close the switch and the LED comes on. Open the switch and the LED goes off.
You can see in my picture that there are 4 post-it notes. The idea here is that people write on the post-it notes what they want their invention to do – what does the switch represent, what local processing or decision making is done near to the switch, what processing is done at the other end, and what does the LED represent.
For example, in a tele-health application, the switch could represent the monitoring of a patient heartbeat. The local processing could represent checking of the heartbeat to see if it is too fast or too slow. The remote processing could represent looking up a patient record and comparing heartbeats against previous records, and the LED could represent an alarm popping up on a doctors mobile phone saying “Ring Janet and check if she is ok”.
Building blocks of the demonstrator
The inspiration for the demonstrator was a workshop that I ran in a previous week with some college students. I wanted to show them the basics of network configuration but with a real purpose, so we wrote a simple chat application. This used a little python library I wrote called “network.py” that hides the gritty detail of setting up network sockets, servers, clients, and threading. It makes it possible for children to write a network chat application in only a few lines of python code.
The workshop had three sections – flashing an LED, sensing a switch, and the network chat application. I joined the dots a bit and thought “wouldn’t it be great to merge all three together and control bits of hardware over the network”.
Server – sensing the switch
The server code is quite simple, and based on the network chat application. It waits for an incoming connection, then accepts it. For any character that is received, it reads the switch and replys with a “1″ or a “0″ to represent the switch state.
If the connection is lost, the server waits for another incoming connection again, so that the whole system recovers automatically if the network disappears then comes back again.
import RPi.GPIO as GPIO import time import network SWITCH = 10 GPIO.setmode(GPIO.BCM) GPIO.setup(SWITCH, GPIO.IN) def heard(phrase): print "heard:" + phrase for a in phrase: if a == "\r" or a == "\n": pass # strip it else: if (GPIO.input(SWITCH)): network.say("1") else: network.say("0") while True: print "waiting for connection" network.wait(whenHearCall=heard) print "connected" while network.isConnected(): print "server is running" time.sleep(1) print "connection closed"
Client – controlling the LED
The client code is just as simple, and again, based on the network chat application. It connects to the server by connecting to a know IP address that was configured earlier. In our workshop on networking @ryantek explains how to set up static IP addresses, which is what I used here. But you could use any scheme including dynamic addresses, or even look into Ryan’s new RPi.DNS project as a way of hosting your own public server using a Raspberry Pi.
The client first connects to the configured IP address of the server. Then, once per second, it sends a question mark (?). Any response that comes back is assumed to be a “1″ or a “0″ and this is used to turn the LED on or off.
If the connection is lost, the client repeatedly tries to reconnect, so that it will auto recover if the network disappears and comes back again.
import RPi.GPIO as GPIO import time import sys import network SERVER_IP = sys.argv LED = 11 GPIO.setmode(GPIO.BCM) GPIO.setup(LED, GPIO.OUT) gotResponse = False def heard(phrase): global gotResponse print "heard:" + phrase for a in phrase: if a == "\r" or a == "\n": pass # skip it elif a == "0": GPIO.output(LED, False) else: GPIO.output(LED, True) gotResponse = True while True: while True: try: print "connecting to switch server" network.call(SERVER_IP, whenHearCall=heard) break except: print "refused" time.sleep(1) print "connected" while network.isConnected(): gotResponse = False print "polling" network.say("?") while network.isConnected() and not gotResponse: print "waiting" time.sleep(1) print "connection closed"
Networking – joining it together
I just followed the instructions in the previous networking workshop to set up a static IP address on both Raspberry Pi’s, and connected them together via a short patch cable. The Raspberry Pi uses what is called “Auto-MDX”, which means you don’t need a crossover cable, the Ethernet device will “swap the wires for you” automatically.
Auto starting (headless)
To make the demo work headless, I edited the file “/etc/rc.local” and put a line at the end of the file to auto start the client or the server, depending on which machine it was on. This means I can just plug the whole thing in, wait about 25 seconds for the Raspberry Pi’s to boot, then the demo works.
I used some GPIO breakout boards from Maplin. This was a bit of overkill for just a switch and an LED, but my plan is to build up a box of sensors and actuators (e.g. movement sensors, sound sensors, motors, various displays) and then allow children to plug in different devices at both end to invent their own product.
Update September 2014
Since this project was first showcased in Stevenage, a lot of schools have shown interest in this project. The Raspberry Pi foundation have kindly turned this and related projects into a 4-lesson scheme of work that supports the new National Curriculum. Clive Beale at the Foundation has done a fantastic job of packaging this up into a complete networking scheme, which includes making your own Internet connected “Thing”. Please do take a look, and try those advanced exercises too! See link  below.
 Newer network code on github supports python2 and python3 (by @whaleygeek)
 Raspberry Pi Foundation – Networking Lessons, September 2014