Least Significant Bit

Ask Hack Learn Share!

Jenkins CI + Raspberry Pi + Arduino

Have you ever wanted to fire the office alarm, play a Star Trek epic track and start a red blinking light every time your build of the master branch broke in your Continuous Integration server? This is how we did it: we wired our Jenkins to make calls to a Sinatra webserver running on a Raspberry Pi which then commands an Arduino over an USB to have the fun going at the office.

Let’s introduce the parts of our prototype.

Raspberry PI

Raspberry Pi (RPi) is a computer the size of a fist. We chose to install a Raspbian distro which is a debian distro tailored to fit into this small device.

Arduino

Arduino is an open-source electronics platform that is incredibly powerful as an automats controller, with many sensors and periphrals available. In our prototype we wired it to a bread board with 2 leds, a red one on pin 2 and a green one on pin 3, and connected it to the Raspberry Pi over an USB cable.

Jenkins CI

Our Continuous Integration server of choice. Will be making API calls to the Sinatra web app running in the Raspberry Pi to report builds outcome.

Install the Raspberry Pi

Then it was just a matter of ssh-ing into the RPi and install things, the user is pi and password raspberry if I remember correctly. From now on, all bash commands are to be run in the bash of our debian in the RPi.

There exist many options to develop robots in a RPi, we chose Ruby as our language of choice, so install it with sudo apt-get install ruby

Download and install gort a Command Line Toolkit for robots.

If you want to follow along the easiest would be to clone the code of our prototype on github. So install git: sudo apt-get install git. Clone the repo, cd into it, do gem install bundler, and then run bundle install.

Inspired by a BaRuCo talk we chose the artoo project to be able to develop the robots using Ruby. The artoo project is awesome, it even provides an API to communicate with the robots. Our sinatra server just talks to the robots over this API.

To communicate the RPi with the Arduino we chose artoo-arduino which contains the drivers and adaptors to talk to Arduino using the Firmata protocol from the RPi.

Now install the Arduino in the Raspberry Pi.

$ artoo arduino install
$ artoo install socat

To communicate from the RPi to Arduino you need to get the address of the Arduino, so do:

$ dmesg # on a raspbian install
...
[13.643661] cdc_acm 1-1.2:1.0: ttyACM0: USB ACM device

Or using gort do

$ gort scan serial
...
[6.570000] cdc_acm 1-1.3:1.0: ttyACM0: USB ACM device

the ttyACMO part is the address of the Arduino.

Check the robot.rb file and change the values in line 5 for your Arduino’s address, and in line 8 and 9 for the correct led pins you choose to wire the breadboard.

Start the server

We are ready to start it, now run

$ artoo arduino connect ttyACM0 8023 > artoo.log 2>&1 &
$ ruby lib/robots.rb > robots.log 2>&1 &
$ ruby lib/server.rb > server.log 2>&1 &

The first and second line connect the RPi and the Arduino over the Firmata protocol. The third one starts your sinatra server. To see the action as it goes do tail -F *.logs if you like.

Jenkins CI

The CI needs to be configured to do a call to the Sinatra when a build succeeds and also when it fails. Basically this:

# for the success case this call
curl http://RASPBERRRY-IP:4567/builds/jobs?status=success

# for the fail case this call
curl http://RASPBERRRY-IP:4567/builds/jobs?status=failure

First get the ip of your RPi in your network, run ifconfig.

Then configure the Jenkins by going to the Configure page of the project. Then at the bottom press the button Add post-build action and select Post build task. We detected the build outcome by finding the words FAILURE and SUCCESS in the build log and then do the corresponding call above.

Now is time to build your project and see lights go green and red!

Video and slides

You can see a demo in this video

or even the slides for the Xing meet robots presentation.

Credits

Credit is due to my team mates in building this prototype Ricard Sole and Krzysztof Heród for an Innovation Week at XING.

blog comments powered by Disqus