-
Notifications
You must be signed in to change notification settings - Fork 1
/
mqtt.cpp
37 lines (33 loc) · 955 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
35
36
#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.print("Attempting MQTT connection with client_id=");
Serial.print(client_id);
Serial.println("...");
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(1000);
}
}
Serial.println("done.");
return reconnected;
}
void maintain_subscriptions(MQTTClient &mqtt, const std::vector<const char*> &topics) {
for (auto topic : topics) {
mqtt.subscribe(topic);
}
}
}