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 NodeMCUboards. Lets talk about it!

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.

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!

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 usearduino-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...

Read more »