spacepanel/show_leds/show_leds.ino

88 lines
2.2 KiB
C++

#include <ArduinoJson.h>
#include <Adafruit_NeoPixel.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#define OWN_PIN 28
#define FREE_CYCLES 10
#define PIN 2
#define NUMPIXELS 58
const char* ssid = "CTDO-LEGACY";
const char* password = "****";
const char* apiEndpoint = "http://spacepanel-aggregator-ingress.ctdo-spacepanel.195.160.168.3.nip.io/leds";
const int pollInterval = 10000;
HTTPClient http;
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_RGB + NEO_KHZ800);
int delayval = 500;
int freecycles = 0;
void setLED(int i, String value) {
String value_short = value.substring(1);
char charbuf[8];
value_short.toCharArray(charbuf,8);
long int rgb = strtol(charbuf,0,16); //=>rgb=0x001234FE;
byte r=(byte)(rgb>>16);
byte g=(byte)(rgb>>8);
byte b=(byte)(rgb);
Serial.print(value_short);
Serial.print("-> ");
Serial.print(r);
Serial.print(", ");
Serial.print(g);
Serial.print(", ");
Serial.println(b);
pixels.setPixelColor(i, pixels.Color(r/25,g/25,b/25));
}
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println(WiFi.macAddress());
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting...");
}
Serial.println("Connected.");
pixels.begin();
}
void loop() {
Serial.println("Sending request...");
http.begin(apiEndpoint);
int httpCode = http.GET();
Serial.println("Success.");
if (httpCode == 200) {
String json = http.getString();
const size_t capacity = JSON_ARRAY_SIZE(NUMPIXELS) + 510;
DynamicJsonDocument doc(capacity);
deserializeJson(doc, json);
for (int i = 0; i < NUMPIXELS; i++) {
if (i == OWN_PIN && freecycles < FREE_CYCLES) {
setLED(i, "#00ff00");
}
else {
Serial.print("LED" + String(i) + ": ");
const char* element = doc[i];
String value = String(element);
setLED(i, value);
Serial.print("LED" );
Serial.print(i);
Serial.print("Color: ");
Serial.println(value);
}
}
pixels.show();
freecycles++;
} else {
Serial.println("Error: Statuscode" + httpCode);
}
delay(pollInterval);
}