Skip to content

Commit

Permalink
Merge pull request #562 from ashubhatt/develop
Browse files Browse the repository at this point in the history
Added MQTT Disconnect Callback Function
  • Loading branch information
hreintke committed Jan 31, 2016
2 parents 048dfd5 + 74ed367 commit bb231fe
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Sming/SmingCore/Network/MqttClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }

Expand Down
5 changes: 5 additions & 0 deletions Sming/SmingCore/Network/TcpClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,8 @@ void TcpClient::onFinished(TcpClientState finishState)
if (completed)
completed(*this, state == eTCS_Successful);
}

void TcpClient::setCompleteDelegate(TcpClientCompleteDelegate completeCb)
{
completed = completeCb;
}
2 changes: 2 additions & 0 deletions Sming/SmingCore/Network/TcpClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
16 changes: 16 additions & 0 deletions samples/MqttClient_Hello/app/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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/#");
}
Expand Down

0 comments on commit bb231fe

Please sign in to comment.