Archiv für den Monat: Januar 2018

Arduino compile on command line for ESP8266 for CI (Continuous Integration)

I wanted to test the functionality of GitLab for CI. As I have a lot of code which was written with the Arduino IDE for ESP8266-Boards, especially for the WeMos D1 Mini, I figured that I should try to automate the compile process for verification of the code.

There were some minor problems that I had to work out first, though:

# Unpack newest Arduino IDE somewhere, e.g. /opt/arduino/arduino-1.8.5 and cd to that dir
mkdir /opt/arduino
cd /opt/arduino
wget https://downloads.arduino.cc/arduino-1.8.5-linux64.tar.xz
tar xvfJ arduino-1.8.5-linux64.tar.xz
cd arduino-1.8.5

The further configuration MUST be made under the new user „gitlab-runner“, as this context will be used during the CI. So we have to create the user first and switch into his context:

sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
sudo su - gitlab-runner

Make sure that the current user is now the gitlab-runner, than continue:

# Add Additional Boards (ESP8266)
cd /opt/arduino/arduino-1.8.5
./arduino --pref "boardsmanager.additional.urls=https://adafruit.github.io/arduino-board-index/package_adafruit_index.json,http://arduino.esp8266.com/stable/package_esp8266com_index.json" --save-prefs
./arduino --install-boards esp8266:esp8266

The resulting board specifications can be found here:
~/.arduino15/packages/esp8266/hardware/esp8266/2.4.0/boards.txt

Some libraries can be installed easily as they are available directly from the official library list, these can be installed like this:

# Install SimpleDHT
./arduino --install-library SimpleDHT

Than there are some libraries, preferably on github, which are available as a tar.gz-File, these should be installed to the ~/Arduino/libraries-Directory, like this (for AsyncMQTTClient):

# Install AsyncMQTTClient
wget -q https://github.com/marvinroger/async-mqtt-client/archive/v0.8.1.tar.gz -O - | tar xvfz - -C ~/Arduino/libraries/

The above mentioned Library depends on ESPAsyncTCP, which is NOT available as a ZIP-File, so we have to clone the github directory and copy the relevant part to the Library-Directory:

# Install ESPAsyncTCP
GITCLONEDIR=$(mktemp -d) && git clone https://github.com/me-no-dev/ESPAsyncTCP ${GITCLONEDIR} && cp -r ${GITCLONEDIR}/src ~/Arduino/libraries/ESPAsyncTCP

To test compilation, we’ll be using the following short code, put it in a directory „ESP-TEST“ and name it „ESP-TEST.ino“:

#include <ESP8266WiFi.h>
 
void setup() {
  Serial.begin(115200);
  Serial.println("5s Deep Sleep initiating");
  ESP.deepSleep(5000000);
  delay(100);
}
 
void loop() {}

To run the compilation, we have to set the CPU Frequency (here: 80 MHz) and the Memory Ratio on how much we want to give to SPIFFS (here: 1M for SPIFFS):

./arduino -v --board esp8266:esp8266:d1_mini:CpuFrequency=80,FlashSize=4M1M --verify ESP-TEST/ESP-TEST.ino

To activate the CI in GitLab, we first have to install the gitlab-runner as a service. Trouble is, the gitlab-runner which is available from within Ubuntu (17.10 as of the time of this writing) is incompatible with gitlab >10.0, so we MUST install it manually as mentioned on the GitLab Runner Install Site.

sudo wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
sudo chmod +x /usr/local/bin/gitlab-runner
sudo /usr/local/bin/gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
sudo gitlab-runner start

For updates of the runner in the future, you have to follow these instructions:

sudo gitlab-runner stop
sudo wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
sudo chmod +x /usr/local/bin/gitlab-runner
sudo gitlab-runner start

In your GitLab Project-Settings, go to „CI/CD“ and use the values under „Specific Runners“ to register the runner. When asked for a executor, answer „Shell“. Please activate „git clone“ instead of „git fetch“, at least in my configuration I got errors like error: could not lock config file, the switch to „git clone“ resolved these.

sudo gitlab-runner register

By now the runner should be registered, but we still have to „run“ it. I prefer using a screen session for this. Exit the session by pressing CTRL+a and then „d“ (this „detaches the screen“, to return to the screen, enter screen -r gitlabrunner).

screen -S gitlabrunner
cd ~gitlab-runner
sudo gitlab-runner run --user=gitlab-runner
# Exit by pressing "CTRL+a" and then "d".

Finally we can start with the .gitlab-ci.yml. Add a file with this name to your project and add the following context (assuming you were using the example project from above):

build:
  stage: build
  script: 
  - /opt/arduino/arduino-1.8.5/arduino -v --board esp8266:esp8266:d1_mini:CpuFrequency=80,FlashSize=4M1M --verify ESP8266-DHT22.ino