Compare commits

...

10 Commits

Author SHA1 Message Date
9e22556d36 fix logging 2020-12-26 22:08:59 +01:00
1ac129a027 add -Wall and -Werror 2020-11-20 11:12:36 +01:00
44647d686f larger buffer 2020-11-15 01:46:41 +01:00
3d52c14d53 api docs 2020-11-13 14:55:49 +01:00
f27e286e57 api docs 2020-11-13 14:52:25 +01:00
dab7dd22a5 api docs 2020-11-13 14:49:28 +01:00
7497aa9921 change in comment in Makefile 2020-11-13 14:42:33 +01:00
931eddce41 readme changed 2020-11-13 14:41:08 +01:00
796b25b7ad readme changed 2020-11-13 14:38:01 +01:00
e4d9be0106 fix clientRead function 2020-11-13 14:13:43 +01:00
4 changed files with 87 additions and 6 deletions

View File

@@ -1,11 +1,10 @@
# Makefile - A simple client for MQTT in C
# Derived from the PubSubClient from Nick O'Leary
# Wolfgang Hottgenroth <woho@hottis.de>
# https://home.hottis.de/gitlab/wolutator/pubsubc
CFLAGS?=-mcpu=cortex-m3 -mthumb -Og -fdata-sections -ffunction-sections -g -gdwarf-2
CFLAGS?=-mcpu=cortex-m3 -mthumb -Og -fdata-sections -ffunction-sections -g -gdwarf-2 -Wall -Werror
CC=arm-none-eabi-gcc
AR=arm-none-eabi-ar

View File

@@ -1,5 +1,85 @@
# pubsubc
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.
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.
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.
## API
The library provides the following functions:
### Initialization
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);
### Connect
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);
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);
### Disconnect
void mqttDisconnect(mqttClient_t *mqttClient);
### Publish
bool publish(mqttClient_t *mqttClient,
const char *topic,
const uint8_t *payload, uint16_t plength,
bool retained);
### Subscribe and Unsubscribe
bool subscribe(mqttClient_t *mqttClient,
const char *topic, uint8_t qos);
bool unsubscribe(mqttClient_t *mqttClient,
const char* topic);
### Loop
bool mqttLoop(mqttClient_t *mqttClient);
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
bool mqttConnected(mqttClient_t *mqttClient);

View File

@@ -46,7 +46,7 @@ void clientStop(client_t *client) {
if (res != SOCK_BUSY) {
close(client->sockNum);
logMsg("clientStop: disconnect returns 0x%02x, invalid response, ignore it", res);
logMsg("clientStop: disconnect returns %d, invalid response, ignore it", res);
} else {
bool successfullyClosed = false;
uint32_t startTime = HAL_GetTick();
@@ -72,7 +72,7 @@ int clientRead(client_t *client) {
int res = -1;
if (clientAvailable(client) >= 1) {
uint8_t buf;
int32_t res = recv(client->sockNum, &buf, 1);
res = recv(client->sockNum, &buf, 1);
if (res == 1) {
res = (int) buf;
}
@@ -99,4 +99,6 @@ bool clientConnected(client_t *client) {
uint32_t millis() {
return HAL_GetTick();
}
}

View File

@@ -22,7 +22,7 @@
// MQTT_MAX_PACKET_SIZE : Maximum packet size. Override with setBufferSize().
#ifndef MQTT_MAX_PACKET_SIZE
#define MQTT_MAX_PACKET_SIZE 256
#define MQTT_MAX_PACKET_SIZE 512
#endif
// MQTT_KEEPALIVE : keepAlive interval in Seconds. Override with setKeepAlive()