-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMQTTManager.cpp
More file actions
266 lines (244 loc) · 8.2 KB
/
Copy pathMQTTManager.cpp
File metadata and controls
266 lines (244 loc) · 8.2 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include <Arduino.h>
#include "MQTTManager.h"
#include "ConnectionManager.h"
#include <WiFi.h>
#include <PubSubClient.h>
//Costruttori
MQTTManager::MQTTManager():
MQTTManager(true,
CONNECTION_LED_PIN,
WIFI_MAX_INITIAL_TIMEOUT,
WIFI_RECONNECT_INTERVAL,
WPS_BLINK_INTERVAL,
CONN_BUTTON_PIN,
CONN_BUTTON_PIN_MODE,
CONN_BUTTON_MODE) {}
MQTTManager::MQTTManager(bool autoReconnect,
byte connection_led_pin,
uint32_t wifi_max_initial_timeout,
uint32_t wifi_min_time_tra_connection,
uint32_t wps_blink_interval,
byte conn_button_pin,
byte conn_button_pin_mode,
byte conn_button_mode): ConnectionManager(autoReconnect,
connection_led_pin,
wifi_max_initial_timeout,
wifi_min_time_tra_connection,
wps_blink_interval,
conn_button_pin,
conn_button_pin_mode,
conn_button_mode)
{
MQTTIntervalTraConnetion = MQTT_RECONNECT_INTERVAL;
MQTTLastConnectionIstant = 0;
//intervalli per il reboot
RebootOnMQTTReconectionMaxTimes = REBOOT_ON_NOT_MQTT_RECONNECTION_FOR_RETRIES;
maxMQTTReconnetcRetries = MAX_MQTT_RECONNECTION_TIMES;
RebootOnMQTTReconectionMaxTime = REBOOT_ON_NOT_MQTT_RECONNECTION_FOR_TIME;
maxMQTTDisconnectedTime = MAX_MQTT_DISCONNECTED_TIME;
MQTTReconnectionTimes = 0;
lastMQTTConnectedIstant = 0;
_name = "";
MQTTServer = NULL;
MqttDisconnectedBlinkInterval = MQTT_DISCONNECTED_BLINK_INTERVAL;
topicsLen = 0;
}
String MQTTManager::getStringState() {
String tmp = ConnectionManager::getStringState();
if (tmp == "" || tmp == "CONNECTED") {
switch (_state) {
case MQTT_CONNECTED: return "MQTT_CONNECTED";
case MQTT_DISCONNECTED: return "MQTT_DISCONNECTED";
case MQTT_SUBSCRIBE_FAILED: return "MQTT_SUBSCRIBE_FAILED";
default: return String(_state);
}
} else {
return tmp;
}
}
//METODI
void MQTTManager::setMQTTServer(PubSubClient* server, String deviceName, const char* ipAddress, uint16_t port) {
if (server != NULL) {
_name = deviceName;
MQTTServer = server;
MQTTServer->setServer(ipAddress, port);
}
}
void MQTTManager::setMQTTServer(PubSubClient* server, String deviceName, uint8_t* ipAddress, uint16_t port) {
if (server != NULL) {
_name = deviceName;
MQTTServer = server;
MQTTServer->setServer(ipAddress, port);
}
}
void MQTTManager::setMQTTServer(PubSubClient* server, String deviceName, IPAddress ipAddress, uint16_t port) {
if (server != NULL) {
_name = deviceName;
MQTTServer = server;
MQTTServer->setServer(ipAddress, port);
}
}
void MQTTManager::setCallback(MQTT_CALLBACK_SIGNATURE) {
if (MQTTServer != NULL) {
MQTTServer->setCallback(callback);
}
}
void MQTTManager::setTopics(String* topics, uint16_t len) {
this->topics = topics;
topicsLen = len;
}
void MQTTManager::setDisconnectBlink(uint16_t interval) {
MqttDisconnectedBlinkInterval = interval;
}
void MQTTManager::setRebootOptions(bool forTime, bool forRetries, bool forMQTTTime, bool forMQTTRetries,
uint16_t max_time, uint32_t max_retries, uint16_t maxMQTTTtime, uint32_t maxMQTTRetries) {
ConnectionManager::setRebootOptions(forTime, forRetries, max_time, max_retries);
RebootOnMQTTReconectionMaxTimes = forMQTTRetries;
maxMQTTReconnetcRetries = maxMQTTRetries;
RebootOnMQTTReconectionMaxTime = forMQTTTime;
maxMQTTDisconnectedTime = maxMQTTTtime;
}
byte MQTTManager::startMQTT() {
if (MQTTServer != NULL) {
MQTTLastConnectionIstant = millis();
if (MQTTServer->connect(_name.c_str())) {
debugPort->println("[LOG]: Connected to MQTT server");
if (topicsLen != 0) {
subscribe(topics, topicsLen);
}
} else {
debugPort->println("[ERR]: Error connecting to MQTT server");
}
}
return _state;
}
void MQTTManager::subscribe(String topics[], uint16_t len) {
if (len > 0 && MQTTServer != NULL) {
for (int i = 0; i < len; i++) {
if (!MQTTServer->subscribe(topics[i].c_str())) {
_state = MQTT_SUBSCRIBE_FAILED;
return;
}
}
}
}
//Override methods
void MQTTManager::setHomepage() {
ConnectionManager::setHomepage();
}
void MQTTManager::startConnection(bool withWPS, bool tryReconnection) {
ConnectionManager::startConnection(withWPS, tryReconnection);
}
void MQTTManager::loop(bool withServer, bool withOTA ) {
ConnectionManager::loop(withServer, withOTA);
if (MQTTServer != NULL && MQTTServer->connected()) {
MQTTServer->loop();
}
}
//TO DO //gestisce lo stato e le riconnessioni, return: stato
void MQTTManager::connectionHandler() {
//Controllo di stato
if (MQTTServer != NULL) {
if (WiFi.status() == WL_CONNECTED && _state != MQTT_SUBSCRIBE_FAILED) {
if (millis() - MQTTLastConnectionIstant > 2000) { //Per evitare false connessioni
if (MQTTServer->connected()) {
_state = MQTT_CONNECTED;
} else if (!MQTTServer->connected()) {
_state = MQTT_DISCONNECTED;
}
}
}
if(WiFi.status() != WL_CONNECTED && _state == MQTT_CONNECTED){
_state = DISCONNECTED;
}
ConnectionManager::connectionHandler();
switch (_state) {
case MQTT_CONNECTED:
lastMQTTConnectedIstant = millis();
lastWiFiConnectedIstant = millis();
MQTTReconnectionTimes = 0;
if (!WPSConfigurated) {
debugPort->println("[LOG MQTT]: Enabling WPS after reconnetion!");
setupWPS();
}
break;
case MQTT_DISCONNECTED:
case MQTT_SUBSCRIBE_FAILED:
MQTTReconnect();
break;
}
}
}
void MQTTManager::connectionLedRoutine() { //TO DO //CONTROLLA IL LED
static uint32_t t1;
switch (_state) {
case NOT_CONNECTED:
case DISCONNECTED:
digitalWrite(ConnLedPin, LOW);
break;
case WPS_CONNECTION:
if (millis() > t1 + WPSBlinkInterval) {
t1 = millis();
digitalWrite(ConnLedPin, !digitalRead(ConnLedPin));
}
break;
case WPS_TIMEOUT:
case WPS_FAILED:
if (millis() < t1 + 2000) {
int dt = millis() - t1;
if (dt < 200) {
digitalWrite(ConnLedPin, HIGH);
} else if (200 <= dt && dt < 2 * 200) {
digitalWrite(ConnLedPin, LOW);
} else if (2 * 200 <= dt && dt < 3 * 200) {
digitalWrite(ConnLedPin, HIGH);
} else {
digitalWrite(ConnLedPin, LOW);
}
} else {
t1 = millis();
}
break;
case MQTT_CONNECTED:
digitalWrite(ConnLedPin, HIGH);
break;
case MQTT_DISCONNECTED:
case MQTT_SUBSCRIBE_FAILED:
if (millis() > t1 + MqttDisconnectedBlinkInterval) {
t1 = millis();
digitalWrite(ConnLedPin, !digitalRead(ConnLedPin));
}
break;
}
}
void MQTTManager::MQTTReconnect() {
if (MQTTServer != NULL && !MQTTServer->connected()) {
//Reboot for reconnection times
if (RebootOnMQTTReconectionMaxTimes && MQTTReconnectionTimes == maxMQTTReconnetcRetries) {
debugPort->println("[REBOOT]: for max MQTT reconnection times!!");
if (onRebootCallback != NULL) {
onRebootCallback();
delay(250);
}
ESP.restart();
}
//Reboot for max disconnected time
if (RebootOnMQTTReconectionMaxTime && millis() - lastMQTTConnectedIstant > maxMQTTDisconnectedTime) {
debugPort->println("[REBOOT]: for max MQTT disconnected time!!");
if (onRebootCallback != NULL) {
onRebootCallback();
delay(250);
}
ESP.restart();
}
if (millis() - MQTTLastConnectionIstant > MQTTIntervalTraConnetion) {
if (RebootOnMQTTReconectionMaxTimes) {
debugPort->printf("[LOG]: Tentativo di riconnessione al server MQTT n %d di %d...\n", MQTTReconnectionTimes + 1, maxMQTTReconnetcRetries);
} else {
debugPort->printf("[LOG]: Tentativo di riconnessione al server MQTT n %d...\n", MQTTReconnectionTimes + 1);
}
startMQTT();
MQTTReconnectionTimes++;
}
}
}