MQTT C Client library

Eclipse Paho CMQTT C Client Library - 图1 (opens new window) and Eclipse Paho Embedded CMQTT C Client Library - 图2 (opens new window) are all client libraries in C language (MQTT C Client) under the Eclipse Paho project, and are full-featured MQTT clients written in ANSI C.

Eclipse Paho Embedded C can be used on the desktop operating system, but mainly for embedded environments such as mbedMQTT C Client Library - 图3 (opens new window), ArduinoMQTT C Client Library - 图4 (opens new window) and FreeRTOSMQTT C Client Library - 图5 (opens new window) .

The client has synchronous/asynchronous APIs, which start with MQTTClient and MQTTAsync:

  • The synchronous API is designed to be simpler and more useful and some calls will be blocked until the operation is completed, which is easier for programming;
  • There is only one calling block API-waitForCompletion in the asynchronous API, which is notified through the callback, and is more suitable for the non-main thread environment.

Paho C Usage example

For detailed descriptions of the comparison, download, and usage of the two MQTT client libraries related to the C language, please move to the project homepage to view. This example contains the complete code of the Paho C in C language connecting to the EMQX Broker, sending and receiving messages:

  1. #include "stdio.h"
  2. #include "stdlib.h"
  3. #include "string.h"
  4. #include "MQTTClient.h"
  5. #define ADDRESS "tcp://broker.emqx.io:1883"
  6. #define CLIENTID "emqx_test"
  7. #define TOPIC "testtopic/1"
  8. #define PAYLOAD "Hello World!"
  9. #define QOS 1
  10. #define TIMEOUT 10000L
  11. int main(int argc, char* argv[])
  12. {
  13. MQTTClient client;
  14. MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
  15. MQTTClient_message pubmsg = MQTTClient_message_initializer;
  16. MQTTClient_deliveryToken token;
  17. int rc;
  18. MQTTClient_create(&client, ADDRESS, CLIENTID,
  19. MQTTCLIENT_PERSISTENCE_NONE, NULL);
  20. // MQTT Connection parameters
  21. conn_opts.keepAliveInterval = 20;
  22. conn_opts.cleansession = 1;
  23. if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
  24. {
  25. printf("Failed to connect, return code %d\n", rc);
  26. exit(-1);
  27. }
  28. // Publish message
  29. pubmsg.payload = PAYLOAD;
  30. pubmsg.payloadlen = strlen(PAYLOAD);
  31. pubmsg.qos = QOS;
  32. pubmsg.retained = 0;
  33. MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token);
  34. printf("Waiting for up to %d seconds for publication of %s\n"
  35. "on topic %s for client with ClientID: %s\n",
  36. (int)(TIMEOUT/1000), PAYLOAD, TOPIC, CLIENTID);
  37. rc = MQTTClient_waitForCompletion(client, token, TIMEOUT);
  38. printf("Message with delivery token %d delivered\n", token);
  39. // Disconnect
  40. MQTTClient_disconnect(client, 10000);
  41. MQTTClient_destroy(&client);
  42. return rc;
  43. }

Paho C MQTT 5.0 support

Paho C has fully supported MQTT 5.0 Currently.