I finally figured out automated builds… for Arduino!

Currently I’m the main maintainer of node-hid, the Node.js package that lets you talk to USB HID devices like blink(1).  I recently cribbed automated build config from node-serialport so now node-hid is automatically built for Mac, Linux, & Windows and Node 4.x,6.x,7.x.   It’s pretty cool to have these robots doing my bidding.  But did you know you can do automated builds of Arduino sketches too?  I didn’t until last night and it’s AMAZING.

For instance, that CrashSpace BigButton Arduino sketch I mentioned previously, is now getting automatically recompiled for the Wemos D1 mini ESP8266 board every time I check in. See?

How is this magic done?

It turns out there’s a few ways to do it:

  • Using PlatformIO
  • Using Arduino from the commandline

I tried out both and PlatformIO was the cleaner and faster solution.  PlatformIO is basically a Python script connected to a huge microcontroller board and library database. If your board info and libraries you need are in that database, setup is very simple. For instance, here’s the entire “platformio.ini” conf file for the CrashSpaceESP Arduino sketch (a not trivial sketch running on a ESP8266 D1 mini using FastLED, ArduinoJson, and several ESP8266-specific libraries):

[platformio]
src_dir = .
[env:d1_mini]
platform = espressif8266
board = d1_mini
upload_speed = 921600
framework = arduino
lib_deps = FastLED, ArduinoJson

And then to test this on my laptop, I just type (after installing the platformio script):

cd CrashSpaceESP
platformio run

This is doing what in the Arduino GUI would be:

  • Install new ESP8266 board with Board Manager dialog
  • Install FastLED & ArduinoJson libraries with Library Manager dialog
  • Select Board from Tools/Board menu
  • Press the Compile button

And now that compiling Arduino sketches is easily commandline-able, we can use Travis to automate that. The folks at PlatformIO anticipated you might want to do this, so they offer a “continuous integration” mode.  This means that our Travis config can be just about be two lines:

install:
    - python -c "$(curl -fsSL https://raw.githubusercontent.com/platformio/platformio/master/scripts/get-platformio.py)"
script:
    - platformio ci --project-conf CrashButtonESP/platformio.ini CrashButtonESP

This is pretty astounding. However, if you want to use the real Arduino software to compile your code from the commandline, you can do that instead.  But it’s a bit more confusing to set up and slower to compile. Thankfully, Adafruit has a script to at least make the config easier. One of the simplest examples I’ve seen using this technique is the neat ESP8266HueEmulator project, which has a Travis config with a core of:

before_install:
  - source <(curl -SLs https://raw.githubusercontent.com/adafruit/travis-ci-arduino/master/install.sh)
script:
  - build_platform esp8266

That won’t install any libraries, you’ll need to do that yourself. And if you actually look at that Adafruit script, you’ll see it’s doing a lot of weird stuff behind the scenes like starting up a fake display. (mostly because Arduino is a Java app) But it’s official Arduino and you can track to the latest releases of things faster than PlatformIO.

I’ve recently become a big fan of automated builds because I have to compile for multiple OSes and software versions.  I’m stoked I can use the same tricks for embedded compiles too.  Thanks PlatformIO and Adafruit and Arduino and Travis and Appveyor and and and!

 

One Reply to “I finally figured out automated builds… for Arduino!”

  1. Been using Platform.io for a while via DevIoT’s plugin on ST3.
    It’s an interesting platform, although the plugin sometimes breaks annoying things :D

Leave a Reply

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