Over the weekend I decided to put together a small project using a Rasbperry Pi. One way of monitoring deployments at my company is IRC. I thought it would be neat to put together a Raspberry Pi to run erlbot and a plugin to toggle a couple LEDs to tell me when a deployment is in progress and when there was a problem with the deployment. (Note problem does not mean downtime, it just means a potential rollback or unexpected behavior during deployment which is transparent to our users). Here is the result:
There is a ton of helpful information out there on the Raspberry Pi. Here are a few links I found useful:
Beginner’s Guide to Raspberry Pi
LED tutorials: 1, 2, 3
GPIO Pins
Ten More Awesome Projects for your Raspberry Pi
I couldn’t find any Erlang modules for the Raspberry Pi to control LEDs on the Raspberry Pi, but I did find this post which pointed out you just write 1 or 0 to a file to toggle the LED. This is the basis of the led_controller.erl I wrote. You need to be root to write do anything with these files, but I wanted to run my Erlang script as a non-privileged user. Here is what to do if you want to control the LEDs while running as a normal user.
At your shell:
1 2 3 4 | # Add user group gpio sudo addgroup gpio # Put pi user in group usermod -a -G gpio pi |
Next we update rc.local so we set our pins to work as outputs on boot and give the pi user the proper access. Note I’m using GPIO pins 17 and 22. Replace those numbers with whatever pins you use. Put this in /etc/rc.local before exit 0:
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | # Use pin gpio17 as output echo 17 > /sys/class/gpio/export echo out > /sys/class/gpio/gpio17/direction # Allow users in gpio group to toggle pins chgrp gpio /sys/class/gpio/gpio17/value chmod g+w /sys/class/gpio/gpio17/value # Use pin gpio17 as output echo 22 > /sys/class/gpio/export echo out > /sys/class/gpio/gpio22/direction # Allow users in gpio group to toggle pins chgrp gpio /sys/class/gpio/gpio22/value chmod g+w /sys/class/gpio/gpio22/value # This was there already exit 0 |
You’ll need to run those lines as root or reboot before it will work. If you have your LEDs wired up and you’ve run the above, you can verify it works:
# Turn LED on pin 17 on! echo 1 > /sys/class/gpio/gpio17/value # Turn it off! echo 0 > /sys/class/gpio/gpio17/value |
Finally if you want to toggle or blink LEDs in Erlang, here is the led_controller.erl I wrote.
You can try it at the erl shell:
% Compile module c(led_controller). % Start led controller module (My Red LED is on pin 22, Green on 17) led_controller:start_link([{red, 22}, {green, 17}]). % Turn on the green led led_controller:on(green). % Blink the green led 10 times. When done it returns to previous state, on led_controller:blink(green, 10). % Blink the red led 5 times. When done it returns to previous state, off led_controller:blink(green, 5). |