Embedded MQTT C/C++ Client
The "full" Paho MQTT C client library was written with Linux and Windows in mind. It assumes the existence of Posix or Windows libraries for networking (sockets), threads and memory allocation. This library is intended to have these characteristics:
- use very limited resources - pick and choose the components needed
- not reliant on any particular libraries for networking, threading or memory management
- ANSI standard C for maximum portability, at the lowest level
- optional higher layer(s) in C and/or C++.
The library can be used on desktop operating systems, but is primarily aimed for environments such as mbed and FreeRTOS. This code is intended to be used in the smallest pieces needed for the particular embedded system.
A prebuilt Arduino client is available. To use download and in the Arduino IDE use Sketch -> Import Library... -> Add Library... with the downloaded client zip file. A sample sketch demonstrating the client is available in the in the git repository in MQTTClient/samples/arduino/Hello
Building from source
Samples and tests can be built with "build" shell scripts in their respective directories
Here is the core of a simple publishing program:
MQTTPacket_connectData data = MQTTPacket_connectData_initializer; int rc = 0; char buf; MQTTString topicString = MQTTString_initializer; char* payload = "mypayload"; int payloadlen = strlen(payload);int buflen = sizeof(buf); data.clientID.cstring = "me"; data.keepAliveInterval = 20; data.cleansession = 1; len = MQTTSerialize_connect(buf, buflen, &data); /* 1 */ topicString.cstring = "mytopic"; len += MQTTSerialize_publish(buf + len, buflen - len, 0, 0, 0, 0, topicString, payload, payloadlen); /* 2 */ len += MQTTSerialize_disconnect(buf + len, buflen - len); /* 3 */ rc = Socket_new("127.0.0.1", 1883, &mysock); rc = write(mysock, buf, len); rc = close(mysock);