-
Notifications
You must be signed in to change notification settings - Fork 1
/
mqtt.cpp
35 lines (31 loc) · 884 Bytes
/
mqtt.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
#include "mqtt.h"
namespace mqtt {
bool maintain_connection(MQTTClient &mqtt, const char* client_id, const std::vector<const char*> &topics) {
if (mqtt.connected())
return false;
bool reconnected = false;
unsigned int count = 0;
while (!mqtt.connected()) {
Serial.println("Attempting MQTT connection...");
if (mqtt.connect(client_id)) {
Serial.println();
Serial.println("connected");
maintain_subscriptions(mqtt, topics);
reconnected = true;
} else {
count++;
Serial.print(".");
Serial.print(count);
Serial.print(".");
delay(5000);
}
}
Serial.println("done.");
return reconnected;
}
void maintain_subscriptions(MQTTClient &mqtt, const std::vector<const char*> &topics) {
for (auto topic : topics) {
mqtt.subscribe(topic);
}
}
}