Skip to content

Commit 84174b5

Browse files
committed
Fix d'un crash lors de la connexion en Access Point
1 parent 8a814e8 commit 84174b5

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

lightkit/software/src/wifi/wifi.cpp

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ extern flash_settings_t flashSettings;
1414

1515
// Internals
1616
wifi_handle_t * wifiHandle = NULL;
17-
uint32_t wifiTick = 0;
18-
uint32_t APFallbackTick = 0; // Tick set at init before falling in AP mode in case of unsuccessfull client mode
19-
17+
uint32_t wifiTick = WIFI_CHECK_PERIOD; // Init with its value to avoid an execution on the first run of wifi_main()
18+
uint32_t APFallbackTick = 0; // Tick set at init before falling in AP mode in case of unsuccessfull client mode
19+
bool isAPConfigToDo;
2020
/**
2121
* We need some compiler tricks here
2222
* See: https://pcbartists.com/firmware/esp32-firmware/designator-outside-aggregate-initializer-solved/
@@ -80,10 +80,16 @@ static void wifi_fallback_as_ap(void)
8080
static int wifi_ap_init(void)
8181
{
8282
WiFi.mode(WIFI_AP);
83+
8384
WiFi.softAP(wifiHandle->ap.ssid, wifiHandle->ap.password, wifiHandle->ap.channel, wifiHandle->ap.isHidden, wifiHandle->ap.maxConnection);
85+
8486
/** This line does magic, keep it here */
8587
WiFi.persistent(false);
86-
WiFi.softAPConfig(wifiHandle->ap.ip, wifiHandle->ap.gateway, wifiHandle->ap.subnet);
88+
89+
// We have to wait at least 100ms after WiFi.softAP() before calling WiFi.softAPConfig()
90+
// so we moved this call into the main function with the flag isAPConfigToDo
91+
// More info : https://github.com/espressif/arduino-esp32/issues/985#issuecomment-359157428
92+
isAPConfigToDo = true;
8793
return 0;
8894
}
8995

@@ -108,7 +114,7 @@ static int wifi_client_init(void)
108114
*
109115
* @return boolean
110116
*/
111-
static bool wifi_is_handle_valid(wifi_handle_t * pWifiHandle, String &reason)
117+
static bool wifi_is_handle_valid(wifi_handle_t * pWifiHandle, String & reason)
112118
{
113119
if (pWifiHandle->userMode >= MODE_MAX) {
114120
reason = "Mauvais mode wifi";
@@ -159,13 +165,13 @@ wifi_handle_t * wifi_get_handle(void)
159165
* @brief Use new settings for wifi module
160166
* @details Store them in flash after validation check
161167
*/
162-
int32_t wifi_use_new_settings(wifi_handle_t * pWifiHandle, String &reason)
168+
int32_t wifi_use_new_settings(wifi_handle_t * pWifiHandle, String & reason)
163169
{
164170
if (wifi_is_handle_valid(pWifiHandle, reason)) {
165171
// Copy new data to handle
166172
memcpy(wifiHandle, pWifiHandle, sizeof(wifi_handle_t));
167173
wifiHandle->forcedMode = MODE_NONE;
168-
wifiHandle->mode = MODE_NONE;
174+
wifiHandle->mode = MODE_NONE;
169175

170176
// Save handle to flash
171177
return flash_write();
@@ -211,7 +217,7 @@ int wifi_init(void)
211217
ret = wifi_ap_init();
212218

213219
// In AP, we are ready to print now
214-
wifi_print();
220+
//wifi_print();
215221
} else if (wifiHandle->mode == MODE_CLIENT) {
216222
ret = wifi_client_init();
217223
wifi_print();
@@ -239,6 +245,13 @@ void wifi_main(void)
239245
wifiTick = tick + WIFI_CHECK_PERIOD;
240246

241247
if (wifiHandle->mode == MODE_AP) {
248+
// We have to wait a bit before calling WiFi.softAPConfig()
249+
// so do it here 1 time after one WIFI_CHECK_PERIOD
250+
if (isAPConfigToDo) {
251+
isAPConfigToDo = false;
252+
WiFi.softAPConfig(wifiHandle->ap.ip, wifiHandle->ap.gateway, wifiHandle->ap.subnet);
253+
}
254+
242255
if (WiFi.softAPgetStationNum() <= 0) {
243256
_unset(STATUS_WIFI, STATUS_WIFI_DEVICE_CO);
244257
} else {

0 commit comments

Comments
 (0)