Beliebte Suchanfragen
//

Using a Raspberry PI to control an extreme feedback device

24.7.2013 | 6 minutes of reading time

In this article I will show you how to use a Raspberry PI and a Jenkins CI server to control a USB traffic light. I have used the USB traffic light in software projects to display the status of one or multiple Jenkins CI jobs. The traffic light was clearly visible to all team members and stakeholders and everybody knew right away once a build was broken.

Unfortunately I did not find an affordable USB traffic light, that I could control directly from the Jenkins CI server over the network and that came with software for different operating systems. I was also forced to have it connected to my local computer. Once I left the office and the computer was shut down, the traffic light was turned off.

That problem can be solved using the inexpensive Raspberry PI 🙂

Architecture

The following diagram gives an overview of the architecture:

  1. We will have a script running on the Raspberry PI that constantely polls the status of a configured Jenkins Job
  2. Depending on the status of the Jenkins Job the script updates the USB traffic light

And these are the Prerequisites

Lets go 🙂

Raspberry PI installation

First of all we need to get the Raspberry up and running. Therefore you need to install a bootloader and operating system image on the SD Card. I suggest you use the Raspbian “wheezy” installation.

I was running the installation from my ubuntu computer. When you are using Windows or Mac please check http://elinux.org/RPi_Easy_SD_Card_Setup for information.

Step 1: Download the wheezy image

http://www.raspberrypi.org/downloads

Step 2: Verify and unzip image

1marcelbirkner@ubuntu:~/Downloads/raspberry$ sha1sum 2013-02-09-wheezy-raspbian.zip
2b4375dc9d140e6e48e0406f96dead3601fac6c81 2013-02-09-wheezy-raspbian.zip
3 
4marcelbirkner@ubuntu:~/Downloads/raspberry$ unzip 2013-02-09-wheezy-raspbian.zip

Step 3: Prepare the SD Card

Insert the SD Card into your computer, in order to copy the wheezy image onto the card.

1marcelbirkner@ubuntu:~/Downloads$ df -h
2Filesystem Size Used Avail Use% Mounted on
3...
4/dev/mmcblk0p1 1.9G 1.8G 75M 97% /media/marcelbirkner/SD_CARD
5 
6marcelbirkner@ubuntu:~/Downloads$ umount /dev/mmcblk0p1

Step 4: Copy image to SD Card

1sudo dd bs=4M if=2013-02-09-wheezy-raspbian.img of=/dev/mmcblk0

Step 5: Prepare Raspberry PI hardware

Once you have copied the wheezy image onto the SD Card you can connect the card to your Raspberry. Add the network cable, tv cable, USB keyboard, your monitor and the Cleware USB traffic light to the Raspberry. After that you can attach the micro USB power supply to startup your Pi.

You should see a blue screen starting up the system. Please follow the instructions on the screen. During the first startup the Raspberry wants to connect to the internet and check for updates. Once you are done you can proceed with the next step.

Step 6: Login to your Raspberry PI

1ssh pi@192.168.0.104
2Password: raspberry

Once you are logged in you can check out the operating system and directory structure. Next you should switch to the root user.

1sudo su

Step 7: Install Clewarecontrol

During the next step we need to install additional software that controls the USB traffic light under Linux. This Software is provided by Folkert van Heusden.

1wget http://www.vanheusden.com/clewarecontrol/clewarecontrol-2.5.tgz
2tar xvfz clewarecontrol-2.5.tgz
3cd clewarecontrol-2.5
4make install

Step 8: Test USB traffic light

First verify that you can find your USB traffic light. It should return the device id. You can use that id later on to control specific traffic lights, in case you have multiple attached to your Raspberry PI.

1root@raspberrypi:~/clewarecontrol-2.5# clewarecontrol -l
2Cleware library version: 330
3Number of Cleware devices found: 1
4Device: 0, type: Switch1 (8), version: 29, serial number: 900455

In case the device list is empty you should reboot the raspberry.

1sudo reboot

Once the software is installed correctly ou should be able to control the red, yellow and green light:

1root@raspberrypi:~/clewarecontrol-2.5# clewarecontrol -d 900455 -as 2 1
2Switch 2: set to On

Step 9: Connect Jenkins Build Job and USB Traffic light

In the last step we need to configure a script that checks the status of a Jenkins CI Job. You can use the following shell script for that. Just fill out your properties on top of the script and start it on the Raspberry PI command line.

1#!/bin/sh
2 
3# This script is used to control a USB Traffic Light from Cleware. You can buy 
4# the USB Traffic Light from this shop: http://www.cleware-shop.de
5# 
6# The script uses the REST API and can be used for newer versions of Jenkins that provide a REST API.
7#
8# Requirements:
9# 
10#   The Cleware USB Traffic Light comes with a control software that you can 
11#   download from http://www.vanheusden.com/clewarecontrol/ 
12#
13#   This script can be run under Linux. You need to have "curl" installed, 
14#   so the script can poll the REST API.
15#
16#   This script has been tested under Ubuntu and clewarecontrol 2.0
17#
18# @MarcelBirkner
19# 
20 
21USER=user
22PASSWORD=password
23JENKINS_SERVER=http://serverurl:8080/job/
24JOB_NAME=
25DEVICE_NO=
26 
27# Methods for controlling the device (2=blue, 1=yellow, 0=red)
28lightOn() {
29  clewarecontrol -c 1 -b -d $DEVICE_NO -as $1 1 2>&1 
30}
31lightOff() {
32  clewarecontrol -c 1 -b -d $DEVICE_NO -as $1 0 2>&1 
33}
34allOff() {
35  lightOff 0;
36  lightOff 1;
37  lightOff 2;
38}
39 
40while true; do 
41  color=`curl -silent -u $USER:$PASSWORD $JENKINS_SERVER$JOB_NAME/api/json?pretty=true | grep color `
42  state=`echo $color | sed 's/\"//g' | sed 's/,//g' | awk '{print $3}'` 
43  echo $state;  
44  case $state in 
45    red)          echo "State: $state"; allOff; lightOn 0;;
46    yellow)       echo "State: $state"; allOff; lightOn 1;;
47    blue)         echo "State: $state"; allOff; lightOn 2;;
48    red_anime)    echo "State: $state"; allOff; sleep 1; lightOn 0; sleep 1; lightOff 0; sleep 1; lightOn 0;;
49    yellow_anime) echo "State: $state"; allOff; sleep 1; lightOn 1; sleep 1; lightOff 1; sleep 1; lightOn 1;;
50    blue_anime)   echo "State: $state"; allOff; sleep 1; lightOn 2; sleep 1; lightOff 2; sleep 1; lightOn 2;;
51    *)            echo "Nothing matched state: $state";;  
52  esac;
53  sleep 1;  
54done;

I posted the script as a Gist on Github so you do not have to manually create the script on your PI. Link: https://gist.github.com/marcelbirkner/6037722/download

Simply follow these instructions:

1wget -o gist.tgz https://gist.github.com/marcelbirkner/6037722/download
2tar xvfz gist.tgz
3 
4cd gist6037722*
5chmod u+x controlTrafficLight.sh

In the next step you need to configure the shell script that checks your Jenkins job status and controls the usb traffic light.

  • set USER
  • set PASSWORD
  • set JENKINS_SERVER
  • set JOB_NAME
  • set DEVICE_NO

Once you have configured these parameters on top of the shell script you can start the script. It should poll your jenkins and update the traffic lights.

Start the configured jenkins job and see what happens 🙂

You can find more sample script for bash, ruby and clojure on the following site:

https://github.com/codecentric/cleware-build-traffic-light-jenkins-connector

The Final Result

Here are some pictures and a video what my setup looks like. For the video I configured a Jenkins job that has a select box. Depending on the selected value (success, failure) the build job will fail or succeed.

Video showing the Raspberry PI, an example Jenkins Job and the Cleware USB traffic light.

Watch video: Raspberry PI controlling usb traffic light

Have fun with your new extreme feedback device and feel free to share your ideas and experiences 🙂

share post

//

Gemeinsam bessere Projekte umsetzen.

Wir helfen deinem Unternehmen.

Du stehst vor einer großen IT-Herausforderung? Wir sorgen für eine maßgeschneiderte Unterstützung. Informiere dich jetzt.

Hilf uns, noch besser zu werden.

Wir sind immer auf der Suche nach neuen Talenten. Auch für dich ist die passende Stelle dabei.