Xmas2017 for Kosice

Intro

So I am now working with a team in Kosice (SK) for nearly two years and we have come a looong way. For this Christmas I thought they own a special present from my side, that also focuses and raises their awareness to green status lights on their continuous integration environment 😉

For this we need:

Yeap a 24V alarm light (Amazon) used by special agents on highway patrol 😉 So that the office is recognizing if there is a failed build / test run.

Used Hardware

In order to control the light we need a relay module and of course a small Arduino (Nano will do):

 PLUS        

I admit the used relay module (Amazon) is completely over the top, but you never know, maybe you want to add some more electrical devices to the feedback system later on (Laser, a real sirene, … Bildergebnis für evil grin)?

Printing the case

In order to have the hardware assembled and operating safely in place we need a case and since I have got my first 3D printer this year, it was obviously the best thing to print it on my own:

1) Base plate (ExtFeedbDevV3-base)

2) Side walls (ExtFeedbDevV3-side  && ExtFeedbDevV3-front && ExtFeedbDevV3-back)

3) Cover (ExtFeedbDevV3-cover)

This is how it will look like (after we have fixated the stuff and connected the pins too)

To glue the side plates, be sure to use a glue that is specific for PLA (polyamid), most of the time this is a two component glue. The first part is to “roughen” the surface of the plastic parts so that the second part can glue them, successfully. Not tested, but probably some formic acid will do the surface roughening job too, and then just Loctite it 😉 ! In my case i simply used Loctite super glue, but also noticed its not glued very tight, so handle with care.

Dance Arduino Dance!

So in order to get the stuff working we need to connect the pins of Nano with the relay module and programme the Nano. To be ready for future extensions i have not only connected one digital pin to the relay module but two (Did i mention LASER!!!! Bildergebnis für evil grin )

Me being a complete newbie on hardware stuff, it took me some time to find appropriate wiring to connect the two but finally here it is.

The wires' colours map to:
Brown  -> VCC
Red    -> Ground
Orange -> In1
Yellow -> In2 (unused for now)

 

Alarm driver software

The Arduino sketch for the alarm driver is really simply:

int LED = 2; // digital pin to used to control relay

void setup() {
 // control relay to close left circuit per default
 pinMode(LED, OUTPUT); 
 digitalWrite(LED, HIGH);
 // set up serial comm with USB port and print SETUP to signal we are ready
 Serial.begin(57600);
 Serial.println("SETUP");
}

void loop() {
 // if no command sent from pc return
 if (Serial.available() == 0) return;

 int inbyte = Serial.read();
 if (inbyte == '0') {
 Serial.println("Turning Alarm OFF"); 
 digitalWrite(LED, HIGH);
 } else if (inbyte == '1') {
 Serial.println("Turning Alarm ON"); 
 digitalWrite(LED, LOW);
 }
}

Upload this sketch to the Nano using the Arduino Studio and we are ready for the next steps 🙂

Together with the serial monitor (Arduino Studio) or HTerm.exe you can now connect a serial comm (57600 baud) to the Arduino. After you have uploaded the code, reset the Nano and wait for “SETUP” to appear in the serial console. Now using a Multimeter device you can check, that in initial state the right circuit is disconnected, while the left circuit is enabled and beeps.

Connecting the alarm light

Waiting for the power supply and the alarm light to appear in my mail box 😉

UPDATE 10th of Jannuary: So it arrived today, the alarm LED (24V) and a power supply (220V -> 24V). Cut the 24V cable and connect to the relay module’s right circuit as shown in the picture to the right.

If you connect it this way and the alarm LED starts as soon as you plug in the 220V you need to use the other circuit of the relay module 😉

Make sure to thread the cables BEFORE fixating them.

VOILA using the HTerm or Arduino Studio you can now turn on / off the alarm LED.

Now in order to activate the alarm LED programmatically I suggest to use the jSerialComm package. Maven POM is

 <dependency>
 <groupId>com.fazecast</groupId>
 <artifactId>jSerialComm</artifactId>
 <version>1.3.11</version>
 </dependency>

Just make sure to implement a SerialPortDataListener and wait for “SETUP” to be received from the Nano before sending a “1” back.

Checking Jenkins build status

For this we need to call the JENKINS JSON REST API and retrieve the status of the last build:

import org.apache.commons.io.Charsets;
import org.apache.commons.io.IOUtils; 
import net.sf.json.JSONObject;

....
Properties prop = new Properties();
prop.put("jenkins.url", "XXXXX");
prop.put("jenkins.user", "XXXXX");
prop.put("jenkins.pwd", "XXXXX");
String job = "XXX";

// Checking state of job
HttpURLConnection c = jm.getWebResource(jurl + "view/" + job.replace(" ", "%20") + "/lastBuild/api/json", prop.getProperty("jenkins.user"), prop.getProperty("jenkins.pwd"), "POST");
String json = IOUtils.toString(c.getInputStream(), Charsets.UTF_8);
JSONObject jo = JSONObject.fromObject(json);
if (jo.getString("result").equals("SUCCESS")) {
  // SUCCESS
} else {
  // START ALARM
}

Now in the Start alarm branch we will add the code to start the alarm ligtht and play some sound…

Playing the sounds

For playing sounds we will simply resort to the javax.sound package that allows to play WAV files.

Clip clip = (Clip) AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(new File("YOUR wav file here")));
clip.start();
Thread.sleep(1000);
while (clip.isRunning()) {
  Thread.sleep(1000);
}
clip.close();

If you need some convincing wav files: Star Wars- The Imperial March (Darth Vader’s Theme)

or Sick_Villain-Peter_De_Lang-1465872262

might be nice 😉

Combining it all together

Now in order to make this really work cool, organize a BIIIG loudspeaker (JBL Charge 3 or even larger 😉 Check your jenkins jobs and in case of failure, send “1” via serial USB comm to the arduino, while starting to play the sound…

Creme de la creme

Of course it would be sooo cool to personalize the audio to name the person(s) who broke the build. Well no code here but its fairly easy(and implemented for the Kosice team).

The Jenkins response returns a property “culprits” that is an array of full names. Now if you have one wav file for each name you can play them in sequence and end with “YOU HAVE BROKEN A BUILD!!!”. Be sure to add some hall effect to your voice (Audacity plus GVerb plugin helps a lot).

Then it could SOME DAY look like the following video:

Leave a Reply

Your email address will not be published. Required fields are marked *