@@ -11,6 +11,12 @@ class EasyMqtt : public MqttMap {
11
11
PubSubClient mqttClient;
12
12
13
13
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 ;
14
20
const char * mqtt_username = " N/A" ;
15
21
const char * mqtt_password = " N/A" ;
16
22
@@ -22,8 +28,35 @@ class EasyMqtt : public MqttMap {
22
28
/* *
23
29
Handle connections to mqtt
24
30
*/
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);
27
60
if (mqttClient.connect (deviceId.c_str (), mqtt_username, mqtt_password)) {
28
61
debug (" Connected to MQTT" );
29
62
subscribe ();
@@ -65,25 +98,19 @@ class EasyMqtt : public MqttMap {
65
98
Setup connection to wifi
66
99
*/
67
100
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;
85
103
86
104
// 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
+ };
87
114
get (" system" )[" wifi" ][" rssi" ] << []() {
88
115
return String (WiFi.RSSI ());
89
116
};
@@ -103,7 +130,9 @@ class EasyMqtt : public MqttMap {
103
130
mqttClient.setCallback ([&](const char * topic, uint8_t * payload, unsigned int length) {
104
131
callback (topic, payload, length);
105
132
});
106
- mqttClient.setServer (host, port);
133
+
134
+ mqtt_host = host;
135
+ mqtt_port = port;
107
136
mqtt_username = username;
108
137
mqtt_password = password;
109
138
@@ -117,7 +146,7 @@ class EasyMqtt : public MqttMap {
117
146
Handle the normal loop
118
147
*/
119
148
void loop () {
120
- mqttReconnect ();
149
+ reconnect ();
121
150
mqttClient.loop ();
122
151
MqttMap::loop ();
123
152
}
0 commit comments