This is a networked traffic light system with 3 Raspberry Pis, acting like a traffic light junction with two sets of lights, a crossing button and pedestrian beep (buzzer). Built by Andy K.
I was demonstrating the complete set-up at the Coding Evening at TAP on 19th November 2015 and also at recent Jams and the simplified version at the Subject Leaders’ Meeting at WHSG.
I have uploaded a simplified single web-controlled example to GitHub here: https://github.com/andzandz/soslug-web-udp-controlled-pi-traffic-light
Hardware set up: Plug a PiStop into pins 12,10,8 and 6, or LEDs into pins 12,10 and 8, on each of the two slave Pis.
Each LED must have a resistor in series and be connected to ground to avoid damaging it/your Raspberry Pi. Please google for how to correctly connect a LED to a Pi. Also, connect a button to pin 11 of the master Pi and a buzzer to pin 15. Check that your button and buzzer work first.
Plug your three Pis into a router/switch/hub (if using a switch or hub, it must also be connected to another router) with network cables (the LAN ports, not the WAN port), or use WiFi dongles and connect them all to the same wireless network. School/council/etc networks may restrict communication between computers / access to various ports, in which case this wouldn’t work. But you can give it a go.
Diagram of the system:
Firstly, find your Pi’s network IP address by checking your router’s config page for devices called “raspberrypi”, or by running in a terminal on the Pi:
Look for eth0 (for wired, wlan for wireless) and then look for the second line of that section starting with “inet addr”. It will be 4 numbers separated by dots, such as 192.168.1.2 or 10.0.0.4. IP addresses may change when adding/removing them from the network.
For convenience, you can run commands on a Pi without having to have a monitor and keyboard plugged into it. Run “sudo apt-get install ssh” and then you can log in remotely using PuTTY on Windows (you will need to know the IP address – you might consider setting the Pis to (different!) static IP addresses so they never change – see tutorial here).
If you have a keyboard and monitor plugged into one Pi, you can use SSH to remotely log into other Pis on the same network to run terminal commands on them: (replace 192.168.x.x with the correct IP address) – as usual type the password “raspberry” unless you have changed it.
$ ssh firstname.lastname@example.org
This can get confusing (they all show “raspberrypi” at the terminal) so you can use ifconfig to tell which one you’re currently logged in to. Type exit to exit.
You will need to install the apache2 web server with PHP on the master Pi:
$ sudo apt-get install apache2 php5
To see if it works, type the Pi’s IP address into your browser’s URL bar and you should see an “it works!” default page. Once this works, put the contents of the html folder (in the zip download below) into /var/www/. If you extracted it into your home directory, then you can copy it there with the terminal command:
$ sudo cp -r html/* /var/www/
Now try going to the IP in your web browser again; you should see the web interface with buttons (the buttons might be tiny, the CSS styles need some tweaking).
On each slave Pi, run the following command
$ sudo python python/recv_lights.py &
The & at the end will detach the process from the terminal, you can launch more commands and close the terminal without killing the process. Now the slave Pis are listening for instructions sent through UDP with sockets.
On the master Pi, run the following commands (assuming you unzipped the python folder directly into your home directory)
$ sudo python python/button.py &
press enter, and then:
$ sudo python python/controlLights.py &
If it’s set up right, it should start cycling through the traffic light patterns. You will probably need to change the IP addresses listed in the Python (and index) files. I set my Pis to static IP addresses of 192.168.1.70, 71 and 72.
In particular, this line
$slave_ip = '192.168.1.' . $msgparts;
The 192.168.1. needs to be changed to the first three numbers of the Pi’s IP addresses.
Any questions, please email me at andy @ soslug d0t org.