There are a number of excellent open source MQTT (Message Queue Telemetry Transport) brokers or servers that are available. Eclipse Mosquitto is a very popular light weight server that can be run on lower end hardware such as Raspberry Pi modules.
An MQTT project will have the following components:
- MQTT Broker. The Mosquitto Server that manages and distributes messages
- MQTT Publisher. A source for the data, such as a Arduino or Raspberry Pi
- MQTT Subscriber. An application that wants the data
There is a good selection of MQTT libraries and test utilities for MQTT publishers and subscribers.
To install the Mosquitto broker and test utilities on a Raspberry Pi or Debian system enter:
sudo apt-get update sudo apt-get install mosquitto mosquitto-clients
For Windows and other systems see the Mosquitto download documentation.
The features of the mosquitto broker are defined by the mosquitto configuration file (/etc/mosquitto/mosquitto.conf). For simple testing you can leave the configuration as is, however if you need to any added features like bridging of MQTT brokers, adding diagnostics or setting up MQTT for web access then you’ll need to edit mosquitto.conf.
For my project I wanted to add diagnostic (with a 10 second update), and I wanted MQTT to also run on Websockets. To do this I edited the config file by:
sudo nano /etc/mosquitto/mosquitto.conf
then I added the lines:
listener 8080 protocol websockets sys_interval 10
The websockets can be set to any port number (9001, 8000,8080 etc.). After the file has been modified you’ll need to restart the Mosquitto service:
sudo service mosquitto restart
To test that your Mosquitto MQTT broker is running you can open 2 terminal sessions and run the mosquitto_sub and mosquitto_pub utilities.
In the first session we start mosquitto_sub with it subscribing to a topic called mytopic1:
~$ mosquitto_sub -t mytopic1 value1 1234
In the second session we use mosquitto_pub and we can publish some values to the topic mytopic1:
~$ mosquitto_pub -t mytopic1 -m "value1" ~$ mosquitto_pub -t mytopic1 -m 1234
If everything is working correctly then the published values should appear in the subscribing session.
The Mosquitto Diagnostics have a topic of : $SYS/broker/… Some of these diagnostics include:
where + = 1min, 5min or 15min averages
For a full description of the Mosquitto diagnostics see: https://mosquitto.org/man/mosquitto-8.html .
This library is very similiar to Python Paho MQTT library, and it supports both subscribe and publish functions.
By subscribing to $SYS/broker/# we can get see all the different diagnostic values.
Mosquitto is an excellent MQTT broker that can be run on both high end servers and low end hardware like Raspberry Pi’s.
With the default MQTT broker it is important to point out that a MQTT subscriber will only see new values. So if you do not publish values periodically a newly connected client will start with no values.