Skip to content

Commit 4a37ad0

Browse files
committed
Add wifi reconnect support
1 parent 85c65f8 commit 4a37ad0

File tree

1 file changed

+50
-21
lines changed

1 file changed

+50
-21
lines changed

src/EasyMqtt.h

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ class EasyMqtt : public MqttMap {
1111
PubSubClient mqttClient;
1212

1313
String deviceId = "deviceId";
14+
15+
const char* wifi_ssid = "N/A";
16+
const char* wifi_password = "N/A";
17+
18+
const char* mqtt_host = "N/A";
19+
int mqtt_port = 1883;
1420
const char* mqtt_username = "N/A";
1521
const char* mqtt_password = "N/A";
1622

@@ -22,8 +28,35 @@ class EasyMqtt : public MqttMap {
2228
/**
2329
Handle connections to mqtt
2430
*/
25-
void mqttReconnect() {
26-
while (!mqttClient.connected()) {
31+
void reconnect() {
32+
if(WiFi.status() == WL_DISCONNECTED) {
33+
WiFi.mode(WIFI_STA);
34+
WiFi.begin(wifi_ssid, wifi_password);
35+
36+
int timer = 0;
37+
while ((WiFi.status() == WL_DISCONNECTED) && timer < 60) {
38+
delay(500);
39+
#ifdef DEBUG
40+
Serial.print(".");
41+
#endif
42+
timer++;
43+
}
44+
if(WiFi.status() == WL_CONNECTED) {
45+
#ifdef DEBUG
46+
Serial.println("WiFi connected");
47+
Serial.print(" * IP address: ");
48+
Serial.println(WiFi.localIP());
49+
#endif
50+
} else {
51+
#ifdef DEBUG
52+
Serial.println("WiFi Unable to connect");
53+
#endif
54+
delay(5000);
55+
}
56+
57+
}
58+
if (WiFi.status() == WL_CONNECTED && !mqttClient.connected()) {
59+
mqttClient.setServer(host, port);
2760
if (mqttClient.connect(deviceId.c_str(), mqtt_username, mqtt_password)) {
2861
debug("Connected to MQTT");
2962
subscribe();
@@ -65,25 +98,19 @@ class EasyMqtt : public MqttMap {
6598
Setup connection to wifi
6699
*/
67100
void wifi(const char* ssid, const char* password) {
68-
WiFi.mode(WIFI_STA);
69-
WiFi.begin(ssid, password);
70-
71-
while (WiFi.status() != WL_CONNECTED) {
72-
delay(500);
73-
#ifdef DEBUG
74-
Serial.print(".");
75-
#endif
76-
}
77-
#ifdef DEBUG
78-
Serial.println("WiFi connected");
79-
Serial.print("IP address: ");
80-
Serial.println(WiFi.localIP());
81-
82-
Serial.print("Chip ID : ");
83-
Serial.println(ESP.getChipId());
84-
#endif
101+
wifi_ssid = ssid;
102+
wifi_password = password;
85103

86104
// Setup wifi diag
105+
get("system")["wifi"]["quality"] << []() {
106+
if(WiFi.RSSI() <= -100) {
107+
return String(0);
108+
} else if(WiFi.RSSI() >= -50) {{
109+
return String(100);
110+
} else {
111+
return String(2 * (WiFi.RSSI() + 100));
112+
}
113+
};
87114
get("system")["wifi"]["rssi"] << []() {
88115
return String(WiFi.RSSI());
89116
};
@@ -103,7 +130,9 @@ class EasyMqtt : public MqttMap {
103130
mqttClient.setCallback([&](const char* topic, uint8_t* payload, unsigned int length) {
104131
callback(topic, payload, length);
105132
});
106-
mqttClient.setServer(host, port);
133+
134+
mqtt_host = host;
135+
mqtt_port = port;
107136
mqtt_username = username;
108137
mqtt_password = password;
109138

@@ -117,7 +146,7 @@ class EasyMqtt : public MqttMap {
117146
Handle the normal loop
118147
*/
119148
void loop() {
120-
mqttReconnect();
149+
reconnect();
121150
mqttClient.loop();
122151
MqttMap::loop();
123152
}

0 commit comments

Comments
 (0)