Hello MQTT

For an upcoming project, I needed a light-weight publish-subscribe system, and I discovered MQTT

MQTT (Telemetry Transport) is a protocol designed for sending telemetry from devices in situations where bandwidth is very limited. Because of this, is an extremely light-weight protocol, and at a mere 42 pages the protocol specification is light-weight too.

 

The MQTT protocol is 10 years old now, and has been used in many ways.  It has found applications in healthcare, on Android phones and has recently been given a shot in the arm by the announcement that Facebook are using it in their group Messenger application.

 

My understanding is that the most popular open source broker for MQTT is Mosquitto, and there are clients available in many languages and for many platforms (Java for Android, Arduino, Python and Erlang).

Here is a brief guide to how I got started with MQTT under Ubuntu.

 

Mosquitto

Installing Mosquitto

Installing Mosquitto is very simple under Ubuntu, open a terminal and paste in the following 4 commands. The first adds the Mosquitto repository to your sources.list, the second updates your sources and the last 2 commands install Mosquitto, the Mosquito Python bindings and a couple of command line clients for testing your installation.

sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
sudo apt-get update 
sudo apt-get install mosquitto python-mosquitto
sudo apt-get install mosquitto-clients
       

Testing Mosquitto

Subscribing to a Topic

The broker should be up and running on port 1883, assuming that nothing else was running on that port. You will find out if it is running when you try the following tests. Open 2 terminals. In the first run this command:

mosquitto_sub -d -t hello/world

Here we are subscribing to all messages with the topic "hello/world". We are running in debug mode, so that of it fails we will know why.

Notice the "/" in the topic name. We can use this to create hierarchies of topics for example: "sport/rugby/munster".

If for some reason this command responds like the screenshot below, then it looks like the broker is not running:

MQTT Connection Refused

Try running the broker by simply typing mosquitto at the command line.

Publishing under a Topic

To publish a message under a specific topic you can run the following in your thus far unused terminal:

mosquitto_pub -d -t hello/world -m "Hello, MQTT. This is my first message."

This command should have published a message that will be pushed to all clients which have subscribed to the "hello/world" topic.

MQTT Message Received

Python Client

There is a Python wrapper for libmosquitto, which allows you to communicate with an MQTT broker from your python code.

Here is an example of publishing messages to a broker on the same host as the client.

import mosquitto
mqttc = mosquitto.Mosquitto("python_pub")
mqttc.will_set("/event/dropped", "Sorry, I seem to have died.")
mqttc.connect("127.0.0.1", 1883, 60, True)

mqttc.publish("hello/world", "Hello, World!")                                                                                                                               mqttc.publish(car1.device_id+"/init/1.0", "")

Comments

  • avatar

    Mario Medina

    Posted 1 month, 3 days ago.

    Good start point for running MQTT server. This was from great help!! I could verify that my server was working pretty well :)

  • avatar

    nasaa

    Posted 7 days ago.

    Thanks for putting this together. It works fine when I try to access it through the commandline on windows. When I try to access it using SAM_PHP by giving my IP address, I get connection refused. Could you tell me how to check the exact ip and port it is running on.

  • avatar

    Site Administrator

    Posted 4 days, 12 hours ago.

    Hi Nasaa, I think by default it should be listening on 1883, and all interfaces. Perhaps there is a firewall issue? I am not familiar with SAM_PHP