8000 Merge pull request #497 from Brisdo/master · llawall/esp32-snippets@756e8d8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 756e8d8

Browse files
authored
Merge pull request nkolban#497 from Brisdo/master
Additional to PR nkolban#475. Addresses BootWiFi::boot hanging
2 parents a578b10 + 0eda8c6 commit 756e8d8

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

networking/bootwifi/BootWiFi.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ class BootWifiEventHandler: public WiFiEventHandler {
243243

244244
esp_err_t staGotIp(system_event_sta_got_ip_t event_sta_got_ip) {
245245
ESP_LOGD("BootWifiEventHandler", ">> staGotIP");
246+
m_pBootWiFi->m_apConnectionStatus = ESP_OK; // Set the status to ESP_OK
246247
m_pBootWiFi->m_completeSemaphore.give(); // If we got an IP address, then we can end the boot process.
247248
ESP_LOGD("BootWifiEventHandler", "<< staGotIP");
248249
return ESP_OK;
@@ -255,6 +256,11 @@ class BootWifiEventHandler: public WiFiEventHandler {
255256

256257
/**
257258
* Boot WiFi
259+
*
260+
* @brief Get connected to WiFi
261+
*
262+
* @detailed If SSID & Password were previously saved, connect to the AP. Otherwise become an AP and start an HTTP server so that the user can set SSID & Password - then save it.
263+
*
258264
*/
259265
void BootWiFi::bootWiFi2() {
260266
ESP_LOGD(LOG_TAG, ">> bootWiFi2");
@@ -286,10 +292,8 @@ void BootWiFi::bootWiFi2() {
286292
connectionInfo.ipInfo.netmask.addr
287293
);
288294

289-
// Connect to the access point.
290-
while(!m_wifi.connectAP(connectionInfo.ssid, connectionInfo.password)){
291-
ESP_LOGE(LOG_TAG, "Unable to connect to access point \"%s\" - trying again...", connectionInfo.ssid);
292-
};
295+
m_apConnectionStatus = m_wifi.connectAP(connectionInfo.ssid, connectionInfo.password); // Try to connect to the access point.
296+
m_completeSemaphore.give(); // end the boot process so we don't hang...
293297

294298
} else {
295299
// We do NOT have connection information. Let us now become an access
@@ -316,22 +320,28 @@ void BootWiFi::setAccessPointCredentials(std::string ssid, std::string password)
316320

317321

318322
/**
319-
* @brief Main entry point into booting WiFi
323+
* @brief Main entry point into booting WiFi - see BootWiFi2 for more detail.
324+
*
325+
* The event handler will be called back with the outcome of the connection.
326+
*
327+
* @returns ESP_OK if successfully connected to an access point. Otherwise returns wifi_err_reason_t - to print use GeneralUtils::wifiErrorToString
320328
*/
321-
void BootWiFi::boot() {
329+
uint8_t BootWiFi::boot() {
322330
ESP_LOGD(LOG_TAG, ">> boot");
323331
ESP_LOGD(LOG_TAG, " +----------+");
324332
ESP_LOGD(LOG_TAG, " | BootWiFi |");
325333
ESP_LOGD(LOG_TAG, " +----------+");
326334
ESP_LOGD(LOG_TAG, " Access point credentials: %s/%s", m_ssid.c_str(), m_password.c_str());
327-
m_completeSemaphore.take("boot"); // Take the semaphore which will be unlocked when we complete booting.
335+
m_completeSemaphore.take("boot"); // Take the semaphore which will be unlocked when we complete booting.
328336
bootWiFi2();
329337
m_completeSemaphore.wait("boot"); // Wait for the semaphore that indicated we have completed booting.
330338
m_wifi.setWifiEventHandler(nullptr); // Remove the WiFi boot handler when we have completed booting.
331339
ESP_LOGD(LOG_TAG, "<< boot");
340+
return m_apConnectionStatus;
332341
} // boot
333342

334343
BootWiFi::BootWiFi() {
335344
m_httpServerStarted = false;
345+
m_apConnectionStatus = UINT8_MAX;
336346
setAccessPointCredentials("esp32", "password"); // Default access point credentials
337347
}

networking/bootwifi/BootWiFi.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ class BootWiFi {
2424
bool m_httpServerStarted;
2525
std::string m_ssid;
2626
std::string m_password;
27+
uint8_t m_apConnectionStatus; // receives the connection status. ESP_OK = received SYSTEM_EVENT_STA_GOT_IP event.
2728
FreeRTOS::Semaphore m_completeSemaphore = FreeRTOS::Semaphore("completeSemaphore");
2829

2930
public:
3031
BootWiFi();
3132
void s 4C3E etAccessPointCredentials(std::string ssid, std::string password);
32-
void boot();
33+
uint8_t boot();
3334
};
3435

3536
#endif /* MAIN_BOOTWIFI_H_ */

0 commit comments

Comments
 (0)
0