-
Notifications
You must be signed in to change notification settings - Fork 1
/
mqttthread.cpp
163 lines (140 loc) · 4.49 KB
/
mqttthread.cpp
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
this thread handles mqtt messages
*/
#include <thread>
#include <unistd.h>
#include "mqttthread.h"
#include "fifo.h"
#include "config.h"
bool MQTTTHREAD::connected = false;
MQTTTHREAD::MQTTTHREAD()
{
}
bool MQTTTHREAD::init()
{
cleanup();
getMQTTtopic();
mosquitto_lib_init();
mqttclient = mosquitto_new("Pylonmonitor", true, NULL);
if (!mqttclient)
{
printf("Failed to create MQTT client\n");
mqttclient = nullptr;
return false;
}
if(brokerusername.length() > 0) {
printf("set username and password\n");
// Set username and password for authentication
int set_credentials = mosquitto_username_pw_set(mqttclient, brokerusername.c_str(), brokerpassword.c_str());
if (set_credentials != MOSQ_ERR_SUCCESS)
{
printf("Failed to set MQTT username and password\n");
mosquitto_destroy(mqttclient);
mqttclient = nullptr;
return false;
}
}
// set callbacks
mosquitto_connect_callback_set(mqttclient, MQTTTHREAD::on_connect);
mosquitto_message_callback_set(mqttclient, MQTTTHREAD::on_message);
mosquitto_disconnect_callback_set(mqttclient, MQTTTHREAD::on_disconnect);
// connect to the MQTT broker
string BROKER_IP = "1.2.3.4";
if(mqttBrokerIP.length() >= 7) {
BROKER_IP = mqttBrokerIP;
}
printf("connecting to Broker: %s:%d\n",BROKER_IP.c_str(), BROKER_PORT);
int rc = mosquitto_connect(mqttclient, BROKER_IP.c_str(), BROKER_PORT, 20);
if (rc != MOSQ_ERR_SUCCESS)
{
printf("Failed to connect to MQTT Broker: %s:%d, return code %d\n", BROKER_IP.c_str(), BROKER_PORT,rc);
mosquitto_destroy(mqttclient);
mqttclient = nullptr;
return false;
}
printf("connected to Broker: %s:%d\n",BROKER_IP.c_str(), BROKER_PORT);
// start the network loop thread
rc = mosquitto_loop_start(mqttclient);
if (rc != MOSQ_ERR_SUCCESS)
{
printf("Failed to start MQTT network loop thread, return code %d\n", rc);
mosquitto_disconnect(mqttclient);
mosquitto_destroy(mqttclient);
mqttclient = nullptr;
return false;
}
return true;
}
MQTTTHREAD::~MQTTTHREAD()
{
cleanup();
}
void MQTTTHREAD::cleanup()
{
if (mqttclient != nullptr) {
printf("MQTT cleanup\n");
mosquitto_disconnect(mqttclient);
mosquitto_loop_stop(mqttclient, true);
mosquitto_destroy(mqttclient);
mosquitto_lib_cleanup();
mqttclient = nullptr; // Set to nullptr after cleanup
}
}
bool MQTTTHREAD::isConnected()
{
return connected;
}
void MQTTTHREAD::on_connect(struct mosquitto *mosq, void *userdata, int result)
{
if (result == 0) {
printf("connected to MQTT Broker\n");
connected = true;
/* printf("subscribe to topics\n");
int ret = mosquitto_subscribe(mqttclient, NULL, topic.c_str(), QOS);
if(ret)
{
printf("ERROR subscribing to topic: %s errocode:%d\n",topic.c_str(),ret);
}*/
}
else
{
printf("Failed to connect to MQTT Broker, return code %d\n", result);
}
}
// receive a message from the MQTT broker
void MQTTTHREAD::on_message(struct mosquitto *mosq, void *userdata, const struct mosquitto_message *message)
{
// If message is null, return
if(message == nullptr) return;
if(message->topic == nullptr) return;
string topic = string(message->topic);
if(message->payload != nullptr && message->payloadlen > 0) {
string payload = string(static_cast<const char*>(message->payload), message->payloadlen);
}
//do something with topic and payload
}
void MQTTTHREAD::on_disconnect(struct mosquitto *mosq, void *userdata, int rc)
{
printf("\nDisconnected from MQTT Broker. Reconnect ...\n");
connected = false;
}
void MQTTTHREAD::publish()
{
string topic;
string payload;
bool dataavail = read_from_readbatt(topic, payload);
if(dataavail) {
//printf("MQTT publish <%s>,<%s>\n",topic.c_str(),payload.c_str());
int ret = 0;
if(payload.length() == 0)
ret = mosquitto_publish(mqttclient, NULL, topic.c_str(), 0, NULL, 0, false);
else
ret = mosquitto_publish(mqttclient, NULL, topic.c_str(), payload.length(), payload.c_str(), 0, false);
if (ret != MOSQ_ERR_SUCCESS)
{
//printf("Failed to publish message <%s>. Return code: %d\n", topic.c_str(), ret);
connected = false;
return;
}
}
}