Can't compile Arduino code when using a payload

#1

Hey,

I’m attempting to send a device command to my Arduino (the one from the kit) and I want to include a payload. When I attempt to build it, it errs out.

The error I’m seeing is:

In function 'void handleCommand(LosantCommand*)':
rgbled:26: error: invalid initialization of reference of type 'ArduinoJson::JsonObject&' from expression of type 'ArduinoJson::JsonObject*'
   JsonObject& payload = command->payload;

The revelant section of the code is:

void handleCommand(LosantCommand *command) {
     Serial.print("Command received: ");
     Serial.println(command->name);
     Serial.println(command->time);
     JsonObject& payload = command->payload;
}

The code is copied from the Losant Github docs.

I’m not super familiar with C, but I guessed it was related to pointer vs object, so I changed the offending line to:

 JsonObject* payload = command->payload;

It compiles but I can’t get any of the payload values!

Any help would be appreciated!

#2

OK, I think I figured it out via trial-and-error:

JsonObject& payload = *command->payload;

I’m not sure as to why it works, however.

1 Like
#3

Looks like your code change is correct. The underlying library is returning a pointer. Not sure why it’s not just returning the reference. I’ll update the docs. Thanks!