Assigning JsonVariant to CRGB led

I’m trying to assign a json array value to a led of CRGB type. I have struggled with this for days. Clearly, there is something that I don’t understand. Any assistance would be greatly appreciated.

I’m using the web ide compiling to a core with firmware 0.6.0 using these libraries: SparkJson (0.0.2), FastLED (3.1.5) and MQTT (0.4.17)

Incoming Json:

{
“state” : “on”,
“html” : [ “CRGB::Blue” ]
}

gLED and gLEDcount are both defined at the top of my program.

CRGB gLED[3]; // storage for saving leds
int gLEDcount;

Here is the function that decodes my Json:

bool decodePayload(char* json) {
const size_t bufferSize = 4*JSON_ARRAY_SIZE(3) + JSON_OBJECT_SIZE(2) + 60; // 60 is additional space for strings
StaticJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(json);
gEffect[0] = 0;

if (root.containsKey("state")) {
    gStateOn = (strcmp(root["state"], "on") == 0);
    strcpy(gState, root["state"].asString());
    client.publish(DEBUG,"in state");
    delay(100);
}

if (root.containsKey("effect")) {
    strcpy(gEffect, root["effect"].asString());
    client.publish(DEBUG,"in effect");
    delay(100);
}

if (root.containsKey("rgb")) {
    client.publish(DEBUG,"in rgb");
    delay(100);
    JsonArray& rgb = root["rgb"];
    gLEDcount = root["rgb"].size();  // try sizeof(gLED); for global led array 
    for(int i = 0; i<(gLEDcount + 1); i++){
        gLED[i].r = rgb[i][0]; 
        gLED[i].g = rgb[i][1]; 
        gLED[i].b = rgb[i][2];
    }
}

if (root.containsKey("html")) {
    client.publish(DEBUG,"in html");
    delay(100);
    JsonArray& html = root["html"];
    gLEDcount = root["html"].size();   
    for(int i = 0; i < gLEDcount; i++){
        gLED[i] = html[i];
    }
    client.publish(DEBUG,html[0]);
    delay(100);
}

return root.success();

}

I am getting what I would expect in my debug statements. First this client.publish(DEBUG,"in html"); then this client.publish(DEBUG,html[0]); which sends CRGB::Blue to my MQTT broker. The problem is that I have been unable to figure out how to assign that value to the CRGB led. This gLED[i] = html[i]; isn’t working.

"CRGB::Blue" in your JSON is only a string and not the actual object CRGB::Blue.
If you want to use the color constants I’d think using a collection of if() conditions would be required.
But I personally would rather send the numeric CRGB values, parse these and assign the color components individually (or at once as one int)

@ScruffR thanks for your reply.

I’m not following your suggestion on using a collection of if()s. I have my program working sending hex values, and I plan on working on sending the color components next but it bugs me that I haven’t been able to figure this out on my own. Could you give me an example of what you mean by the collection of if() conditions?

I was thinking along this line

  if (strcmp(root["html"], "CRGB::Red" == 0) {
    gLED[i] = CRGB::Red;
  }
  else if (strcmp(root["html"], "CRGB::Green" == 0) {
    gLED[i] = CRGB::Green;
  }
  else if (strcmp(root["html"], "CRGB::Blue" == 0) {
    gLED[i] = CRGB::Blue;
  }

C++ hasn’t got a thing like reflection in C# where you can easily find an object by its name, but there are ways to implement something similar like shown here

But I honestly don’t know whether FastLED has something like it built in and I’m not ploughing through their massive library :wink:

Thanks, I’ll check it out.