Introduction: LinkIt One + MQTT = First Step to IoT
LinkIt One is a great development board for IoT, especially because it comes packed with WiFi, BLE, GSM, GPRS, GPS etc., My view of IoT is that there will be many connected devices sending small drips of data to a central server which crunches the mathematics of making knowledge out of this data. So after the device, the next important thing in IoT is the communication protocol used in the system. You need a light weight and highly reliable communication protocol for such robust systems. MQTT makes a perfect candidate for being the language of IoT. For those who are new to IoT, I would suggest you to read my introductory blog to IoT here.
Eclipse foundation has chosen MQTT as one their supported protocols for IoT and many other giant players like IBM Bluemix, Amazon AWS IoT, Microsoft Azure are also supporting MQTT in their IoT offerings. What this all means is MQTT is the language of IoT.
Sorry I sounded a bit technical above, but this instructable is all about how to use MQTT with LinkIt One.
Things Required
LinkIt One board
A lot of enthusiasm ;)
Step 1: Install MQTT Library in Arduino
First thing you require is to install an MQTT library. In this instructable, I will be using the PubSub client by knollery.
There is also an AWS IoT officially supported library, but i found it a little hard for the beginners to use. So I decided to use PubSub client library, which is well known in arduino circles :)
To install library
1. Goto https://github.com/knolleary/pubsubclient and download the zip file.
2. Now open your arduino IDE : Sketch -> Include Library -> Add ZIP library
3. Now browse to the downloaded ZIP file and click OK.
This will install 'PubSub' library in your arduino IDE.
Step 2: Sketch You LinkIt One
Once you installed the library, you will be able to compile the sketch I attached in this step.
This sketch read the analog pins and publishes it to network every 5 seconds.
Some important things to remember while using this sketch:
1. Change WifI settings in the sketch to match to your WiFI settings
2. Change the client name to some unique name. Otherwise you may have conflict if you are using a public broker.
3. Sketch publishes a message to "outTopic" once connected(and reconnection) and subscribe to "inTopic".
4. Analog pin readings are published on topic "analogData".
5. Change the broker settings as required. You can either use a public broker such as iot.eclipse.org:1883 or setup your own (see step 3)
Attachments
Step 3: Installing a Local Broker
In case you don't want to use a public broker, you can install one of the many broker softwares available to your local PC and use it. Don't forget to configure this in your sketch.
I recommend using mosquitto broker. You can go to http://mosquitto.org/ and get a binary installation for your OS.
Important things to notice are:
1. By default, MQTT brokers runs at port 1883. This is what is configured in the sketch. In case you want to change it, update the sketch also.
2. You can change these settings in mosquitto.conf file
Step 4: Installing MQTT Lens
So great!! If all the above steps went well, you would be successfully communicating with a broker by now and streaming your analog data. Now how I can view all these messages which are bouncing back and forth?? In this step, we will use MQTTLens to see all these communications.
MQTTLens is a chrome plugin. You can download it and add it to your chrome using the chrome store.
Once installed, connect to the same broker as your linkit one is connected to. Then subscribe to topics "inTopic", "outTopic", and "analogData". You can see the messages from your LinkIt One now. If you are lucky, you may be seeing some other maker's data who is trying this tutorial also. ;)
You can also publish to "inTopic" using MQTTLens. Then your linkit one will receive the message :)
With that I'm winding up this instuctable.
Happy Making,
vish
3 Comments
7 years ago
Hi,
Great post.
This is a request for guidance.
My
ESP8266 module connects to “test.mosquitto.org” and subscribes to a
topic say “b1”. What ever is published to “b1”, it can read it
immediately.
Problem happens when my second ESP8266 module subscribes to “test.mosquitto.org” to “b1”. It continuously returns :
WiFi connected
IP address:
192.168.0.9
Attempting MQTT connection…connected
Message arrived [b1] 0
Attempting MQTT connection…connected
Message arrived [b1] 0
Attempting MQTT connection…connected
Message arrived [b1] 0
Attempting MQTT connection…connected
Could you kindly suggest what might be going wrong. I look forward to your advice.
Reply 5 years ago
You must compile separate code for each of your ESP8266 modules ensuring the client name (Line 45: if ( client.connect("LinkIt One Client") ) { // Better use some random name) is different for each client. MQTT Brokers require a unique name for each client.
7 years ago
If you have a problem MQTT not connect, you must be check MQTT version in PubSubClient.h .
Please check v.3.1 or v.3.1.1
// MQTT_VERSION : Pick the version
#define MQTT_VERSION MQTT_VERSION_3_1
//#define MQTT_VERSION MQTT_VERSION_3_1_1
Good luck.