#include <ArduinoJson.h>
// Sample JSON data stored in PROGMEM to save RAM
const char json[] PROGMEM = R"rawliteral({
"request": "control",
"type": "hardware(led)",
"data": [
{"index": 0, "RGB": "255,0,0"},
{"index": 1, "RGB": "0,255,0"},
{"index": 2, "RGB": "0,0,255"},
{"index": 3, "RGB": "255,255,0"},
{"index": 4, "RGB": "0,255,255"},
{"index": 5, "RGB": "255,0,255"},
{"index": 6, "RGB": "128,0,0"},
{"index": 7, "RGB": "0,128,0"},
{"index": 8, "RGB": "0,0,128"},
{"index": 9, "RGB": "128,128,0"},
{"index": 10, "RGB": "0,128,128"},
{"index": 11, "RGB": "128,0,128"},
{"index": 12, "RGB": "255,128,0"},
{"index": 13, "RGB": "128,255,0"},
{"index": 14, "RGB": "0,255,128"},
{"index": 15, "RGB": "128,0,255"},
{"index": 16, "RGB": "255,0,128"},
{"index": 17, "RGB": "0,128,255"},
{"index": 18, "RGB": "128,128,128"},
{"index": 19, "RGB": "64,64,64"},
{"index": 20, "RGB": "192,192,192"},
{"index": 21, "RGB": "255,64,64"},
{"index": 22, "RGB": "64,255,64"},
{"index": 23, "RGB": "64,64,255"},
{"index": 24, "RGB": "255,255,64"},
{"index": 25, "RGB": "64,255,255"},
{"index": 26, "RGB": "255,64,255"},
{"index": 27, "RGB": "192,0,0"},
{"index": 28, "RGB": "0,192,0"},
{"index": 29, "RGB": "0,0,192"},
{"index": 30, "RGB": "192,192,0"},
{"index": 31, "RGB": "0,192,192"},
{"index": 32, "RGB": "192,0,192"},
{"index": 33, "RGB": "255,128,128"},
{"index": 34, "RGB": "128,255,128"}
]
})rawliteral";
// Define a struct to hold the LED data, optimizing storage
struct LEDData {
uint8_t index;
uint32_t rgb; // Store RGB as a single uint32_t
};
LEDData ledDataArray[35];
void setup() {
Serial.begin(9600);
// Calculate the required size for the JSON document
const size_t capacity = JSON_ARRAY_SIZE(35) + 35 * JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(3) + 700; // Increased capacity
DynamicJsonDocument doc(capacity);
// Copy JSON from PROGMEM to RAM
char jsonBuffer[sizeof(json)];
memcpy_P(jsonBuffer, json, sizeof(json));
// Deserialize the JSON data
DeserializationError error = deserializeJson(doc, jsonBuffer);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
return;
}
// Parse the JSON array
JsonArray data = doc["data"];
for (JsonObject obj : data) {
uint8_t index = obj["index"];
const char* rgb = obj["RGB"];
int r, g, b;
sscanf(rgb, "%d,%d,%d", &r, &g, &b);
ledDataArray[index] = { index, ((uint32_t)r << 16) | ((uint32_t)g << 8) | b };
}
// Print the parsed data for verification
for (int i = 0; i < 35; i++) {
uint32_t rgb = ledDataArray[i].rgb;
Serial.print(F("Index: "));
Serial.print(ledDataArray[i].index);
Serial.print(F(", RGB: "));
Serial.print((rgb >> 16) & 0xFF);
Serial.print(F(", "));
Serial.print((rgb >> 8) & 0xFF);
Serial.print(F(", "));
Serial.println(rgb & 0xFF);
}
}
void loop() {
// Your loop code here
}