2
2
3
3
using namespace HomieInternals ;
4
4
5
+ #ifdef ESP32
5
6
BootNormal::BootNormal ()
6
7
: Boot(" normal" )
7
8
, _mqttReconnectTimer(MQTT_RECONNECT_INITIAL_INTERVAL, MQTT_RECONNECT_MAX_BACKOFF)
@@ -21,17 +22,48 @@ BootNormal::BootNormal()
21
22
, _mqttPayloadBuffer(nullptr )
22
23
, _mqttTopicLevels(nullptr )
23
24
, _mqttTopicLevelsCount(0 ) {
24
- strlcpy (_fwChecksum, ESP.getSketchMD5 ().c_str (), sizeof (_fwChecksum));
25
+ // FIXME: getSketchMD5 not implemented / boot loop on ESP32
26
+ // TODO: Remove Update.h from BootNormal.cpp, change status codes to https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/system/ota.html
27
+ strcpy (_fwChecksum, " 00000000000000000000000000000000" );
25
28
_fwChecksum[sizeof (_fwChecksum) - 1 ] = ' \0 ' ;
26
29
}
30
+ #elif defined(ESP8266)
31
+ BootNormal::BootNormal ()
32
+ : Boot(" normal" )
33
+ , _mqttReconnectTimer(MQTT_RECONNECT_INITIAL_INTERVAL, MQTT_RECONNECT_MAX_BACKOFF)
34
+ , _setupFunctionCalled(false )
35
+ , _mqttConnectNotified(false )
36
+ , _mqttDisconnectNotified(true )
37
+ , _otaOngoing(false )
38
+ , _flaggedForReboot(false )
39
+ , _mqttOfflineMessageId(0 )
40
+ , _otaIsBase64(false )
41
+ , _otaBase64Pads(0 )
42
+ , _otaSizeTotal(0 )
43
+ , _otaSizeDone(0 )
44
+ , _mqttTopic(nullptr )
45
+ , _mqttClientId(nullptr )
46
+ , _mqttWillTopic(nullptr )
47
+ , _mqttPayloadBuffer(nullptr )
48
+ , _mqttTopicLevels(nullptr )
49
+ , _mqttTopicLevelsCount(0 ) {
50
+ strlcpy (_fwChecksum, ESP.getSketchMD5 ().c_str (), sizeof (_fwChecksum));
51
+ _fwChecksum[sizeof (_fwChecksum) - 1 ] = ' \0 ' ;
52
+ }
53
+ #endif // ESP32
54
+
27
55
28
56
BootNormal::~BootNormal () {
29
57
}
30
58
31
59
void BootNormal::setup () {
32
60
Boot::setup ();
33
61
62
+ #ifdef ESP32
63
+ // FIXME
64
+ #elif defined(ESP8266)
34
65
Update.runAsync (true );
66
+ #endif // ESP32
35
67
36
68
_statsTimer.setInterval (Interface::get ().getConfig ().get ().deviceStatsInterval * 1000 );
37
69
@@ -53,8 +85,13 @@ void BootNormal::setup() {
53
85
}
54
86
_mqttTopic = std::unique_ptr<char []>(new char [baseTopicLength + longestSubtopicLength]);
55
87
88
+ #ifdef ESP32
89
+ _wifiGotIpHandler = WiFi.onEvent (std::bind (&BootNormal::_onWifiGotIp, this , std::placeholders::_1, std::placeholders::_2), WiFiEvent_t::SYSTEM_EVENT_STA_GOT_IP);
90
+ _wifiDisconnectedHandler = WiFi.onEvent (std::bind (&BootNormal::_onWifiDisconnected, this , std::placeholders::_1, std::placeholders::_2), WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
91
+ #elif defined(ESP8266)
56
92
_wifiGotIpHandler = WiFi.onStationModeGotIP (std::bind (&BootNormal::_onWifiGotIp, this , std::placeholders::_1));
57
93
_wifiDisconnectedHandler = WiFi.onStationModeDisconnected (std::bind (&BootNormal::_onWifiDisconnected, this , std::placeholders::_1));
94
+ #endif // ESP32
58
95
59
96
Interface::get ().getMqttClient ().onConnect (std::bind (&BootNormal::_onMqttConnected, this ));
60
97
Interface::get ().getMqttClient ().onDisconnect (std::bind (&BootNormal::_onMqttDisconnected, this , std::placeholders::_1));
@@ -144,10 +181,15 @@ void BootNormal::loop() {
144
181
}
145
182
146
183
if (_statsTimer.check ()) {
184
+ char statsIntervalStr[3 + 1 ];
185
+ itoa (Interface::get ().getConfig ().get ().deviceStatsInterval , statsIntervalStr, 10 );
186
+ Interface::get ().getLogger () << F (" 〽 Sending statistics..." ) << endl;
187
+ Interface::get ().getLogger () << F (" • Interval: " ) << statsIntervalStr << F (" s" ) << endl;
188
+ uint16_t intervalPacketId = Interface::get ().getMqttClient ().publish (_prefixMqttTopic (PSTR (" /$stats/interval" )), 1 , true , statsIntervalStr);
189
+
147
190
uint8_t quality = Helpers::rssiToPercentage (WiFi.RSSI ());
148
191
char qualityStr[3 + 1 ];
149
192
itoa (quality, qualityStr, 10 );
150
- Interface::get ().getLogger () << F (" 〽 Sending statistics..." ) << endl;
151
193
Interface::get ().getLogger () << F (" • Wi-Fi signal quality: " ) << qualityStr << F (" %" ) << endl;
152
194
uint16_t signalPacketId = Interface::get ().getMqttClient ().publish (_prefixMqttTopic (PSTR (" /$stats/signal" )), 1 , true , qualityStr);
153
195
@@ -157,7 +199,7 @@ void BootNormal::loop() {
157
199
Interface::get ().getLogger () << F (" • Uptime: " ) << uptimeStr << F (" s" ) << endl;
158
200
uint16_t uptimePacketId = Interface::get ().getMqttClient ().publish (_prefixMqttTopic (PSTR (" /$stats/uptime" )), 1 , true , uptimeStr);
159
201
160
- if (signalPacketId != 0 && uptimePacketId != 0 ) _statsTimer.tick ();
202
+ if (intervalPacketId != 0 && signalPacketId != 0 && uptimePacketId != 0 ) _statsTimer.tick ();
161
203
}
162
204
163
205
Interface::get ().loopFunction ();
@@ -204,10 +246,14 @@ void BootNormal::_endOtaUpdate(bool success, uint8_t update_error) {
204
246
switch (update_error) {
205
247
case UPDATE_ERROR_SIZE: // new firmware size is zero
206
248
case UPDATE_ERROR_MAGIC_BYTE: // new firmware does not have 0xE9 in first byte
249
+ #ifdef ESP32
250
+ // FIXME
251
+ #elif defined(ESP8266)
207
252
case UPDATE_ERROR_NEW_FLASH_CONFIG: // bad new flash config (does not match flash ID)
208
253
code = 400 ; // 400 Bad Request
209
254
info.concat (F (" BAD_FIRMWARE" ));
210
255
break ;
256
+ #endif // ESP32
211
257
case UPDATE_ERROR_MD5:
212
258
code = 400 ; // 400 Bad Request
213
259
info.concat (F (" BAD_CHECKSUM" ));
@@ -246,7 +292,11 @@ void BootNormal::_wifiConnect() {
246
292
247
293
if (WiFi.getMode () != WIFI_STA) WiFi.mode (WIFI_STA);
248
294
295
+ #ifdef ESP32
296
+ WiFi.setHostname (Interface::get ().getConfig ().get ().deviceId );
297
+ #elif defined(ESP8266)
249
298
WiFi.hostname (Interface::get ().getConfig ().get ().deviceId );
299
+ #endif // ESP32
250
300
if (strcmp_P (Interface::get ().getConfig ().get ().wifi .ip , PSTR (" " )) != 0 ) { // on _validateConfigWifi there is a requirement for mask and gateway
251
301
IPAddress convertedIp;
252
302
convertedIp.fromString (Interface::get ().getConfig ().get ().wifi .ip );
@@ -283,32 +333,63 @@ void BootNormal::_wifiConnect() {
283
333
}
284
334
}
285
335
286
- void BootNormal::_onWifiGotIp (const WiFiEventStationModeGotIP& event) {
336
+ #ifdef ESP32
337
+ void BootNormal::_onWifiGotIp (WiFiEvent_t event, WiFiEventInfo_t info) {
287
338
if (Interface::get ().led .enabled ) Interface::get ().getBlinker ().stop ();
288
- Interface::get ().getLogger () << F (" ✔ Wi-Fi connected, IP: " ) << event. ip << endl;
339
+ Interface::get ().getLogger () << F (" ✔ Wi-Fi connected, IP: " ) << IPAddress (info. got_ip . ip_info . ip . addr ) << endl;
289
340
Interface::get ().getLogger () << F (" Triggering WIFI_CONNECTED event..." ) << endl;
290
341
Interface::get ().event .type = HomieEventType::WIFI_CONNECTED;
291
- Interface::get ().event .ip = event. ip ;
292
- Interface::get ().event .mask = event. mask ;
293
- Interface::get ().event .gateway = event. gw ;
342
+ Interface::get ().event .ip = IPAddress (info. got_ip . ip_info . ip . addr ) ;
343
+ Interface::get ().event .mask = IPAddress (info. got_ip . ip_info . netmask . addr ) ;
344
+ Interface::get ().event .gateway = IPAddress (info. got_ip . ip_info . gw . addr ) ;
294
345
Interface::get ().eventHandler (Interface::get ().event );
295
346
MDNS.begin (Interface::get ().getConfig ().get ().deviceId );
296
347
297
348
_mqttConnect ();
298
349
}
299
-
300
- void BootNormal::_onWifiDisconnected (const WiFiEventStationModeDisconnected& event) {
350
+ #elif defined(ESP8266)
351
+ void BootNormal::_onWifiGotIp (const WiFiEventStationModeGotIP& event) {
352
+ if (Interface::get ().led .enabled ) Interface::get ().getBlinker ().stop ();
353
+ Interface::get ().getLogger () << F (" ✔ Wi-Fi connected, IP: " ) << event.ip << endl;
354
+ Interface::get ().getLogger () << F (" Triggering WIFI_CONNECTED event..." ) << endl;
355
+ Interface::get ().event .type = HomieEventType::WIFI_CONNECTED;
356
+ Interface::get ().event .ip = event.ip ;
357
+ Interface::get ().event .mask = event.mask ;
358
+ Interface::get ().event .gateway = event.gw ;
359
+ Interface::get ().eventHandler (Interface::get ().event );
360
+ MDNS.begin (Interface::get ().getConfig ().get ().deviceId );
361
+
362
+ _mqttConnect ();
363
+ }
364
+ #endif // ESP32
365
+
366
+ #ifdef ESP32
367
+ void BootNormal::_onWifiDisconnected (WiFiEvent_t event, WiFiEventInfo_t info) {
301
368
Interface::get ().ready = false ;
302
369
if (Interface::get ().led .enabled ) Interface::get ().getBlinker ().start (LED_WIFI_DELAY);
303
370
_statsTimer.reset ();
304
371
Interface::get ().getLogger () << F (" ✖ Wi-Fi disconnected" ) << endl;
305
372
Interface::get ().getLogger () << F (" Triggering WIFI_DISCONNECTED event..." ) << endl;
306
373
Interface::get ().event .type = HomieEventType::WIFI_DISCONNECTED;
307
- Interface::get ().event .wifiReason = event .reason ;
374
+ // Interface::get().event.wifiReason = info.disconnected .reason;
308
375
Interface::get ().eventHandler (Interface::get ().event );
309
376
310
377
_wifiConnect ();
311
378
}
379
+ #elif defined(ESP8266)
380
+ void BootNormal::_onWifiDisconnected (const WiFiEventStationModeDisconnected& event) {
381
+ Interface::get ().ready = false ;
382
+ if (Interface::get ().led .enabled ) Interface::get ().getBlinker ().start (LED_WIFI_DELAY);
383
+ _statsTimer.reset ();
384
+ Interface::get ().getLogger () << F (" ✖ Wi-Fi disconnected" ) << endl;
385
+ Interface::get ().getLogger () << F (" Triggering WIFI_DISCONNECTED event..." ) << endl;
386
+ Interface::get ().event .type = HomieEventType::WIFI_DISCONNECTED;
387
+ Interface::get ().event .wifiReason = event.reason ;
388
+ Interface::get ().eventHandler (Interface::get ().event );
389
+
390
+ _wifiConnect ();
391
+ }
392
+ #endif // ESP32
312
393
313
394
void BootNormal::_mqttConnect () {
314
395
if (!Interface::get ().disable ) {
@@ -366,7 +447,7 @@ void BootNormal::_advertise() {
366
447
break ;
367
448
case AdvertisementProgress::GlobalStep::PUB_STATS_INTERVAL:
368
449
char statsIntervalStr[3 + 1 ];
369
- itoa (STATS_SEND_INTERVAL_SEC / 1000 , statsIntervalStr, 10 );
450
+ itoa (Interface::get (). getConfig (). get (). deviceStatsInterval , statsIntervalStr, 10 );
370
451
packetId = Interface::get ().getMqttClient ().publish (_prefixMqttTopic (PSTR (" /$stats/interval" )), 1 , true , statsIntervalStr);
371
452
if (packetId != 0 ) _advertisementProgress.globalStep = AdvertisementProgress::GlobalStep::PUB_FW_NAME;
372
453
break ;
@@ -383,7 +464,11 @@ void BootNormal::_advertise() {
383
464
if (packetId != 0 ) _advertisementProgress.globalStep = AdvertisementProgress::GlobalStep::PUB_IMPLEMENTATION;
384
465
break ;
385
466
case AdvertisementProgress::GlobalStep::PUB_IMPLEMENTATION:
467
+ #ifdef ESP32
468
+ packetId = Interface::get ().getMqttClient ().publish (_prefixMqttTopic (PSTR (" /$implementation" )), 1 , true , " esp32" );
469
+ #elif defined(ESP8266)
386
470
packetId = Interface::get ().getMqttClient ().publish (_prefixMqttTopic (PSTR (" /$implementation" )), 1 , true , " esp8266" );
471
+ #endif // ESP32
387
472
if (packetId != 0 ) _advertisementProgress.globalStep = AdvertisementProgress::GlobalStep::PUB_IMPLEMENTATION_CONFIG;
388
473
break ;
389
474
case AdvertisementProgress::GlobalStep::PUB_IMPLEMENTATION_CONFIG:
0 commit comments