@@ -16,12 +16,13 @@ class EasyMqtt : public MqttEntry {
16
16
ConfigEntry* configEntry;
17
17
18
18
String deviceId = " deviceId" ;
19
+ long mqttDelay = 0 ;
19
20
20
21
protected:
21
22
/* *
22
23
Handle connections to mqtt
23
24
*/
24
- void connect () {
25
+ void connectWiFi () {
25
26
if (WiFi.status () == WL_DISCONNECTED) {
26
27
debug (" Connecting to wifi: " + config ().getString (" wifi.ssid" , " " ));
27
28
WiFi.mode (WIFI_STA);
@@ -33,10 +34,9 @@ class EasyMqtt : public MqttEntry {
33
34
34
35
int timer = 0 ;
35
36
while ((WiFi.status () == WL_DISCONNECTED) && timer < 50 ) {
36
- Serial. println ( WiFi.status ());
37
+ debug ( " Wifi Status " , String ( WiFi.status () ));
37
38
delay (500 );
38
39
timer++;
39
- // ToDo: handle timeout, and create AP
40
40
}
41
41
if (timer < 50 && WiFi.status () == WL_CONNECTED) {
42
42
debug (" WiFi connected" );
@@ -50,7 +50,10 @@ class EasyMqtt : public MqttEntry {
50
50
debug (" devideId" , deviceId);
51
51
webPortal.setup (*this );
52
52
}
53
- if (mqttClient.state () == MQTT_DISCONNECTED) {
53
+ }
54
+
55
+ void connectMqtt () {
56
+ if (!mqttClient.connected () && mqttDelay < millis ()) {
54
57
debug (" Configure MQTT" );
55
58
mqttClient.setClient (wifiClient);
56
59
mqttClient.setCallback ([&](const char * topic, uint8_t * payload, unsigned int length) {
@@ -59,8 +62,7 @@ class EasyMqtt : public MqttEntry {
59
62
});
60
63
});
61
64
mqttClient.setServer (config ().getString (" mqtt.host" , " " ).c_str (), config ().getInt (" mqtt.port" , 1883 ));
62
- }
63
- if (!mqttClient.connected ()) {
65
+
64
66
debug (" Connecting to MQTT" );
65
67
if (mqttClient.connect (deviceId.c_str (), config ().getString (" mqtt.username" , " " ).c_str (), config ().getString (" mqtt.password" , " " ).c_str ())) {
66
68
debug (" Connected to MQTT" );
@@ -70,18 +72,34 @@ class EasyMqtt : public MqttEntry {
70
72
});
71
73
72
74
debug (" Topic" , getTopic ());
75
+
73
76
each ([&](MqttEntry* entry){
74
77
if (entry->isOut ()) {
75
78
mqttClient.subscribe (entry->getTopic ().c_str ());
76
79
}
77
80
});
81
+ mqttDelay = 0 ;
78
82
} else {
79
83
debug (" Connection to MQTT failed, rc" , String (mqttClient.state ()));
80
- delay (500 );
84
+
85
+ mqttDelay = millis () + 1000 ;
81
86
}
82
87
}
83
88
}
84
89
90
+ void debug (String msg) {
91
+ #ifdef DEBUG
92
+ Serial.println (msg);
93
+ #endif
94
+ if (mqttClient.connected ()) {
95
+ get (" system/debug" ).publish (msg);
96
+ }
97
+ }
98
+
99
+ void debug (String key, String value) {
100
+ debug (key + " = " + value);
101
+ }
102
+
85
103
public:
86
104
EasyMqtt () : MqttEntry(" easyMqtt" , mqttClient) {
87
105
deviceId = String (ESP.getChipId ());
@@ -129,19 +147,6 @@ class EasyMqtt : public MqttEntry {
129
147
return *configEntry;
130
148
}
131
149
132
- void debug (String msg) {
133
- #ifdef DEBUG
134
- Serial.println (msg);
135
- #endif
136
- if (mqttClient.connected ()) {
137
- get (" system" )[" debug" ].publish (msg);
138
- }
139
- }
140
-
141
- void debug (String key, String value) {
142
- debug (key + " = " + value);
143
- }
144
-
145
150
String getDeviceId () {
146
151
return deviceId;
147
152
}
@@ -151,15 +156,17 @@ class EasyMqtt : public MqttEntry {
151
156
}
152
157
153
158
/* *
154
- Setup connection to wifi
159
+ Configure wifi
160
+ Deprecated
155
161
*/
156
162
void wifi (const char * ssid, const char * password) {
157
163
config ().set (" wifi.ssid" , ssid);
158
164
config ().set (" wifi.password" , password);
159
165
}
160
166
161
167
/* *
162
- Setup connection to mqtt
168
+ Configure mqtt
169
+ Deprecated
163
170
*/
164
171
void mqtt (const char * host, int port, const char * username, const char * password) {
165
172
config ().set (" mqtt.host" , host);
@@ -172,7 +179,8 @@ class EasyMqtt : public MqttEntry {
172
179
Handle the normal loop
173
180
*/
174
181
void loop () {
175
- connect ();
182
+ connectWiFi ();
183
+ connectMqtt ();
176
184
mqttClient.loop ();
177
185
webPortal.loop ();
178
186
each ([](MqttEntry* entry){
0 commit comments