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.

Mbed

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
Dislike (1)
Website | + posts

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.

About Niall Cooling

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.
This entry was posted in C/C++ Programming and tagged , , , . Bookmark the permalink.

5 Responses to IoT – MQTT Publish and Subscriber C Code

  1. Andy Stanford-Clark says:

    LOL - like the new rendering of my surname!

    Like (1)
    Dislike (0)
  2. Opps, sorry 🙂

    Like (0)
    Dislike (0)
  3. Pingback: Sticky Bits » Blog Archive » Native C/C++ Application development for the mbed using CMSIS-DAP

  4. kamal says:

    Hi Niall, I thank you for sharing the code. I am trying to upload the code into STM32L4 Arm processor board and connect to MQTT cloud.

    Like (0)
    Dislike (0)
  5. You do appreciate this code was written back in the very early days of MQTT back in 2012? I haven't tried it recently.

    Like (1)
    Dislike (0)

Leave a Reply