arduino-sketches

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 4  →  ?path2? @ 1
/arduinoPinToggle/arduinoPinToggle.ino
@@ -77,21 +77,23 @@
};
#endif
 
const char* mqttSerialize(StaticJsonDocument<256> msg) {
char message[256];
const char* mqttSerialize(StaticJsonDocument<255> msg) {
String message;
serializeJson(msg, message);
return (const char*) message;
size_t length = message.length() + 1;
char payload[length];
message.toCharArray(payload, length);
 
return (const char*) payload;
}
 
void mqttCallback(char *topic, byte *payload, unsigned int length) {
String msgTopic = String(topic);
// payload is not null terminated and casting will not work
char msgPayload[length + 1];
snprintf(msgPayload, length + 1, "%s", payload);
Serial.println("Message received on topic: " + String(topic) + " with payload: " + String(msgPayload));
String msgPayload = (const char *)payload;
Serial.println("Message received on topic: " + msgTopic + " with payload: " + msgPayload);
 
// Parse the payload sent to the MQTT topic as a JSON document.
StaticJsonDocument<256> doc;
StaticJsonDocument<255> doc;
Serial.println("Deserializing message....");
DeserializationError error = deserializeJson(doc, msgPayload);
if (error) {
@@ -109,7 +111,7 @@
 
Serial.println("Setting pin: " + String(pin) + " to state: " + String(state));
// Announce the action.
StaticJsonDocument<256> msg;
StaticJsonDocument<255> msg;
msg["pin"] = pin;
msg["state"] = state;
msg["action"] = "set";