Building and Using Mosquitto MQTT on Intel Edison
In this article, we will look at using the MQTT protocol for Intel Edison. With it, you can receive data from sensors and transfer control to actuators.
MQTT is a lightweight protocol used for communication between devices (M2M - machine-to-machine). It uses a publisher-subscriber model to send messages over TCP / IP. The central part of the MQTT protocol is the MQTT server or broker that has access to the publisher and subscriber. Using MQTT, you can build a network of sensors, where they publish their data in the form of messages unique to each of them. Actuators subscribe to messages that they need to respond to. The MQTT broker will take care of redirecting messages from the publisher to the subscriber.
Microcontroller A reads the value of the switch and sends its state to a MQTT broker in the form of “switch = on”. Somewhere on the Internet, microcontroller B is subscribed to a “switch” message. If the user presses the switch, microcontroller A will send a message to the MQTT broker. The broker will forward the message to the list of subscribers. When microcontroller B receives a message, it can analyze the contents, determine the status of the switch, and then turn the lamp on or off, respectively.
More information on the MQTT protocol can be found at mqtt.org .
The Edison Yocto operating system comes with a small MQTT broker called RSMB (Really Small Message broker). Unfortunately, there is no built-in MQTT client to test all this. In this article, we will look at how to build another MQTT broker - Mosquitto.
Building a Mosquitto for Edison
It is assumed that the reader has already set up his Edison board and owns standard Linux operations.
Building Mosquitto for Intel Edison is quite simple:
1. Download mosquitto from mosquitto.org
$> wget http://mosquitto.org/files/source/mosquitto-1.3.5.tar.gz
2. Unzip the archive
$> tar xzf mosquitto-1.3.5 $> cd mosquitto-1.3.5
$> make WITH_SRV=no
4. Check and install compiled mosquito
# Create user mosquitto $> add user mosquitto # Test $> cd test/broker $> make test $> cd ../../ # Install $> cp client/mosquitto_pub /usr/bin $> cp client/mosquitto_sub /usr/bin $> cp lib/libmosquitto.so.1 /usr/lib $> cp src/mosquitto /usr/bin
There are tests in the main mosquitto folder. Unfortunately, most of them require Python3, which is not available on the Edison OS and therefore some of the later tests will fail. However, the test in the test / broker folder does not use Python3 and covers all basic MQTT operations.
Client and server testing for mosquitto
Edison OS is configured to automatically launch the rsmb broker. It uses the standard 1883 TCP port. First we test the mosquitto client using the standard port. Later, we will configure the mosquitto broker to check if it works on another port.
To test the client, open two ssh connections to Edison. In the first connection, run the mosquitto_sub client, which subscribes to the “test” topic of the rsmb broker working locally on Edison.
In the second ssh connection, publish the message “Hello World!” In the topic “test” on the same local server.
You should see a message in the window where mosquitto_sub is running. Note that the mosquitto_sub client will work continuously and continue to receive new messages from the server until it is stopped.
The top test shows that the mosquitto_sub and mosquitto_pub clients we built work correctly with the local rsmb MQTT server.
Now we can run a similar test using the mosquitto broker on another port, for example, 1993.
Using the commands with the –p 1993 parameter for mosquitto_sub and mosquitto_pub, test the broker on port 1993.
Additionally, see the Mosquitto documentation .