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)

1#include <LWiFi.h>
2#include <LWiFiClient.h>
3#include <LGSM.h>
4#include <Arduino.h>
5#include <PubSubClient.h>
6 
7// WiFi stuff
8#define WIFI_AP "*****"
9#define WIFI_PASSWORD "*****"
10#define WIFI_AUTH LWIFI_WPA // one of LWIFI_OPEN / LWIFI_WPA / LWIFI_WEP
11#define mqttUser "****"
12#define mqttPass "***"
13#define mqttPort 1883
14LWiFiClient wifiClient;
15IPAddress myIP;
16uint8_t macAddressBin[VM_WLAN_WNDRV_MAC_ADDRESS_LEN] = {0};
17char macAddressStr [20];
18  
19// MQTT broker
20char mqttBroker[] = "******";
21 
22 
23char mqttClientPrefix[] = "LinkitOne01";
24char mqttClientId[40];
25PubSubClient client( wifiClient );
26 
27// Topics
28char inTopic[] = "/node/sms/#";
29 
30//SMS
31char message[256];
32 
33 
34unsigned long lastSend;
35 
36//------------------------------------------------------------------------------------------------
37 
38void InitLWiFi()
39{
40 LWiFi.begin();
41 // Keep retrying until connected to AP
42 Serial.println("Connecting to AP: " + String(WIFI_AP));
43 while (0 == LWiFi.connect(WIFI_AP, LWiFiLoginInfo(WIFI_AUTH, WIFI_PASSWORD))) {
44 delay(1000);
45 }
46 myIP = LWiFi.localIP();
47 Serial.println("Connected");
48 Serial.print("IP Address: ");
49 Serial.println(myIP);
50 LWiFi.macAddress(macAddressBin);
51 Serial.print("MAC Address: ");
52 sprintf(macAddressStr, "%02x%02x%02x%02x%02x%02x", macAddressBin[0], macAddressBin[1], macAddressBin[2], macAddressBin[3], macAddressBin[4], macAddressBin[5]);
53 Serial.println(macAddressStr);
54 sprintf(mqttClientId, "%s.%s", mqttClientPrefix, macAddressStr); // construct unique mqtt client id from prefix and MAC
55 
56}
57 
58void reconnect() {
59 // Loop until we're reconnected
60 while (!client.connected()) {
61 if(LWiFi.status()==LWIFI_STATUS_DISCONNECTED){
62 Serial.println("Connecting to wifi again");
63 InitLWiFi();
64 }
65  
66 Serial.println("Connecting to MQTT broker: " + String(mqttBroker));
67 // Attempt to connect
68 if ( client.connect(mqttClientId,mqttUser,mqttPass) ) { // Better use some random name
69 Serial.println( "Connected as client: " + String(mqttClientId));
70 Serial.println("Subscribing to topic: " + String(inTopic));
71 client.publish( mqttClientId, "ready" );
72 client.subscribe( inTopic );
73 } else {
74 Serial.print( "[FAILED] [ rc = " );
75 Serial.print( client.state() );
76 Serial.println( " : retrying in 5 seconds]" );
77 // Wait 5 seconds before retrying
78 delay( 5000 );
79 }
80 }
81}
82 
83void callback( char* topic, byte* payload, unsigned int length ) {
84 Serial.print( "Received message on Topic: " );
85 Serial.println( topic );
86 
87 Serial.print("Msg:");
88 Serial.println((char *)payload);
89}
90 
91//------------------------------------------------------------------------------------------------
92 
93void setup()
94{
95 delay( 10000 );
96 Serial.begin( 115200 );
97 Serial.println("Setting up WIFI");
98 InitLWiFi();
99 Serial.println("WIFI setup done, setting up mqtt clinet");
100 client.setServer( mqttBroker, mqttPort );
101 client.setCallback( callback );
102 
103 lastSend = 0;
104 Serial.println("Setup done");
105}
106 
107void loop()
108{
109 if( !client.connected() ) {
110 reconnect();
111 }
112 client.loop();
113}

 

2 Comments

Add a Comment

Your email address will not be published. Required fields are marked *