Hello,
As a result of the Websocket/JSON Multislider ESP32 Webserver examples I want to add a webchart where you can select start time, stop time and days of the week. (To set a device for selected days during the week.).
I am struggeling how to store the JSON serial inputfile into Arduino variables that i can use.
The JSON inputfile is like:
myJSON {“Zo”:true,”Ma”:false,”Di”:false,”Wo”:false,”Do”:false,”Vr”:false,”Za”:false}
I use the Arduino_JSON library (So NOT the ArduinoJson.h library) with the following code:
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include “SPIFFS.h”
#include <Arduino_JSON.h>
void handleWebSocketMessage(void *arg, uint8_t *data, size_t len) {
AwsFrameInfo *info = (AwsFrameInfo*)arg;
if (info->final && info->index == 0 && info->len == len && info->opcode == WS_TEXT) {
data[len] = 0;
message = (char*)data;
if (strcmp((char*)data, “getValues”) == 0) {
notifyClients(getAllValues());
}
Serial.print(“Inputfile “);
Serial.println(message);
JSONVar obj =JSON.parse(message);
Serial.print(“JSONVar “);
Serial.println(obj);
Serial.println(obj[“Ma”]);
const char* Ma = obj[“Ma”];
Serial.println (Ma);
This produces the following output in the serial monitor:
Inputfile {“Zo”:true,”Ma”:true,”Di”:true,”Wo”:false,”Do”:false,”Vr”:false,”Za”:false}
JSONVar {“Zo”:true,”Ma”:true,”Di”:true,”Wo”:false,”Do”:false,”Vr”:false,”Za”:false}
true
So I am able to print the Day value (Ma = false or true) depending on the Checkbox in the Webinterface.
However I am not able to translate the Ma variable into a boolean or int variable ,
Can anybody help me out with this?
Thanks in advance,
Leo.
In general, look at how the WiFi credentials are entered in the WiFi Manager example. You should be able to copy that and change the field names, then just store the values.
Look at the declaration in the function, which I think is “uint8_t *data” You can only get a byte value that will be 1/0.
Hi Leo.
Your JSON is probably saving the true/false as a String.
So, I guess you’ll need to compare Strings with an if statement and then create the actual boolean variable depending on the string value.
I hope this helps.
Regards,
Sara
Thank you Sara. Ofcourse It are characters and not bool or int values. Thanks a lot for putting me in the right direction. I will start working with this😀