spacepanel/show_leds/show_leds.ino

80 lines
2.0 KiB
Arduino
Raw Normal View History

2020-02-12 00:37:09 +00:00
#include <ArduinoJson.h>
#include <Adafruit_NeoPixel.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#define PIN 2
#define NUMPIXELS 12
const char* ssid = "CTDO-LEGACY";
const char* password = "******";
const char* apiEndpoint = "http://spacepanel.stablerock.de/leds";
const int pollInterval = 10000;
HTTPClient http;
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
int delayval = 500;
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/255,g/255,b/255));
pixels.show();
}
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++) {
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);
}
} else {
Serial.println("Error: Statuscode" + httpCode);
}
delay(pollInterval);
}