MQTT


Deprecated This team is no longer maintained, please use: https://github.com/ARMmbed/mbed-mqtt instead.

Using MQTT

What is MQTT?

MQTT is a pub/sub architecture that is optimized for realtime embedded devices. It enables one to many communication in a trivially easy fashion. For an in-depth explanation of what MQTT is and how it works please see https://github.com/mqtt/mqtt.github.io/wiki .

How to use it?

To use MQTT you must have two things.

  1. A Broker
  2. A Client

The MQTT Broker is a server that takes care of distributing messages to everyone and keeping everyone up to date. The MQTT client is a piece of software that publishes and subscribes to topics. The MQTT library is a client library that enables mbed devices to use the mqtt protocol. This can be seen in the mqtt hello world example.

MQTT Broker

There are many options for an MQTT broker out there. You can install your own locally, or use a private one in the cloud. If you want to quickly test your mqtt client implementation you can either use one of the many public mqtt test servers or install one locall. For online public brokers check out this list. For a local broker check out mosquitto.

On windows you will need to install the dependencies for mosquitto, make sure to read the readme_windows.txt file in the mosquitto install directory. For OSX/Linux you should be able to run the following command at your terminal to start up a mosquito broker with no security on port 1883

$ mosquitto

All connections will be reported to the terminal window. If you are connecting multiple devices make sure to change the client ID's, conflicting ID's are not allowed.

MQTT Client

A MQTT client publishes and subscribes to channels. Below you will find both a mbed mqtt client and a python client. Try sending messages back and forth from the mbed to the python client.

mbed client

Import programHelloMQTT

Sample MQTT program - simple send and receive

The mbed mqtt library that is used as part of the above example program can be used with any transport layer to use mqtt on mbed. Go give the example a try, make sure to change the broker address and URL address to the broker of your choice.

python client

If you are having trouble with your MQTT broker you can try running an mqtt client in python to validate the broker. Follow these steps to install a python mqtt client. 1) Install the paho-mqtt python client (make sure you have python 2.7.9 or greater installed)

$ pip install paho-mqtt

2) Save the following python code to mqttclient.py . Make sure to change the broker field to the correct broker you are looking at and the topic to the topic you are interested in.

Python-MQTT-Client

import paho.mqtt.client as paho
mqttc = paho.Client()
 
# Settings for connection
host = "test.mosquitto.org"
topic= "mbed-sample/#"
port = 1883

# Callbacks
def on_connect(mosq, obj, rc):
    print("connect rc: "+str(rc))
    mqttc.publish("mbed-sample","Python Script Test Message.");
 
def on_message(mosq, obj, msg):
    print( "Received on topic: " + msg.topic + " Message: "+str(msg.payload) + "\n");
 
def on_subscribe(mosq, obj, mid, granted_qos):
    print("Subscribed OK")
 
# Set callbacks
mqttc.on_message = on_message
mqttc.on_connect = on_connect
mqttc.on_subscribe = on_subscribe
 
# Connect and subscribe
print("Connecting to " +host +"/" +topic)
mqttc.connect(host, port, 60)
mqttc.subscribe(topic, 0)
 
# Wait forever, receiving messages
rc = 0
while rc == 0:
    rc = mqttc.loop()
 
print("rc: "+str(rc))
 

The python script will report back all messages published to that topic. By pointing the python mqtt client and the mbed mqtt client at the same topic on the same broker you should be able to view all messages being exchanged in that topic.


All wikipages