Skip to content

Commit 58fc617

Browse files
committed
Fix de mDNS pour les ESP8266, utilisation de MDNS sans passer par ArduinoOTA
1 parent aea3074 commit 58fc617

File tree

4 files changed

+47
-8
lines changed

4 files changed

+47
-8
lines changed

lightkit/software/platformio.ini

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
;
1111
; Note:
1212
; export envid=board_ring && pio run -e $envid -t upload && pio run -e $envid -t uploadfs && pio device monitor -e $envid
13+
;
14+
; Backtrace filters:
15+
; https://docs.platformio.org/en/latest/core/userguide/device/cmd_monitor.html#cmd-device-monitor-filters
1316

1417
[env]
1518
monitor_speed = 115200
@@ -41,6 +44,7 @@ lib_deps =
4144
ESP32WebServer
4245
build_flags =
4346
-DESP32
47+
monitor_filters = esp32_exception_decoder
4448

4549
[base_esp8266]
4650
platform = espressif8266
@@ -49,6 +53,7 @@ lib_deps =
4953
${env.lib_deps}
5054
build_flags =
5155
-DESP8266
56+
monitor_filters = esp8266_exception_decoder
5257

5358
; ========
5459
; Below are the environnements

lightkit/software/src/ota/ota.cpp

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,20 @@
1212
#include "global.hpp"
1313
#include "ota.hpp"
1414

15+
#ifdef ESP32
16+
#include <ESPmDNS.h>
17+
#else
18+
#include <ESP8266mDNS.h>
19+
#endif
20+
1521
extern uint32_t tick;
1622
uint32_t otaTick = 0;
1723

18-
static int ota_configure_mdns(void)
24+
/**
25+
* @brief Set the hostname for MDNS
26+
* @return 0: OK, -1: error
27+
*/
28+
int ota_configure_mdns(void)
1929
{
2030
// Check if module name is set
2131
if (flashSettings.moduleName[0] == '\0') {
@@ -24,15 +34,25 @@ static int ota_configure_mdns(void)
2434

2535
log_info("Using \"%s\" as module name", flashSettings.moduleName);
2636

27-
// mDNS is enabled by default in both ESP32 and ESP8266 libraries
28-
ArduinoOTA.setHostname(flashSettings.moduleName);
37+
// Clear MDNS before starting
38+
#ifdef ESP32
39+
MDNS.end();
40+
#else
41+
MDNS.close();
42+
#endif
43+
44+
// Start the service
45+
if (!MDNS.begin(flashSettings.moduleName)) {
46+
log_error("mDNS failed to start !");
47+
return -1;
48+
}
49+
MDNS.enableArduino(OTA_PORT);
50+
MDNS.addService("http", "tcp", 80);
2951
return 0;
3052
}
3153

3254
int ota_init(void)
3355
{
34-
ota_configure_mdns();
35-
3656
ArduinoOTA.setPort(OTA_PORT);
3757
ArduinoOTA.setPassword(OTA_PWD);
3858

@@ -78,12 +98,23 @@ int ota_init(void)
7898
else if (error == OTA_END_ERROR)
7999
log_error("End Failed");
80100
});
101+
102+
#ifdef ESP32
103+
ArduinoOTA.setMdnsEnabled(false);
81104
ArduinoOTA.begin();
105+
#else
106+
// false : don't do mdns, we do it ourself
107+
ArduinoOTA.begin(false);
108+
#endif
82109

83110
return 0;
84111
}
85112

86113
void ota_main(void)
87114
{
88115
ArduinoOTA.handle();
116+
117+
#if !defined(ESP32)
118+
MDNS.update();
119+
#endif
89120
}

lightkit/software/src/ota/ota.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define OTA_PWD P_OTA_PWD
1515

1616
// OTA
17+
int ota_configure_mdns(void);
1718
int ota_init(void);
1819
void ota_main(void);
1920

lightkit/software/src/wifi/wifi.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "wifi.hpp"
88
#include "flash/flash.hpp"
9+
#include "ota/ota.hpp"
910
#include "script/script.hpp"
1011

1112
// Externals
@@ -362,7 +363,7 @@ int32_t wifi_start_scan_req(uint32_t delay)
362363
return -1;
363364
}
364365

365-
log_info("Wifi scan requested, starting in %ds", delay / 1000);
366+
log_info("Wifi scan requested, starting in %d ms", delay);
366367

367368
// Trigger a new scan
368369
isScanToStartTick = tick + delay;
@@ -396,10 +397,10 @@ int wifi_init(void)
396397

397398
wifi_print();
398399

399-
// Scan wifi after startup (2s delay)
400+
// Scan wifi after startup (50ms delay)
400401
// Scan cannot be trigger right now because
401402
// OTA and web server requieres WiFi to be up
402-
wifi_start_scan_req(2000);
403+
wifi_start_scan_req(50);
403404

404405
return 0;
405406
}
@@ -438,6 +439,7 @@ void wifi_main(void)
438439
_set(STATUS_WIFI, STATUS_WIFI_IS_CO);
439440
wifi_print();
440441
wifi_save_current_ip();
442+
ota_configure_mdns();
441443
}
442444
}
443445
}

0 commit comments

Comments
 (0)