Building and running Mosquitto MQTT on Intel Edison
Introduction
One of the big feature of the Intel(R) Edison board is that it's not just an Arduino* compatible development board but it is also a Linux* single board computer that can provide full access to the underlying Linux capabilities to an Arduino sketch.
In this series of blogs, I will explore the various ways that the Linux capability can be integrated into an Arduino sketch, and how to leverage existing code, to make IoT development much simpler.
We will start with building a simple sensor node that contains a temperature sensor, light sensor, and LED and a switch. We will then publish the data to the Internet using MQTT. Demonstrate some rudimentary data analytics using Node-Red to trigger events and send command back to our sensor node.
The Linux distribution that came with the Edison board is derived from the Yocto project,http://www.yoctoproject.org. Yocto is a small Linux kernel designed for embedded systems with limited resource.
MQTT
MQTT is a light-weight protocol used for Machine to Machine (M2M) communication. MQTT used a publish/subscribe message forwarding model built on top of TCP/IP protocol. Central to the MQTT protocol is an MQTT server or broker that is accessible to both publishers and subscribers. Using MQTT, one can build a sensor network where various sensors can publish their sensor values in the form of a message unique to each sensor. Actuators can subscribe to different messages that they can act upon. The MQTT broker will take care of forwarding messages from publishers to subscribers.
Example:
Microcontroller A can read the state of a switch and publish the state of the switch as a message in the form "switch = on" to an MQTT server. Microcontroller B somewhere on the internet subscribed to the MQTT message "switch". Now whenever a user pushes the switch, microcontroller A will publish a message to the MQTT broker. The broker will forward the message to a list of subscribers. When microcontroller B receives the message, it can parse the content, determine the state of the switch and turn on or off a light accordingly.
More details on MQTT can be found at http://mqtt.org.
The Edison Yocto OS came with a small MQTT broker called RSMB (Really Small Message broker). Unfortunately, there isn't a bundled MQTT client for the purpose of testing. In this article, we will build another MQTT broker, Mosquitto, mostly for the clients. In subsequent articles, we will use these clients to connect to our Arduino sketches.
Building Mosquitto on Edison
It is assumed that readers have already setup their Edison board and are familiar with standard Linux operations.
Building Mosquitto is fairly straight forward. Here are the steps to build Mosquitto on Edison:
1. Download mosquitto from mosquitto.org
1 $> wget http://mosquitto.org/files/source/mosquitto-1.3.5.tar.gz
2. Extract the archive
1 $> tar xzf mosquitto-1.3.5
2 $> cd mosquitto-1.3.5
3. Build
1 $> make WITH_SRV=no
4. Test and install your mosquitto compilation
01 # Create user mosquitto
02 $> add user mosquitto
03
04 # Test
05 $> cd test/broker
06 $> make test
07 $> cd ../../
08
09 # Install
10 $> cp client/mosquitto_pub /usr/bin
11 $> cp client/mosquitto_sub /usr/bin
12 $> cp lib/libmosquitto.so.1 /usr/lib
13 $> cp src/mosquitto /usr/bin
There is also a test target in the mosquitto root directory. This target, however, requires Python3 which isn't available onthe Edison OS and some of the latter tests will fail. The target in test/broker does not require Python3 and will cover all the essential MQTT operations.
Test your mosquitto client and server
The Edison OS was configured to start the rsmb broker automatically. So the default TCP port 1883 is already used by rsmb. We'll test the mosquitto clients against the rsmb broker using the default MQTT port. Later on, we'll configure the mosquitto broker to run on a different port and test that as well.
To test the client, open two ssh connections to the Edison. In the first connection, run a mosquitto_subclient that subscribe to a topic called 'test' on the rsmb broker running locally on the Edison
In the second ssh connection, publish a message 'Hello World!' to the topic test on the same local server.
You should see the messages in the window running the mosquitto_sub program. Note that themosquitto_sub client is persistent and will continue to listen to new messages from the server until it is terminated.
Above test showed that the mosquitto_sub and mosquitto_pub clients that we built are working properly with the local rsmb MQTT server.
We can run the same test using the mosquitto broker that we built running on a different port (1993)
Using the same command as above but with the -p 1993 argument with mosquitto_sub and mosquitto_pub, test the mosquitto broker on port 1993.
Summary
We've built and ran mosquitto server and clients on the Intel Edison. In subsequent postings, I will start to develop Arduino sketches that leverage the MQTT clients that we've just built.
For more such intel IoT resources and tools from Intel, please visit the Intel® Developer Zone
Source: https://software.intel.com/en-us/blogs/2015/02/20/building-and-running-mosquitto-mqtt-on-intel-edison/