I am using a program that receives an MQTT message and splits up the parts and updates global variables to the values in the message. At least thats what it should do.
Im using the arduinojson library 6.13.0, and the program is successfully building a message and sending it out from the controller. It does receive the incoming message.
The board is some kind of D1 mini variant.
I used the assistant to get the code laid out correctly.
// Callback function header
void callback(char* topic, byte* payload, unsigned int length);
WiFiClient espClient;
PubSubClient client(mqtt_server, 1883, callback, espClient);
// Callback function
void callback(char* topic, byte* payload, unsigned int length) {
Serial.println("message received from MQTT server!!");
const size_t capacity1 = JSON_OBJECT_SIZE(4) + 50;
DynamicJsonDocument doc1(capacity1);
const char* json1 = "{\"fanCommand\":false,\"presence\":false,\"occupation\":false,\"controlTemp\":21.1}";
deserializeJson(doc1, json1);
fanCommand = doc1["fanCommand"];
presence = doc1["presence"];
occupation = doc1["occupation"];
controlTemp = doc1["controlTemp"];
Serial.print ("fan command - ");
Serial.println(fanCommand);
Serial.print ("presence - ");
Serial.println(presence);
Serial.print ("occupation - ");
Serial.println(occupation);
Serial.print ("control temp - ");
Serial.println(controlTemp);
}
I believe that whats happening is that I have declared what the message is, and not passed the message from MQTT into the variable to be deserialized. So I am always deserializing the same thing.
I havent tried this yet but I have read through the docs and found part 3.4
Its a bit above me but from what I can see the section refers to getting a message that is unknown, but I do know it. Its laid out exactly as in the json1 variable. So can I pass payload straight into json1 like this?
char* json1 = payload;
I havent tried yet but based on the fact Im confused Im guessing probably not. The payload is a byte* and the char* is not. I assume that will give me an error.
If thats incorrect, I assume I need to use the info in section 3.4 but rather than see what is in the message, work on it from the point of view that I already know what is in the message.
Thanks