Skip to content

Instantly share code, notes, and snippets.

@olexs
Last active February 4, 2020 20:36
Show Gist options
  • Save olexs/ad8f26dbb3748e024460110210cb2946 to your computer and use it in GitHub Desktop.
Save olexs/ad8f26dbb3748e024460110210cb2946 to your computer and use it in GitHub Desktop.
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecure.h>
#include <ArduinoJson.h>
// WiFi access data
const char *ssid = "WIFI_SSID";
const char *password = "WIFI_PASSWORD";
// Private token of your Gitlab user
const char *private_token = "GITLAB_PRIVATE_TOKEN";
// Gitlab HTTPS certificate SHA-1 fingerprint
const char *https_fingerprint = "F3 85 85 A1 04 F3 77 26 CF CC E5 CE E2 23 ED 63 A1 8F 54 DC";
// Replace <PROJECT_ID> with your Gitlab project ID (the unique number)
const char *url_pipeline = "https://gitlab.com/api/v4/projects/<PROJECT_ID>/pipelines?ref=master&per_page=1";
// LED output mapping
const int LED_RED = 5; // D1
const int LED_YELLOW = 4; // D2
const int LED_GREEN = 15; // D8
// How often to poll for updates
const int updateDelayMillis = 5000;
// How long to wait for response until switching to "error"
const int updateTimeoutMillis = 20000;
long lastUpdateMillis = 0;
enum PipelineState
{
SUCCESS,
RUNNING_TESTED,
RUNNING,
FAILED,
UNKNOWN,
ERROR
};
PipelineState currentState = UNKNOWN;
void setup()
{
pinMode(LED_GREEN, OUTPUT);
pinMode(LED_YELLOW, OUTPUT);
pinMode(LED_RED, OUTPUT);
// turn off onboard blue LED!
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
Serial.begin(115200);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// LED test
Serial.println("Testing LEDs...");
currentState = SUCCESS;
lastUpdateMillis = millis();
loop();
delay(3000);
currentState = RUNNING;
lastUpdateMillis = millis();
loop();
delay(3000);
currentState = FAILED;
lastUpdateMillis = millis();
loop();
delay(3000);
Serial.println("Starting.");
currentState = UNKNOWN;
}
void loop()
{
switch (currentState)
{
case UNKNOWN: // blink yellow
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_RED, LOW);
digitalWrite(LED_YELLOW, HIGH);
delay(1000);
digitalWrite(LED_YELLOW, LOW);
break;
case ERROR: // blink red
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_YELLOW, LOW);
digitalWrite(LED_RED, HIGH);
delay(1000);
digitalWrite(LED_RED, LOW);
break;
case SUCCESS: // green
digitalWrite(LED_GREEN, HIGH);
digitalWrite(LED_YELLOW, LOW);
digitalWrite(LED_RED, LOW);
break;
case FAILED: // red
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_YELLOW, LOW);
digitalWrite(LED_RED, HIGH);
break;
case RUNNING: // yellow
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_YELLOW, HIGH);
digitalWrite(LED_RED, LOW);
break;
case RUNNING_TESTED: // green + yellow
digitalWrite(LED_GREEN, HIGH);
digitalWrite(LED_YELLOW, HIGH);
digitalWrite(LED_RED, LOW);
break;
}
delay(1000);
update();
}
void update()
{
if (millis() <= (lastUpdateMillis + updateDelayMillis))
return;
if (WiFi.status() == WL_CONNECTED)
{
Serial.print("Updating pipeline status... ");
HTTPClient http;
http.setTimeout(updateTimeoutMillis);
http.begin(url_pipeline, https_fingerprint);
http.addHeader("PRIVATE-TOKEN", private_token);
int returnCode = http.GET();
if (returnCode != 200)
{
Serial.print("ERROR: ");
Serial.print(returnCode);
Serial.print(" (");
Serial.print(http.errorToString(returnCode));
Serial.print("): ");
Serial.print(http.getString());
currentState = ERROR;
}
else
{
Serial.print("update retrieved. ");
DynamicJsonDocument doc(http.getSize());
deserializeJson(doc, http.getString());
const char *pipelineStatus = doc[0]["status"];
Serial.print("Pipeline status: ");
Serial.print(pipelineStatus);
if (strcmp(pipelineStatus, "success") == 0)
{
currentState = SUCCESS;
}
else if (strcmp(pipelineStatus, "failed") == 0)
{
currentState = FAILED;
}
else if (strcmp(pipelineStatus, "running") == 0)
{
currentState = RUNNING;
}
else if (strcmp(pipelineStatus, "scheduled") == 0)
{
currentState = RUNNING_TESTED;
}
else
{
currentState = UNKNOWN;
}
}
Serial.println("");
}
lastUpdateMillis = millis();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment