diff --git a/Sming/SmingCore/Network/MqttClient.h b/Sming/SmingCore/Network/MqttClient.h index 12e0346167..23f0c9878d 100644 --- a/Sming/SmingCore/Network/MqttClient.h +++ b/Sming/SmingCore/Network/MqttClient.h @@ -35,6 +35,8 @@ class MqttClient: protected TcpClient bool connect(String clientName); bool connect(String clientName, String username, String password); + using TcpClient::setCompleteDelegate; + __forceinline bool isProcessing() { return TcpClient::isProcessing(); } __forceinline TcpClientState getConnectionState() { return TcpClient::getConnectionState(); } diff --git a/Sming/SmingCore/Network/TcpClient.cpp b/Sming/SmingCore/Network/TcpClient.cpp index 7a91711cd1..4ceb454bad 100644 --- a/Sming/SmingCore/Network/TcpClient.cpp +++ b/Sming/SmingCore/Network/TcpClient.cpp @@ -212,3 +212,8 @@ void TcpClient::onFinished(TcpClientState finishState) if (completed) completed(*this, state == eTCS_Successful); } + +void TcpClient::setCompleteDelegate(TcpClientCompleteDelegate completeCb) +{ + completed = completeCb; +} diff --git a/Sming/SmingCore/Network/TcpClient.h b/Sming/SmingCore/Network/TcpClient.h index 601c932426..72e662d440 100644 --- a/Sming/SmingCore/Network/TcpClient.h +++ b/Sming/SmingCore/Network/TcpClient.h @@ -47,6 +47,8 @@ class TcpClient : public TcpConnection virtual bool connect(IPAddress addr, uint16_t port); virtual void close(); + void setCompleteDelegate(TcpClientCompleteDelegate completeCb = NULL); + bool send(const char* data, uint8_t len, bool forceCloseAfterSent = false); bool sendString(String data, bool forceCloseAfterSent = false); __forceinline bool isProcessing() { return state == eTCS_Connected || state == eTCS_Connecting; } diff --git a/samples/MqttClient_Hello/app/application.cpp b/samples/MqttClient_Hello/app/application.cpp index 2cea924ea4..fd66a94cd1 100644 --- a/samples/MqttClient_Hello/app/application.cpp +++ b/samples/MqttClient_Hello/app/application.cpp @@ -17,6 +17,19 @@ Timer procTimer; // For quickly check you can use: http://www.hivemq.com/demos/websocket-client/ (Connection= test.mosquitto.org:8080) MqttClient mqtt("test.mosquitto.org", 1883, onMessageReceived); +// Check for MQTT Disconnection +void checkMQTTDisconnect(TcpClient& client, bool flag){ + + // Called whenever MQTT connection is failed. + if (flag == true) + Serial.println("MQTT Broker Disconnected!!"); + else + Serial.println("MQTT Broker Unreachable!!"); + + // Restart connection attempt after few seconds + procTimer.initializeMs(2 * 1000, startMqttClient).start(); // every 2 seconds +} + // Publish our message void publishMessage() { @@ -38,9 +51,12 @@ void onMessageReceived(String topic, String message) // Run MQTT client void startMqttClient() { + procTimer.stop(); if(!mqtt.setWill("last/will","The connection from this device is lost:(", 1, true)) { debugf("Unable to set the last will and testament. Most probably there is not enough memory on the device."); } + // Assign a disconnect callback function + mqtt.setCompleteDelegate(checkMQTTDisconnect); mqtt.connect("esp8266"); mqtt.subscribe("main/status/#"); }