-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
41 lines (38 loc) · 943 Bytes
/
client.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "client.h"
#include <stdbool.h>
#include <stdio.h>
#include <mosquitto.h>
bool cli_init(struct cli* me, const char* brokerURL, int qos)
{
if (me) {
me->qos = qos;
me->mosq = mosquitto_new(NULL, true, me);
return (mosquitto_connect(me->mosq, brokerURL, 1883, 60) == MOSQ_ERR_SUCCESS);
} else {
return false;
}
}
void cli_cleanup(struct cli* me)
{
if (me) {
mosquitto_disconnect(me->mosq);
mosquitto_destroy(me->mosq);
}
}
bool cli_publish(struct cli* me, float temp)
{
if (me) {
#ifdef BINARY_TRANSFER
printf("%.1f\n", temp);
return (mosquitto_publish(me->mosq, NULL, "sensors/temperature", sizeof(temp), &temp, me->qos, false) == MOSQ_ERR_SUCCESS);
#else
char payload[8];
int len;
len = sprintf(payload, "%.1f", temp);
printf("%s\n", payload);
return (mosquitto_publish(me->mosq, NULL, "sensors/temperature", len, payload, me->qos, false) == MOSQ_ERR_SUCCESS);
#endif
} else {
return false;
}
}