Posts Tagged ‘erlang’

Deploy Status Box using Raspberry Pi, LEDs, and Erlang

February 27th, 2013 No comments

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:

pi_deploy (5)

pi_deploy (1)

pi_deploy (4)

pi_deploy (2)

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
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:

# 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:

# 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
% 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
% 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).
Categories: Uncategorized Tags: , ,

erlbot: The Erlang IRC Bot

February 20th, 2013 No comments

I recently took a month long introductory course on Erlang. Erlang is a functional language. The closest I’ve come to functional programming is XSLT, so this was quite different for me. Erlang excels at concurrency and fault tolerance. It has made me think differently about how to solve problems which is always great. If you’re interested in Erlang, there is a great free tutorial Learn You Some Erlang

The project I decided to create to help me learn Erlang is an IRC bot. When I first went on the internet in 1995 or so, IRC was one of the first things I found and loved. My network of choice was EFnet. At the time, I recall it was somewhat lawless. People constantly warring over nicks and channels. People were always trying to flood each other off. It was a bit of an arms race. You ran scripts to protect yourself from other users. I bet one of the earliest uses of botnets was IRC. The original purpose was so you could steal a channel or keep one. Thanks to the internet, the world is a lot smaller now. IRC was one of the early technologies that helped regular people connect with each other across the world.

Like many organizations, the one I work for has offices all over the world. I’m on a team that has members in multiple locations. One old, but great way we keep connected is IRC. Different teams have different channels, but you’re free to watch or contribute to the conversation of any group. We use Google Hangouts to do our standups, but everyone I work with uses IRC for fast and easy communication throughout the day. Some people prefer to use an instant messenger client, but I always try to move those conversations to IRC. When you have conversations in a shared space, other people get a chance to listen in and contribute.

Erlbot is no HUBOT (yet). But it is a fun way to learn Erlang for me. I will be maintaining the bot at github:

Features so far:
- Fault tolerance – Using Erlang’s supervisor behavior, an error in a plugin or part of the bot won’t bring the bot down
- Uptime – Erlang supports hot code loading and I’ve built the plugin system to allow the addition of plugins while running
- Extensibility – Plugins!
- Evangelize Plugin – erlbot will send a random fact to the channel when someone mentions Erlang

My one Erlang tip is Reloader from mochiweb. I added this to my erlbot source code and now the first thing I do when I launch the erl shell is reloader:start_link(). This will automatically reload beam files when they are recompiled (say using ./rebar compile).

Categories: Uncategorized Tags: ,