Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / IoT

Building and running Mosquitto MQTT on Intel Edison

5.00/5 (2 votes)
8 May 2015CPOL5 min read 13.2K  
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.

This article is for our sponsors at CodeProject. These articles are intended to provide you with information on products and services that we consider useful and of value to developers

Get access to the new Intel® IoT Developer Kit, a complete hardware and software solution that allows developers to create exciting new solutions with the Intel® Galileo and Intel® Edison boards. Visit the Intel® Developer Zone for IoT.

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
    $> wget http://mosquitto.org/files/source/mosquitto-1.3.5.tar.gz
  2. Extract the archive
    $> tar xzf mosquitto-1.3.5
    $> cd mosquitto-1.3.5
  3. Build
    $> make WITH_SRV=no
  4. Test and install your mosquitto compilation
    # 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 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_sub client 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 the mosquitto_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.

References

Mosquitto documentation can be found here http://mosquitto.org/documentation

Details on the Intel Edison board can be found here: http://www.intel.com/edison

Intel® Developer Zone for IoT

Start inventing today with the Intel® IoT Developer Program which offers knowledge, tools, kits and a community of experts to quickly and easily turn your innovative ideas into IoT Solutions.

Dream it, Build it with the Intel® IoT Developer Kit for Intel® Edison and Intel® Galileo platforms. These kits are versatile, performance-optimized and fully integrated end-to-end IoT solutions supporting a variety of programming environments, tools, security, cloud connectivity and hardware.

For more resources and to learn how the new Intel® IoT Developer Kit v1.0 can help streamline your IoT projects:

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)