Files
Ardiuno/WS3/WS3-ESP32/ws3_mqtt.ino
2021-01-06 17:44:20 +01:00

55 lines
1.3 KiB
C++

// MQTT Stuff
//void callback(char* topic, byte* payload, unsigned int length) {
// debug("Message arrived [");
// Serial.print(topic);
// debug("] ");
// for (int i = 0; i < length; i++) {
// Serial.print((char)payload[i]);
// }
// debug(" ");
//}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
debug("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(Mqtt_clientid)) {
debugln("connected");
// ... and resubscribe
client.subscribe(dom_in);
} else {
debug("failed, rc=");
debug(client.state());
debugln(" try again in 5 seconds");
// Wait 5 seconds before retrying
for(int i = 0; i<5000; i++){
delay(1);
}
}
}
}
// Sends MQTT payload to the Mosquitto server running on a Raspberry Pi.
// Mosquitto server deliveres data to Domoticz server running on a same Raspberry Pi
void sendMQTTPayload(String msgpayload)
{
// Convert payload to char array
msgpayload.toCharArray(msgToPublish, msgpayload.length()+1);
//Publish payload to MQTT broker
if (client.publish(dom_in, msgToPublish))
{
debug("Following data published to MQTT broker: ");
debug(dom_in);
debug(" ");
debugln(msgpayload);
}
else
{
debug("Publishing to MQTT broker failed... ");
debugln(client.state());
}
}