-1

I want to communicate two (2) ESP32's via Json commands using ESPAsyncWeb.h. The master should have specific operations to execute and respond to the client, and the client should be able to send specific Json commands to ask questions and receive responses. I have tried the method below but is throwing me weird errors such as

cannot bind non-const lvalue reference of type 'ArduinoJson::JsonObject&' {aka 'ArduinoJson6185_91::OBjectRef&'} to an rvalue of type 'ArduinoJson6185_91::enable_if<true, ArduinJson6185_91::ObjectRef>::type' {aka 'ArduinoJson6185_91::ObjectRef'}

I am not sure how to implement the operations for sending Json and receiving response from the Server using ArduinoJson 6. If anyone could enligthen me on how to conduct such transactions I'd appreciate it.

 void setup(){

//start serial comm
//Broadcast as WiFi SoftAP

 server.on("/command", HTTP_POST, [](AsyncWebServerRequest *request, JsonVariant &json) 
          {JsonObject& jsonObj = json.as<JsonObject>();
           uint8_t command = jsonObj["C"];
           String response;
           switch(command){
                           case 0: response = "{\"SCAN\":\"received_scan\"}";
                           request -> send(200, "application/json", response);
                           return;
                           case 1: /*...similar stuff */

                           case 2: /*...*/
}});
server.begin();
}

1
  • Have you tried following the examples in the ESPAsyncWebServer documentation? The code you used is different from the example.
    – romkey
    May 2, 2022 at 5:03

1 Answer 1

1

cannot bind non-const lvalue reference of type 'ArduinoJson::JsonObject&' {aka 'ArduinoJson6185_91::ObjectRef&'} to an rvalue of type 'ArduinoJson6185_91::enable_if<true, ArduinJson6185_91::ObjectRef>::type' {aka 'ArduinoJson6185_91::ObjectRef'}

In other words, "cannot assign JsonObject to JsonObject&". The compiler refuses to save a reference to a temporary variable because the reference would inevitably dangle.

To fix this issue, you must remove the ampersand (&) after JsonObject, like so:

JsonObject jsonObj = json.as<JsonObject>();

And it would be best if you also did the same for JsonVariant.

BTW, the cast from JsonVariant to JsonObject isn't necessary in your case, so you could simplify your program to:

server.on("/command", HTTP_POST,
          [](AsyncWebServerRequest* request, JsonVariant json) {
            switch (json["C"].as<uint8_t>()) {
              case 0:
                request->send(200, "application/json",
                              "{\"SCAN\":\"received_scan\"}");
                return;
                
              case 1: /*...similar stuff */

              case 2: /*...*/
            }
          });

I recommend that you have a look at ArduinoJson's documentation; I heard it's pretty good ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.