📜 ⬆️ ⬇️

Building and using Mosquitto MQTT on Intel Edison


In this article, we will look at using Intel Edison's MQTT protocol. With it, you can receive data from sensors and transfer control to the actuators.
MQTT is a lightweight protocol used for communication between devices (M2M - machine-to-machine). It uses the publisher-subscriber model to transmit messages on top of the TCP / IP protocol. 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 that are unique to each of them. Actuators subscribe to messages they need to respond to. The MQTT broker will take care of forwarding messages from the publisher to the subscriber.

Example


Microcontroller A reads the value of the switch and sends its status with a message to the MQTT broker in the form of “switch = on”. Somewhere on the internet, microcontroller B is subscribed to the “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 state of the switch, and then turn the lamp on or off accordingly.



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 Mosquitto for Edison


It is assumed that the reader has already configured his Edison board and owns standard Linux operations.
Building a Mosquitto for Intel Edison is pretty simple:

1. Download mosquitto from mosquitto.org
$> wget http://mosquitto.org/files/source/mosquitto-1.3.5.tar.gz 


2. Unpack the archive
 $> tar xzf mosquitto-1.3.5 $> cd mosquitto-1.3.5 


3. Build
 $> make WITH_SRV=no 


4. Check and install the 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 Edison OS and therefore some of the later tests will not pass. However, the test in the test / broker folder does not use Python3 and covers all the basic operations of MQTT.

Testing the client and server for mosquitto


Edison OS is configured to start rsmb broker automatically. It uses standard TCP port 1883. First, we test the mosquitto client using a standard port. Later we will configure the mosquitto broker to test its operation on another port.
To test the client, open two ssh connections with Edison. In the first connection, start the mosquitto_sub client, which subscribes to the “test” topic of the rsmb-broker working locally on Edison.



In the second ssh connection, post the message “Hello World!” In the topic “test” on the same local server.
You should see a message in the window where the mosquitto_sub program is running. Note that the mosquitto_sub client will run 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 a mosquitto broker on another port, for example, 1993.



Use the commands with the –p 1993 parameter for mosquitto_sub and mosquitto_pub to test the broker on port 1993.





Additionally, you can view the documentation for Mosquitto .

Source: https://habr.com/ru/post/275083/


All Articles