MQTT
is extremely lightweight pubish subscribe
messaging protocol that transport message between device
over the TCP/IP that run over port 1883 for plain MQTT and 8883 for MQTT over TLS
. It is desiggn for bidirectional connection between remotely placed device
(specialy used for iOT devices) with small code footprint
.
- MQTT Client: An MQTT client is any device (from a micro controller up to a full-fledged server) that runs an MQTT library and connects to an MQTT broker over a network. MQTT client can be Publisher or Subscriber.
- MQTT Publisher: MQTT Client that publishes the message to topic on an
MQTT Broker
. - MQTT Subscriber: MQTT Client that subscribe or lister for message on topic from
MQTT Broker
- MQTT Broket: An MQTT broker is a server that receives all messages from the clients
[Publisher]
and then routes the messages to the appropriate destination clients[Subscriber]
.
MQTT clients are very small, require minimal resources so can be used on small microcontrollers. MQTT message headers are small to optimize network bandwidth.
MQTT allows for messaging between device to cloud and cloud to device. This makes for easy broadcasting messages to groups of things.
MQTT can scale to connect with millions of IoT devices.
Reliability of message delivery is important for many IoT use cases. This is why MQTT has 3 defined quality of service levels: 0 - at most once, 1- at least once, 2 - exactly once
Many IoT devices connect over unreliable cellular networks. MQTT’s support for persistent sessions reduces the time to reconnect the client with the broker.
MQTT makes it easy to encrypt messages using TLS and authenticate clients using modern authentication protocols, such as OAuth.
There are lots of MQTT Broker available, but we are going to use Eclipse Mosquitto
.
Eclipse Mosquitto is an open source (EPL/EDL licensed) message broker that implements the MQTT protocol versions 5.0, 3.1.1 and 3.1. Mosquitto is lightweight and is suitable for use on all devices from low power single board computers to full servers.
The MQTT protocol provides a lightweight method of carrying out messaging using a publish/subscribe model. This makes it suitable for Internet of Things messaging such as with low power sensors or mobile devices such as phones, embedded computers or microcontrollers.
The Mosquitto project also provides a C library for implementing MQTT clients, and the very popular mosquitto_pub and mosquitto_sub command line MQTT clients.
For Mac OSX
brew install mosquitto
Start MQTT broker on lunch
brew services start mosquitto
Restart / stop MQTT Broker
brew services restart mosquitto
brew services stop mosquitto
MQTT Configuration file
cd /usr/local/etc/mosquitto/mosquitto.conf
mosquitto_sub -h [host] - t [topic]
mosquitto_sub -h localhost - t greeetings
mosquitto_pub -h [host] -t [topic] -m [message]
mosquitto_pub -h localhost -t greeetings -m "Hello world MQTT"
We can use -d
flag to see the debug message while sending the mqtt message
mosquitto_pub -h localhost -t greeetings -m "Hello world MQTT" -d
//Debug message
Client mosq-ZaL9mzWA7K78ubUN2a sending CONNECT
Client mosq-ZaL9mzWA7K78ubUN2a received CONNACK (0)
Client mosq-ZaL9mzWA7K78ubUN2a sending PUBLISH (d0, q0, r0, m1, 'greetings', ... (16 bytes))
Client mosq-ZaL9mzWA7K78ubUN2a sending DISCONNECT
By default mqtt broke allow anonymous user to receive the message being published. These settings are configured in mosquitto.cong
as
allow_anonymous true
But Mosquitto broker can be configured to allow client authentication using username and password before connection is performed. The username and password is transmitted in plain text unless we use secure transport encryption.
Configuring user authentication
-
Method 1: From text file.
- Create a file auth.txt
nano auth.txt
- Enter username and password seperated by colon.
rajan:password123
- To create additional user enter username and password in new line.
- Now encrypt the auth.txt file before we actually use it with
mosquitto_passwd
utilitymosquitto_passwd -U auth.txt
- This will update the plain text auth.txt and encrypt the password
rajan:$6$dtMF49Z2e8YSrYEY$1nAtPhU4V+d8dj/3glka5H7VrQBf/IZ2WFhJDQI5CGpbK6soFrwk0cciOf0pioSvkTsiZ9rD24pOZTPpYxq1Kg==
- Create a file auth.txt
-
Method 2: Direct Method.
- Use mosquitto_passwd to generate encrypted username and password file
mosquitto_passwd -c auth.text rajan
- This will create a auth.txt file with username = rajan and ask for pasword to enter.
- Use mosquitto_passwd to generate encrypted username and password file
In mac you will need to copy the password file auth.txt
to the directory where mosquitto.conf
exists. Normaly it under /usr/local/etc/mosquitto
. We then need to change two setting in mosquitto.conf file.
- Don't allow anonymous to connect
allow_anonymous false
- Set the password file to use auth.txt
password_file /usr/local/etc/mosquitto/auth.txt
- and restart the mqtt broker
brew services restart mosquitto
From now on we need to use username and password to be able to connect to MQTT Broker. Otherwise we will get Error: Connection refused
//To subscribe
mosquitto_sub -h localhost -u "rajan" -P "password123" -t "greetings"
//To Publish
mosquitto_pub -h localhost -t "greetings" -m "Hello World MQTT" -u rajan -P password123 -d
Normally if a publisher publishes a message to a topic, and no one is subscribed to that topic the message is simply discarded by the broker. However the publisher can tell the broker to keep the last message on that topic by setting the retained message flag.
Without retained messages the subscriber would have to wait for the status to change before it received a message. However with the retained message the subscriber would see the current state of the sensor. What is important to understand is that only one message is retained per topic.
mosquitto_pub -h localhost -t "greetings" -m "Hello World MQTT" -u rajan -P password123 -d -r
Here -r
speficy tells the broker to retain the message.