Integrating Webhooks and Hardware

Written by James on May 7th, 2014. Posted in Engineering

I’ve had my hands on an Arduino for a while and to be honest I wish I spent more time tinkering with it. For those not familiar with what Arduino is…

Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use hardware and software. It’s intended for artists, designers, hobbyists and anyone interested in creating interactive objects or environments.

In this post I’ll be explaining a little about Arduino, giving you a few resources to help you get up and running with your own Arduino, and talk about a quick project I put together making use of webhooks and an Arduino Ethernet microcontroller board.

I own an Arduino Duemilanove with an ATmega328 microcontroller. I got a Starter Kit from Oomlout, which today comes with the more modern Arduino Uno board. If you’re thinking about getting an Arduino I’d highly recommend getting a Starter Kit so you have lots of components to play with straight out the box.

Once you’ve got yourself a board, follow through the Getting Started with Arduino guide for your OS of choice (OS X, Linux, Windows). The guide will help you setup the IDE, install the drivers for the FTDI chip on the board (if you need to), and get your first sketch (program) uploaded and running on your board. There are lots examples bundled with the Arduino IDE too, check under the File menu and try a few of them out.

Arduino IDE

The Arduino language is based on C/C++ and is easy to pick up if you’re new to programming. For those more technically savvy, it links against AVR Libc which is a C library with the “goal to stick as best as possible to established standards” of ANSI, ANSI-C, and parts of their successor C99. There are a few things to bear in mind, such as the library functions are not guaranteed to be reentrant; the user manual goes in to more details about this.

There are a whole host of extra pieces such as an accelerometer or GPS that you can wire up to your Arduino. There are also additional shields which fit on top of your Arduino board to offer you NFC/RFID, data logging, and more. There are lots of resources and libraries available to get these additional components integrated in to your project.

I’ve picked myself up an Arduino Ethernet Shield and this week I put together a little project that integrates with Beanstalk webhooks. The aim for the project was to have the Arduino play an audio notification when someone makes a commit to our website’s Git repo on Beanstalk. I wired up a simple piezo element to play the audio notification and plugged the ethernet shield in to my router.

Arduino with Ethernet Shield

I chose to use Webduino, a third party web server library, to handle the HTTP requests from Beanstalk. I defined the MAC address and private IP address for the ethernet shield and configured the network router so that incoming requests on port 80 would be routed to this IP address. The following is the sketch I wrote and uploaded to the Arduino.

#include "SPI.h"
#include "Ethernet.h"
#include "WebServer.h"
#include "pitches.h"

static uint8_t mac[] = {0x90, 0xA2, 0xDA, 0x00, 0x1F, 0x42};
static uint8_t ip[] = {192, 168, 1, 50};

WebServer webserver("", 80);

#define BUZZER_PIN 9

int melody[] = {NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4};
int noteDurations[] = {4, 8, 8, 4, 4, 4, 4, 4};

void defaultCmd(WebServer &server, WebServer::ConnectionType type, char *, bool) {
  if (type != WebServer::POST) {
    server.httpFail();
    return;
  }
  
  for (int note = 0; note < 8; note++) {
    int noteDuration = 1000 / noteDurations[note];
    tone(BUZZER_PIN, melody[note], noteDuration);
    
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    
    noTone(BUZZER_PIN);
  }
  
  server.httpSuccess();
}

void setup() {
  Ethernet.begin(mac, ip);
  
  webserver.setDefaultCommand(&defaultCmd);
  webserver.begin();
}

void loop() {
  webserver.processConnection();
}

Having tested this locally I then setup the Beanstalk webhooks and added my public IP address. Now each time a commit was made Beanstalk would make a request to this IP address, it would be directed by my router to the Arduino, and if it was in fact a POST request the audio notification would be played.

The project didn’t take long and demonstrates the beauty of how simple, quick, and easy the Arduino is to turn your ideas in to working projects; plus the hardware is relatively cheap too. I strongly recommend you pick yourself up a starter kit and get playing. I’d love to know what you come up with.

You might also enjoy

Bringing Website Deploy Notifications to Slack

A few weeks ago Nik wrote about how we use Slack for communications. Today I’d like to share one of my favourite integrations we’ve setup: Beanstalk deployment notifications. The Why We have a…

Read More

Warning: Unknown: open(/var/lib/php5/sess_3hbu5qbqbe6q9n8raabb93a2s1, O_RDWR) failed: No space left on device (28) in Unknown on line 0 Warning: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/var/lib/php5) in Unknown on line 0