IoT – MQTT Publish and Subscriber C Code

With the buzz around the Internet-of-Things (IoT), I felt I needed to get in on the act. For those that follow my twitter feed (@feabhas) you may be aware of the “home project” I’ve been working on. This project is based around the mbed platform to which I have connected a DS18B20 temperature sensor. The overall goal is to record the water temperature of my son’s fish tank, however due to water quality issues, it is currently sampling the air temperature outside my house.

An interesting part of the project is looking into various solutions to push out the current temperature. Using the mbed LPC1768 Workshop Development Board gives me easy access to an ethernet port and thanks to the mbed community there is an off-the-shelf library for socket programming. Once you have sockets, the options suddenly open up.


Inspired by Andy Stanford-Clark and his house that twitters, I first implemented an MQTT based publisher. Even though there was an MQTT library available I really wanted to understand the protocol. As part of my learning process I downloaded the open source MQTT broker called mosquitto and developed both a simple C based publisher and subscriber on the Mac rather than the mbed (as the mbed socket library doesn’t quite follow the standard socket programming interface). With Wireshark and the existing lightweight C client library published on google code as a reference point I have implemented a simple set of files that demonstrate the principles of the MQTT publish-subscribe model. Current the code only works to “QoS-0”, but I intend to add further Quality-of-Service levels.

The code can be found on github here and is designed to be build using CMake.

Niall Cooling

Director at Feabhas Limited
Co-Founder and Director of Feabhas since 1995.
Niall has been designing and programming embedded systems for over 30 years. He has worked in different sectors, including aerospace, telecomms, government and banking.
His current interest lie in IoT Security and Agile for Embedded Systems.

Latest posts by Niall Cooling (see all)

Posted on April 21st, 2012
» Feed to this thread
» Trackback

3 Comments a “IoT – MQTT Publish and Subscriber C Code”

  1. Andy Stanford-Clark says:

    LOL – like the new rendering of my surname!

  2. Niall Cooling says:

    Opps, sorry :-)

  3. Sticky Bits » Blog Archive » Native C/C++ Application development for the mbed using CMSIS-DAP says:

    […] you have been following the Feabhas blog for some time, you may remember that in April of last year I posted about my experiences of using the MQTT protocol. The demonstration code was ran the ARM Cortex-M3 based mbed […]

Leave a Reply

%d bloggers like this: