-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt.c
70 lines (57 loc) · 1.89 KB
/
mqtt.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//
// Created by davi on 11/7/17.
//
#include <string.h>
#include <stdlib.h>
#include "mqtt.h"
#include "debug.h"
#include "config.h"
MQTTClient client;
void setup_mqtt() {
MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
int rc;
if (NULL != client) {
MQTTClient_disconnect(client, 10000);
MQTTClient_destroy(&client);
}
TRACE("Conectando em %s...\n", BROKER);
MQTTClient_create(&client, BROKER, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL);
conn_opts.keepAliveInterval = 600;
conn_opts.cleansession = 1;
if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS) {
printf("Failed to connect, return code %d\n", rc);
terminate = 1;
exit(-1);
}
conexao_mqtt = 1;
}
void mqtt() {
setup_mqtt();
}
void mqtt_pub_double(char *topic, double measure) {
char m[5];
TRACE("Convertendo medida [%.1f] para string...\n", measure);
snprintf(m, 5, "%.1f", measure);
mqtt_pub(topic, m);
}
void mqtt_pub(char *topic, char *measure) {
MQTTClient_message pubmsg = MQTTClient_message_initializer;
MQTTClient_deliveryToken token;
char *dstTopic;
size_t strSize = sizeof(char) * (strlen(ROOT_TOPIC) + strlen(topic) + 1);
dstTopic = (char *) malloc(strSize);
bzero(dstTopic, strSize);
strcpy(dstTopic, ROOT_TOPIC);
strcat(dstTopic, topic);
TRACE("Publicando [%s] em [%s]...\n", measure, dstTopic);
pubmsg.payload = measure;
pubmsg.payloadlen = sizeof(measure);
pubmsg.qos = QOS;
pubmsg.retained = 0;
MQTTClient_publishMessage(client, dstTopic, &pubmsg, &token);
TRACE("Waiting for up to %d seconds for publication of [%s]"
"on topic %s for client with ClientID: %s\n",
(int) (TIMEOUT / 1000), pubmsg.payload, dstTopic, CLIENTID);
MQTTClient_waitForCompletion(client, token, TIMEOUT);
free(dstTopic);
}