Skip to content

Commit 0de5429

Browse files
authored
fix: prevent infinite WiFi connection loop (#11)
1 parent bfca8c7 commit 0de5429

2 files changed

Lines changed: 18 additions & 7 deletions

File tree

platformio.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,6 @@ lib_deps =
1616
adafruit/Adafruit BMP280 Library@^2.6.8
1717
adafruit/Adafruit AHTX0@^2.0.5
1818
claws/BH1750@^1.3.0
19+
monitor_speed = 115200
20+
upload_speed = 921600
21+

src/utils.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,29 @@ void isolate_all_rtc_gpio()
6060

6161
/**
6262
* Establishes a WiFi connection using credentials from env.h
63-
*
64-
* @note This function will block indefinitely if WiFi connection fails
65-
* TODO(FIX): this surely shouldn't be implemented this way
66-
* TODO(FIX): we should FIX it ASAP as it already caused issues
6763
*/
6864
void connect_to_wifi()
6965
{
7066
serial_log("Connecting to WiFi...");
7167
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
72-
while (WiFi.status() != WL_CONNECTED) {
68+
for (int i = 0; i < 20; i++) {
69+
if (WiFi.status() == WL_CONNECTED) {
70+
serial_log("\nWiFi connected!");
71+
serial_log(WiFi.localIP().toString());
72+
return;
73+
}
74+
if (WiFi.status() == WL_NO_SSID_AVAIL) {
75+
Serial.println("SSID not found!");
76+
Serial.println("Entering deep sleep for 5 minutes.");
77+
esp_sleep_enable_timer_wakeup(300000000);
78+
esp_deep_sleep_start();
79+
}
7380
delay(500);
7481
serial_log(".");
7582
}
76-
serial_log("\nWiFi connected!");
77-
serial_log(WiFi.localIP().toString());
83+
84+
Serial.println("Response: " + String(WiFi.status()));
85+
ESP.restart();
7886
}
7987

8088
/**

0 commit comments

Comments
 (0)