Instructables

Jenkins CI Traffic Light

Picture of Jenkins CI Traffic Light

So I have an Arduino Uno lying around and I could not think of any projects except for the ones bundled in the software.

I decided to build a traffic light instead, the catch is that instead of the normal timed change of LED's, I wanted to read the status of our continuous integration server (Jenkins).

Of all tutorials I found on the www, not a single one worked out of the box, so I decided to break it down to it's simplest form and take it from there.

 
Remove these adsRemove these ads by Signing Up

Step 1: Required Items

HardWare.

  1. Arduino (I made use of the UNO V3 but any should work. {Please correct me if I'm wrong.})
  2. Bread Board
  3. Bread Board Jumper Cables
  4. LED's (Red, Yellow, Green)
  5. Resistors (180 Ohm is perfect. {Bands: Brown, Grey, Brown})

Software.

  1. PyCharm (by Jetbrains)
  2. Arduino

Please follow the individual applications install details as set out on their respective websites.

Step 2: Creating the Python Script (PyCharm)

Once PyCharm has been installed on yout computer, copy and paste the following code:

>>> Copy everything below this line <<<

import json
import sys import urllib2 import time import serial # Configurations ping_server = 30 jenkins_jobs=["Your Job Name Here"] ser = serial.Serial('COM3', 9600) # Arduino Configuration SUCCESS = 'b' FAILURE = 'r' BUILDING = 'a' UNSTABLE = 'y' time.sleep(5) def get_status(jobName): jenkinsUrl = "http://Your Jenkins Server:Port/job/" try: jenkinsStream = urllib2.urlopen( jenkinsUrl + jobName + "/lastBuild/api/json" ) except urllib2.HTTPError, e: print "URL Error: " + str(e.code) print " (job name [" + jobName + "] probably wrong)" sys.exit(2) try: buildStatusJson = json.load( jenkinsStream ) except: print "Failed to parse json" sys.exit(3) return jobName,buildStatusJson["timestamp"], buildStatusJson["result"], while(1): for job in jenkins_jobs: status = get_status(job) # print status[0], status[2] if status[2] == "UNSTABLE": ser.write(UNSTABLE) elif status[2] == "SUCCESS": ser.write(SUCCESS) elif status[2] == "FAILURE": ser.write(FAILURE) elif status[2] == None: ser.write(BUILDING) time.sleep(ping_server)

Notes:

On Windows 7, the Arduinos COM port number can be seen under the device manager.