arduino-sketches – Diff between revs 1 and 4

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 1 Rev 4
Line 75... Line 75...
75 23, 25, 26, 27, 32, 33, 34, 35, 36, 37, 75 23, 25, 26, 27, 32, 33, 34, 35, 36, 37,
76 38, 39 76 38, 39
77 }; 77 };
78 #endif 78 #endif
Line 79... Line 79...
79   79  
80 const char* mqttSerialize(StaticJsonDocument<255> msg) { 80 const char* mqttSerialize(StaticJsonDocument<256> msg) {
81 String message; 81 char message[256];
82 serializeJson(msg, message); -  
83 size_t length = message.length() + 1; -  
84 char payload[length]; -  
85 message.toCharArray(payload, length); -  
86   82 serializeJson(msg, message);
87 return (const char*) payload; 83 return (const char*) message;
Line 88... Line 84...
88 } 84 }
89   85  
-   86 void mqttCallback(char *topic, byte *payload, unsigned int length) {
-   87 String msgTopic = String(topic);
90 void mqttCallback(char *topic, byte *payload, unsigned int length) { 88 // payload is not null terminated and casting will not work
91 String msgTopic = String(topic); 89 char msgPayload[length + 1];
Line 92... Line 90...
92 String msgPayload = (const char *)payload; 90 snprintf(msgPayload, length + 1, "%s", payload);
93 Serial.println("Message received on topic: " + msgTopic + " with payload: " + msgPayload); 91 Serial.println("Message received on topic: " + String(topic) + " with payload: " + String(msgPayload));
94   92  
95 // Parse the payload sent to the MQTT topic as a JSON document. 93 // Parse the payload sent to the MQTT topic as a JSON document.
96 StaticJsonDocument<255> doc; 94 StaticJsonDocument<256> doc;
97 Serial.println("Deserializing message...."); 95 Serial.println("Deserializing message....");
98 DeserializationError error = deserializeJson(doc, msgPayload); 96 DeserializationError error = deserializeJson(doc, msgPayload);
Line 109... Line 107...
109 String state = (const char *)doc["state"]; 107 String state = (const char *)doc["state"];
110 const int pin = (const int)doc["pin"]; 108 const int pin = (const int)doc["pin"];
Line 111... Line 109...
111   109  
112 Serial.println("Setting pin: " + String(pin) + " to state: " + String(state)); 110 Serial.println("Setting pin: " + String(pin) + " to state: " + String(state));
113 // Announce the action. 111 // Announce the action.
114 StaticJsonDocument<255> msg; 112 StaticJsonDocument<256> msg;
115 msg["pin"] = pin; 113 msg["pin"] = pin;
116 msg["state"] = state; 114 msg["state"] = state;
117 msg["action"] = "set"; 115 msg["action"] = "set";
118 mqttClient.publish(MQTT_TOPIC().c_str(), mqttSerialize(msg)); 116 mqttClient.publish(MQTT_TOPIC().c_str(), mqttSerialize(msg));