• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

ESP32 webserver use JSON string

Q&A Forum › Category: ESP32 › ESP32 webserver use JSON string
0 Vote Up Vote Down
Leo asked 3 years ago

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.

Question Tags: Arduino_JSON library, ESP32 Webserver, JSON
4 Answers
0 Vote Up Vote Down
Barry answered 3 years ago

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.

0 Vote Up Vote Down
Sara Santos Staff answered 3 years ago

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

0 Vote Up Vote Down
Leo answered 3 years ago

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😀

0 Vote Up Vote Down
Sara Santos Staff answered 3 years ago

Great!
Then, let us know if you need further help.
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.