Hi,
Pretty new to arduino development and I run into an issue, I’m pretty certain of that the error is that I send in the wrong type of string to Fastled.
I have an ESP32 with the following setup:
A JSON file that looks like this:
{
"1": "255, 0, 0","2": "255, 0, 0","3": "255, 0, 0","4": "0, 255, 0","5": "255, 0, 0","6": "0, 255, 0","7": "0, 0, 255","8": "0, 255, 0","9": "255, 0, 0","10": "255, 255, 255"
}
A my code base looks like this:
#include <WiFi.h>
#include <HTTPClient.h>
#include <Arduino_JSON.h>
#include <FastLED.h>
#define LED_PIN 19
#define NUM_LEDS 150
CRGB leds[NUM_LEDS];
const char* ssid = "Wifi";
const char* password = "Pass";
const char* serverName = "http://192.168.1.233/led/ledData.json";
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastTime = 0;
// Timer set to 10 minutes (600000)
//unsigned long timerDelay = 600000;
// Set timer to 5 seconds (5000)
unsigned long timerDelay = 5000;
String sensorReadings;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
Serial.println("Setting up fastled");
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.");
}
void loop() {
//Send an HTTP POST request every 10 minutes
if ((millis() - lastTime) > timerDelay) {
//Check WiFi connection status
if(WiFi.status()== WL_CONNECTED){
sensorReadings = httpGETRequest(serverName);
Serial.println(sensorReadings);
JSONVar myObject = JSON.parse(sensorReadings);
// JSON.typeof(jsonVar) can be used to get the type of the var
if (JSON.typeof(myObject) == "undefined") {
Serial.println("Parsing input failed!");
return;
}
Serial.print("JSON object = ");
Serial.println(myObject);
// myObject.keys() can be used to get an array of all the keys in the object
JSONVar keys = myObject.keys();
for (int i = 0; i < keys.length(); i++) {
JSONVar value = myObject[keys[i]];
leds[i] = CRGB(255, 0, 0); //CRGB(myObject[keys[i]]);
Serial.print("Set led no: ");
Serial.print(keys[i]);
Serial.print(" to ");
Serial.println(value);
}
FastLED.show();
}
else {
Serial.println("WiFi Disconnected");
}
lastTime = millis();
}
}
String httpGETRequest(const char* serverName) {
HTTPClient http;
// Your IP address with path or Domain name with URL path
http.begin(serverName);
// Send HTTP POST request
int httpResponseCode = http.GET();
String payload = "{}";
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
payload = http.getString();
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
return payload;
}
The line that looks like the one below is the one I’m struggling with:
leds[i] = CRGB(255, 0, 0); //CRGB(myObject[keys[i]]);
As it is now it works but I would like the led to get the color from the myObject[keys*] and not be hardcoded. Any idea on how I can get this to work?*
BR,
Andreas