pubsubc/README.md

86 lines
2.5 KiB
Markdown
Raw Normal View History

2020-11-13 14:41:08 +01:00
# pubsubc
2020-11-13 14:38:01 +01:00
This is a minimal MQTT client library in C.
It comes with an adaption layer for the ioLibrary_Driver for the
WizNet chips which can be found here https://github.com/Wiznet/ioLibrary_Driver.
You find a fork with a Makefile a STMCubeMX generated project here
https://home.hottis.de/gitlab/wolutator/ioLibrary_Driver/-/tree/WolfgangsOwnBranch.
Using this adaption layer you should find it easy to adjust it for
other platforms.
2020-11-12 16:51:32 +01:00
This work is directly derived from the famous PubSubClient library
of Nick O'Leary, which can be found at https://pubsubclient.knolleary.net/
It is most or less a plain C rewrite.
2020-11-13 14:38:01 +01:00
All honour for this working MQTT client library goes to Nick O'Leary,
all blame for bugs in the C port shall go to me.
2020-11-13 14:49:28 +01:00
## API
The library provides the following functions:
2020-11-13 14:52:25 +01:00
### Initialization
2020-11-13 14:49:28 +01:00
void mqttClientInit(mqttClient_t *mqttClient, client_t *client, callback_t callback);
Initializes the MQTT client, handover a client handle according to the definition
from ``platformAdaption.h`` and a callback function for incoming messages.
Implement this callback function for incoming messages with this footprint:
typedef void (*callback_t)(char*, uint8_t*, uint16_t);
2020-11-13 14:52:25 +01:00
### Connect
2020-11-13 14:49:28 +01:00
bool mqttConnect(mqttClient_t *mqttClient,
uint8_t *address, uint16_t port,
const char *id,
const char *user, const char *pass,
const char *willTopic, uint8_t willQos,
bool willRetain, const char *willMessage,
bool cleanSession);
2020-11-13 14:55:49 +01:00
In the original C++ implementation multiple variants of the ``connect``method are available
with less and lesser arguments. Unfortunately, in C this is no option. If you don't care
about authentication and the whole will stuff you can call it
mqttConnect(&mqttClient, brokerAddress, brokerPort, clientId, NULL, NULL, NULL, 0, false, NULL, false);
2020-11-13 14:49:28 +01:00
2020-11-13 14:52:25 +01:00
### Disconnect
2020-11-13 14:49:28 +01:00
void mqttDisconnect(mqttClient_t *mqttClient);
2020-11-13 14:52:25 +01:00
### Publish
2020-11-13 14:49:28 +01:00
bool publish(mqttClient_t *mqttClient,
const char *topic,
const uint8_t *payload, uint16_t plength,
bool retained);
2020-11-13 14:52:25 +01:00
### Subscribe and Unsubscribe
2020-11-13 14:49:28 +01:00
bool subscribe(mqttClient_t *mqttClient,
const char *topic, uint8_t qos);
bool unsubscribe(mqttClient_t *mqttClient,
const char* topic);
2020-11-13 14:52:25 +01:00
### Loop
2020-11-13 14:49:28 +01:00
bool mqttLoop(mqttClient_t *mqttClient);
2020-11-13 14:52:25 +01:00
Call this function in your idle loop. For my own part, I've implemented a time-triggered system
and call it with a period of 100ms.
### Connection status
2020-11-13 14:49:28 +01:00
bool mqttConnected(mqttClient_t *mqttClient);