One of the main features of an “object” in the Internet of Things world is its ability to exchange messages, sending data or receiving commands, as effectively and efficiently as possible. There are several standard protocols for this purpose but one of my favorites is MQTT.

In this article, we will see how it is possible to use an MQTT client on the
Intel Galileo board with "Windows for IoT", so that we will add the required capacity in order to be recognized as a main part of the Internet of Things business.

Paho project : the MQTT client

is an open source project from Eclipse Foundation, which provides a range of clients, developed with different programming languages, for the main protocols used in the M2M (Machine To Machine) communication and IoT (Internet of Things) . In particular, our attention is focused on the MQTT client developed with ANSI C language that provides Posix/Windows support and that provides two types of APIs :

  • synchronous : all calls will block until the operation has completed;
  • asynchronous : the requested operation is started asynchronously by returning control to the caller immediately and all notifications are made ​​via callbacks (very useful in the context of software with a UI);

To build this project using Visual Studio 2013, we must use GIT to clone the corresponding repository whose URL is available in the section Downloads -> MQTT -> C / C ++ -> C (Posix / Windows).



In the “Windows Build” folder, we can find the “Paho C MQTT APIs” solution with following projects :


  • MQTTVersion : console application shows the current version of a specific library;
  • paho-mqtt3c, paho-mqtt3a : synchronous and asynchronous MQTT libraries without SSL support;
  • paho-mqtt3cs, paho-mqttas : synchronous and asynchronous MQTT libraries with SSL support;
  • stdoutsub, stdoutsuba : console applications provide synchronous and asynchronous subscribers;
  • test1, test3 : console applications for testing synchronous library without and with SSL;
  • test4, test5 : console applications for testing asynchronous library without and with SSL;




The solution is well structured but unfortunately it is necessary to make a lot of changes to the settings in order to compile the projects on Windows, then move on to execution on the Intel Galileo with "Windows for IoT."

Compiling the synchronous library

We focus on the library that provides the synchronous implementation (paho-mqtt3c) without SSL support in the Debug mode configuration. It compiles without any problem but for running on Intel Galileo it is necessary to disable the "enhanced instructions" due to an "illegal instruction" runtime error on
difftime function. This is possible in the project properties setting “No Enhanced Instructions (/arch:IA32)” in C/C++ –> Code Generation –> Enable Enhanced Instruction Set.





The main changes are needed on test1 project for the above library, since it doesn’t compile immediately due to a number of incorrect settings.

First, it fails because can’t include
MQTTClient.h (Cannot open include file: 'MQTTClient.h: No such file or directory) but we can fix it adding related folder in the project properties; we have to add the  $(SolutionDir)\..\src path in C/C++ –> General –> AdditionalIncludeDirectoris.





In addition, we must add the following define in C/C++ –> Preprocessor –> Preprocessor Definitions :


    • _WINDOWS : used to avoid the inclusion of some header files related to Linux and provides inclusion of header files related to WinSock;
    • _CRT_SECURE_NO_WARNINGS : the library uses the “unsafe” version of a lot of functions (for example, strtok instead of strtok_s);






At this point, the compiler is able to do its job but the Linker reports a series of "unresolved externals" errors, because it isn’t able to find the symbols relating to the WinSock and the MQTT libraries. In the case of the WinSock library, we have to add the reference to the ws2_32.lib library in Linker -> Input -> Additional Dependencies.




Regarding the MQTT library, we can directly referencing the project (because it is in the same solution) in Common Properties –> References –> Add New Reference.




With these changes the compilation goes successfully but unfortunately if we try to run the test  application on our PC, we find ourselves facing a "stack corruption" error. I promptly reported the bug (you can find details here) that with the current version has not yet fixed. The solution consists of a simple change in the file MQTTClient.c (function MQTTClient_deliverMessage) moving the following line of code:


ListRemove(m->c->messageQueue, m->c->messageQueue->first->content);


after #endif and MQTTPersistence_unpersistQueueEntry function call.

First test on PC

Before launching the execution of the test application on the Intel Galileo, we can ensure that everything is working properly by doing it on your own PC. First, we must choose an MQTT broker to be able to perform the tests (or use the default). I preferred to use the lightweight
Mosquitto broker and you can download it here. Let’s run the broker with the option "-v" in "verbose" mode so we can see all the debug messages during the test.
Before compiling and running the test1 application, we must change the options structure that contains, among other things, the address of which broker to connect (in my case, the IP address of my PC).


   1: struct Options
   2: {
   3:     char* connection;         /**< connection to system under test. */
   4:     char** haconnections;
   5:     int hacount;
   6:     int verbose;
   7:     int test_no;
   8:     int MQTTVersion;
   9:     int iterations;
  10: } options =
  11: {
  12:     "tcp://",
  13:     NULL,
  14:     0,
  15:     0,
  16:     0,
  18:     1,
  19: };


If everything is set up correctly, the test application will run without any problem.




MQTT on “Windows for IoT”


Upon verification that it works on the PC, we can move on to the configuration of the remote debugging to run the application on the Intel Galileo board directly from Visual Studio; needed settings are available on the official website of Windows On Devices.




Also in this case, launching the application in debug mode you should see all the messages that are exchanged with the board in the mosquitto console .

We have a MQTT client running on it !



      If you are curious about the output of the test application, we can see it using a Telnet session directly from the board. After execution, it shows a report with all passed tests.


      Obviously, any changes made ​​to projects related to the synchronous library , can also be applied to projects related to asynchronous library with no differences with the guarantees to have the same result.

      A video with the test on Intel Galileo is also available on my YouTube channel.