MQTT on Linkit ONE

In this blog we will see how to connect to a MQTT broker and receive messages from the broker. We will also handle connection losses to broker and WiFi AP.

Dependencies

We need download the PubSubClient first. this is the only external dependency program has.

Code organization

The flow is simple, we will use linkitOne WiFi client to connect to the WiFi AP , then connect to the MQTT broker. Once connected to broker we can subscribe to the channels and send messages on channels.

In the main loop we have to call loop() method on PubSubClient so that library code can check if new messages are available and call our callback method which process the messages.

We also need to check if we are connected to broker in the main loop. if the connection is broken we need to first check the WiFi connection, then try to reconnect to the broker. (you need to change the AP name, AP passowrd , MQTT username,password and hostname in the following code)