Introduction: MQTT for Dummies

Picture of MQTT for Dummies

Hallo Liebe folk!

I would like to start like "Once upon a time, when i was a kit" but unfortunately it's not a cool story about me. It is about MQTT, but no worries it is also cool!

Currently i was hearing a lot about MQTT and i checked it to figure out what it is. Then i got that it is really cool messaging protocol for IoT, M2M such applications. So, i thougth perhaps to use it also shows me a bit cool. Then i wanted to make some apps about it. There was a lot of topics, documents about it on the internet, but i couldn't find enough and clear applications about it. So that's why i wanted make somethings with my Arduino MKR1000 and NodeMCU boards. Lets talk about it!

Step 1: What Is MQTT

"MQTT is a machine-to-machine (M2M)/"Internet of Things" connectivity protocol. It was designed as an extremely lightweight publish/subscribe messaging transport. It is useful for connections with remote locations where a small code footprint is required and/or network bandwidth is at a premium. For example, it has been used in sensors communicating to a broker via satellite link, over occasional dial-up connections with healthcare providers, and in a range of home automation and small device scenarios. It is also ideal for mobile applications because of its small size, low power usage, minimised data packets, and efficient distribution of information to one or many receivers"

It is a official description of MQTT from http://mqtt.org. So you can communicate your devices using MQTT with many advantages. Of course you can use other protocols such as ZigBee6LowPAN, etc. But since MQTT works with TCP/IP you could implement it for any hardware which has ethernet, wifi, gsm etc. kind of hardwares. So its well fitted for lot of applications.

Step 2: How Does It Work?

Picture of How Does It Work?

As you see at the picture to explain easily how it does work, i made simple application with two boards and a computer(Ubuntu).

There are two main parts in MQTT. One of them is mqtt broker and other one is mqtt client. Broker like a governor. It manages all clients so network. Clients also can publish some messages under topics or could subscribe already published topics. So clients could be publisher, subscriber or both. To make it easy understandable i made Arduino as publisher and NodeMCU subscriber. Of course my ubuntu pc is broker.

As you see in the picture Arduino measures 3 LM35 temperature sensors and send values to broker as topics. So it publishes 3 topics(3 sensor values), then NodeMCU subscribes these topics and get these temperature values. Scenario is simple!

Step 3: MQTT Library and Broker

Picture of MQTT Library and Broker

To make MQTT application you need a Broker. Broker is also program which runs under some hardwares such Raspberry pi, PC etc. Of course to make it logical we should make it run under with cheapest hardware! So I also searched a bit on the net to use NodeMCU, Arduino kind boards or Android phones as broker but now it isn't possible yet. For this application i used my Ubuntu installed computer to run Broker. So i am also not so logical person.

Next question is which broker program should we use? So again i searched a bit then i have noticed most of people use Mosquitto! Mosquitto is broker program for MQTT applications. It also has client program. So you can test it or emulate some MQTT applications only using your computer with mosquitto. As you see on the picture Mosquitto is just console program. Run it and leave alone. It would start to manage MQTT network. As you remember it is TCP/IP based. So default port is for MQTT usually 1883, perhaps some times it could be 1884. Also dont forget your IP address of pc whic runs Mosquitto. We need it to connect clients to Broker.

For Arduino boards i found some mqtt libraries then i have decided to use arduino-mqtt library. Because it worked well with both of my boards.(Arduino and NodeMCU) But i couldn't install it with library manager. That's why i installed it as zip file. So i think i explained everything. If somethings missing or less ask me or google it please. Let's talk about aplication codes.

Step 4: Arduino(Publisher) and NodeMCU(Subscriber) Code

Picture of Arduino(Publisher) and NodeMCU(Subscriber) Code

Since NodeMCU also supported by Arduino community i used same library at all. So i just changed a bit codes to make it publisher or subscriber. Also i put some comments to explain it more.

<p>/*****************************************************************************************<br>* Author:      Erhan YILMAZ                                                                                  *
* Application: MQTT Test application(Publisher)                                                  *
* Date :       02.08.2016                                                                                        *
* Version :    1.0                                                                                                    *
* Description: It is a publisher part of a mqtt test application                               *
* which is works with arduino mkr1000.                                                               *
* It gets 3 channels temp values with LM35 sensors then send values               *
* as topics to subscribers.                                                                                    *
* As broker mosquitto used on Ubuntu based pc.                                                *
* MQTT deneme uygulaması                                                                               *
* Gilberto Conti tarafından yazılan MQTT kütüphanesi kullanılmıştır.                 *
* <a href="https://github.com/256dpi/arduino-mqtt"> https://github.com/256dpi/arduino-mqtt </a> buradan kütüphaneye erişilebilir.        *
* Uygulama MQTT client olarak çalışıp LM35 sıcaklık sensörlerinden ölçtüğü   *
* değerleri topicler halinde yayınlamaktadır(Publish).                                         *
*****************************************************************************************</p><p>/ Libraries
#include <spi.h>
#include <wifi101.h>
#include <mqttclient.h></mqttclient.h></wifi101.h></spi.h></p><p>// Wifi info
char *ssid = "SSID_NAME";
char *pass = "SSID_PASS";</p><p>WiFiClient net;
MQTTClient client;</p><p>unsigned long lastMillis = 0;</p><p>// Setup for used modules/libraries(ADC,Uart,MQTT,WIFI)
void setup()
{
  Serial.begin(115200);
  analogReference(AR_INTERNAL1V0);
  analogReadResolution(12);
  WiFi.begin(ssid, pass);
  // Use your Broker ip address
  client.begin("192.168.1.8", net); // Begin client with broker Ip and wifi client</p><p>  connect();
}</p><p>void connect()
{
  // Connect to wifi
  Serial.print("checking wifi...");
  while (WiFi.status() != WL_CONNECTED)
  {
    Serial.print(".");
    delay(1000);
  }
  
  // Connect to broker
  Serial.print("\nconnecting...");
  while (!client.connect("arduinoPub", "try", "try"))
  {
    Serial.print(".");
    delay(1000);
  }</p><p>  Serial.println("\nconnected!");</p><p>  // client.subscribe("/example");    // No subscription for any topic
  // client.unsubscribe("/example");  // No unsubscription aswell
}</p><p>void loop() 
{
  char temp[50];</p><p>  client.loop();</p><p>  if(!client.connected()) 
  {
    connect();
  }</p><p>  // publish a messages roughly every 5 seconds.
  if(millis() - lastMillis > 5000) 
  {
    lastMillis = millis();
    int sensorValue = analogRead(A0);
    float temperature = sensorValue / 40.96;
    sprintf(temp,"%.2f°C", temperature);
    
    // /sensors/temp1 topic for temp sensor 1
    client.publish("/sensors/temp1", temp);
    
    sensorValue = analogRead(A1);
    temperature = sensorValue / 40.96;
    sprintf(temp,"%.2f°C", temperature);
    // /sensors/temp2 topic for temp sensor 2
    client.publish("/sensors/temp2", temp);</p><p>    sensorValue = analogRead(A2);
    temperature = sensorValue / 40.96;
    sprintf(temp,"%.2f°C", temperature);
    // /sensors/temp3 topic for temp sensor 3
    client.publish("/sensors/temp3", temp);
  }
}</p><p>void messageReceived(String topic, String payload, char * bytes, unsigned int length) 
{
  // Callback function for subscriptions
}</p>

As you see in the code Arduino connects wifi and make some setup then publish tempereture values as topic those called as /sensors/temp1, /sensors/temp2 and /sensors/temp3 in everey 5 seconds approximately. That's it!

<p>/****************************************************************************<br>* Author:      Erhan YILMAZ                                                 *
* Application: MQTT Test application(Subscriber)                            *
* Date :       02.08.2016                                                   *
* Version :    1.0                                                          *
* Description: It is a subscriber part of a mqtt test application           *
* which is works with NodeMCU(ESP8266).                                     *
* It subscribes to "/sensors/#" topic to get all messages under /sensors/   *
* As broker mosquitto used on Ubuntu based pc.                              *
* MQTT deneme uygulaması                                                    *
* Gilberto Conti tarafından yazılan MQTT kütüphanesi kullanılmıştır.        *
* <a href="https://github.com/256dpi/arduino-mqtt"> https://github.com/256dpi/arduino-mqtt </a> buradan kütüphaneye erişilebilir.  *
* Uygulama MQTT client olarak çalışıp /sensors/ altında gelen topiclerde ki *                    
* mesajları almaktadır(Subscribe). Alınan mesajları uarttan pcye gönderir.	*
****************************************************************************</p><p>/ Libraries
#include <esp8266wifi.h>
#include <mqttclient.h></mqttclient.h></esp8266wifi.h></p><p>// Wifi info
const char *ssid = "SSID_NAME";
const char *pass = "SSID_PASS";</p><p>WiFiClient net;
MQTTClient client;</p><p>unsigned long lastMillis = 0;</p><p>// Setup for used modules/libraries(Uart,MQTT,WIFI)
void setup()
{
  Serial.begin(115200);
  WiFi.begin(ssid, pass);
  // Use your Broker ip address
  client.begin("192.168.1.8", net); // Begin client with broker Ip and wifi client</p><p>  connect();
}</p><p>void connect()
{
  // Connect to wifi
  Serial.print("checking wifi...");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(1000);
  }</p><p>  // Connect to broker
  Serial.print("\nconnecting...");
  while (!client.connect("ESP8266Sub", "try", "try")) {
    Serial.print(".");
    delay(1000);
  }</p><p>  Serial.println("\nconnected!");</p><p>  client.subscribe("/sensors/#");   // Subscription for sensors
  // client.unsubscribe("/example");
}</p><p>// Main loop
void loop() {
  client.loop();
  delay(10); // <- fixes some issues with WiFi stability
}</p><p>// Send incoming topic messages over uart
void messageReceived(String topic, String payload, char * bytes, unsigned int length) {
  Serial.print("incoming: ");
  Serial.print(topic);
  Serial.print(" - ");
  Serial.print(payload);
  Serial.println();
}</p>

As you also see NodeMCU code is also smilar to Arduino code. After initialization it subscribes to all topics under the name of /sesnsors by texting "/sensors/#" # is a one kind of syntax of MQTT. So /sensors/# mean is i wanna subscribes all topic under the name of /sensors so /sensors/temp1, /sensors/temp2 and /sensors/temp3. There are few more syntax about mqtt. You could check it on the internet. It is enough for our application.

Anyways when NodeMCU get some messages it sends it to computer over uart. You can see the running of the application on the picture.

Step 5: Conclusion

As result as we ended up such a nice application. Thanks Allah. You can get all files about application here as github repo. if somethings is not clear, missing or wrong don't hesitate to write down. Also i am always open to hear some feed backs etc. Thanks for your attention. See you on next post. Cheers...

Erhan

Comments

artmez (author)2017-06-15

The article's embedded code appears to have misplaced html escape sequences? The GitHub code does not.

mosix (author)2016-08-03

many thanks! very useful

engineerhan (author)mosix2016-08-21

Thanks mosix! It is nice to hear that.

siddhc (author)2016-08-20

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.

engineerhan (author)siddhc2016-08-21

Hi thanks,

It does matter which library you used and your code of course. So also it could be about broker. Perhpas its too busy. To be sure about that you could make your phone as router and connect all clients and broker to your phone.

Step by step. From part to whole!

Enjoy...

leseagle (author)2016-08-05

I am looking for a program, hardware to connect my iPhone or all smartphones to control my wheel chair. It only has 4 wires coming from the drive port. I have a Pine 64 mini computer with BlueTooth. I am a novice but want to me this work... could you give me any input from a lay persons understanding as to my next steps.

Leseagle

engineerhan (author)leseagle2016-08-21

Hi!

I dont know your skills but to develop easy android apps you could use app inventor (http://appinventor.mit.edu/explore ) from mit. So then you need to underdtand that you chairs is able to hack it! Then you need some motor driver shield, arduino, bluetooth module and lot of free time to waste it.

Never give up!

Enjoy!