2016年4月27日 星期三

MQTT + LinkIt ONE

MQTT協定

參考此網址
http://www.instructables.com/id/LinkIt-One-MQTT-First-Step-to-IoT/?ALLSTEPS#step1

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)

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

Picture of Installing MQTT Lens
IMG_20151110_180643.jpg
Screenshot from 2015-11-08 14:45:04.png
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


沒有留言:

張貼留言