arduino-sketches
/arduinoPinToggle/arduinoPinToggle.ino |
@@ -77,23 +77,21 @@ |
}; |
#endif |
|
const char* mqttSerialize(StaticJsonDocument<255> msg) { |
String message; |
const char* mqttSerialize(StaticJsonDocument<256> msg) { |
char message[256]; |
serializeJson(msg, message); |
size_t length = message.length() + 1; |
char payload[length]; |
message.toCharArray(payload, length); |
|
return (const char*) payload; |
return (const char*) message; |
} |
|
void mqttCallback(char *topic, byte *payload, unsigned int length) { |
String msgTopic = String(topic); |
String msgPayload = (const char *)payload; |
Serial.println("Message received on topic: " + msgTopic + " with payload: " + msgPayload); |
// 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)); |
|
// Parse the payload sent to the MQTT topic as a JSON document. |
StaticJsonDocument<255> doc; |
StaticJsonDocument<256> doc; |
Serial.println("Deserializing message...."); |
DeserializationError error = deserializeJson(doc, msgPayload); |
if (error) { |
@@ -111,7 +109,7 @@ |
|
Serial.println("Setting pin: " + String(pin) + " to state: " + String(state)); |
// Announce the action. |
StaticJsonDocument<255> msg; |
StaticJsonDocument<256> msg; |
msg["pin"] = pin; |
msg["state"] = state; |
msg["action"] = "set"; |