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)
2 | #include <LWiFiClient.h> |
5 | #include <PubSubClient.h> |
9 | #define WIFI_PASSWORD "*****" |
10 | #define WIFI_AUTH LWIFI_WPA |
11 | #define mqttUser "****" |
14 | LWiFiClient wifiClient; |
16 | uint8_t macAddressBin[VM_WLAN_WNDRV_MAC_ADDRESS_LEN] = { 0 }; |
17 | char macAddressStr [ 20 ]; |
20 | char mqttBroker[] = "******" ; |
23 | char mqttClientPrefix[] = "LinkitOne01" ; |
25 | PubSubClient client( wifiClient ); |
28 | char inTopic[] = "/node/sms/#" ; |
34 | unsigned long lastSend; |
42 | Serial.println( "Connecting to AP: " + String(WIFI_AP)); |
43 | while ( 0 == LWiFi.connect(WIFI_AP, LWiFiLoginInfo(WIFI_AUTH, WIFI_PASSWORD))) { |
46 | myIP = LWiFi.localIP(); |
47 | Serial.println( "Connected" ); |
48 | Serial.print( "IP Address: " ); |
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); |
60 | while (!client.connected()) { |
61 | if (LWiFi.status()==LWIFI_STATUS_DISCONNECTED){ |
62 | Serial.println( "Connecting to wifi again" ); |
66 | Serial.println( "Connecting to MQTT broker: " + String(mqttBroker)); |
68 | if ( client.connect(mqttClientId,mqttUser,mqttPass) ) { |
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 ); |
74 | Serial.print( "[FAILED] [ rc = " ); |
75 | Serial.print( client.state() ); |
76 | Serial.println( " : retrying in 5 seconds]" ); |
83 | void callback( char * topic, byte * payload, unsigned int length ) { |
84 | Serial.print( "Received message on Topic: " ); |
85 | Serial.println( topic ); |
88 | Serial.println(( char *)payload); |
96 | Serial.begin( 115200 ); |
97 | Serial.println( "Setting up WIFI" ); |
99 | Serial.println( "WIFI setup done, setting up mqtt clinet" ); |
100 | client.setServer( mqttBroker, mqttPort ); |
101 | client.setCallback( callback ); |
104 | Serial.println( "Setup done" ); |
109 | if ( !client.connected() ) { |
If I want to use Linkit 7697 connect to a MQTT broker , which code should I change?
i have tried but it is not working..
what should I change.