I am wrapping my head around this problem quite a long time now. I am opening a websocket Connection from my ESP32 to my NodeJS Backend. When receiving a message, the content gets parsed with ArduinoJSON.
I store the parsed content in global Variables so I can access them in my void loop(). Everytime a new message comes in they are overwritten. Thats how it should be.
The Variable Declaration:
uint8_t brightness = 10;
uint8_t lastMillis = 0;
int ArrayPointer = 0;
int interval = 2000;
bool immediate = true;
const size_t capacity = JSON_ARRAY_SIZE(32) + JSON_OBJECT_SIZE(1) + 290;
After the void Setup()
void onMessageCallback(WebsocketsMessage message) {
Serial.print("Got Message: ");
DynamicJsonBuffer jsonBuffer(capacity);
JsonObject & JSONResponse = jsonBuffer.parseObject(message);
JsonArray & PixelArray = JSONResponse["frame"];
brightness = JSONResponse["brightness"];
ArrayPointer = 0;
immediate = true;
}
void loop() {
client.poll();
if(millis() - lastMillis >= interval || immediate == true) {
// Here I would like to access the Variable PixelArray
lastMillis = millis();
}
}
Of course I cant access PixelArray in the void loop because its a different scope. Now i Need a way to make PixelArray globally accessible.
What I tried:
- Declared a global JsonArray before void Setup() but this threw an error ;(.
- Assigning it to another (global) array didnt work properly because the size of the PixelArray varies.
Hopefully somebody can help me ;)
Thanks in advance ;)
PS: Currently I am using ArduinoJson 5 but an upgrade would be no problem.