O'Reilly logo

Mastering Internet of Things by Peter Waher

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Recovering from lost connections

Since MQTT uses TCP, it requires a live socket connection to work. Since a socket connection may fail for different reasons over time, we need to build a recovery mechanism into our device. The simplest such mechanism is to check the state of the connection at regular intervals, and reconnect if we detect that the connection has been lost. We can use our sampling timer for this. Once a minute, we check the connection state, and initiate a reconnection if it is offline or in an error state. The reconnection itself fails, if the network is not available at the time. In that case, new reconnection attempts will be performed every minute:

if (Timestamp.Second == 0 && this.mqttClient != null && (this.mqttClient.State ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required