Skip to content

Instantly share code, notes, and snippets.

@ArthurGuy
Created August 25, 2017 15:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ArthurGuy/784ce4b95a895752cb07df6a1320c026 to your computer and use it in GitHub Desktop.
Save ArthurGuy/784ce4b95a895752cb07df6a1320c026 to your computer and use it in GitHub Desktop.
UK Security Level MI5 Defcon Sign
#include "Particle.h"
#include "neopixel.h"
SYSTEM_MODE(AUTOMATIC);
// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_PIN D2
#define PIXEL_COUNT 120
#define PIXEL_TYPE WS2812B
Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
void setup()
{
strip.begin();
strip.show();
// The response is large and gets split into two parts, we are only interested in the first piece
Particle.subscribe("hook-response/get_security_level/0", securityLevelHandler, MY_DEVICES);
}
void loop()
{
Particle.publish("get_security_level");
delay(600000);
//activateLight(5);
//delay(1000);
//activateLight(4);
//delay(1000);
//activateLight(3);
//delay(1000);
//activateLight(2);
//delay(1000);
//activateLight(1);
//delay(1000);
}
void activateLight(uint8_t light) {
uint16_t i;
for(i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, strip.Color(0, 0, 0));
}
light = light - 1;
for(i = 0; i < 12; i++) {
strip.setPixelColor(i + (light * 12), strip.Color(255, 255, 255));
}
strip.show();
}
void securityLevelHandler(const char *event, const char *data) {
String str = String(data);
String levelString = tryExtractString(str).toLowerCase();
if (levelString != "") {
Particle.publish("SecurityLevel", levelString, PRIVATE);
if (levelString == "low") {
activateLight(5);
} else if (levelString == "moderate") {
activateLight(4);
} else if (levelString == "substantial") {
activateLight(3);
} else if (levelString == "severe") {
activateLight(2);
} else if (levelString == "critical") {
activateLight(1);
}
}
}
String tryExtractString(String str) {
if (str == NULL) {
return "";
}
int idx = str.indexOf("Current Threat Level:");
if (idx < 0) {
return "";
}
int endIdx = str.indexOf("</title>", idx);
if (endIdx < 0) {
return "";
}
return str.substring(idx + 22, endIdx);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment