8000 Merge pull request #475 from Brisdo/master · benjaminaigner/esp32-snippets@a578b10 · GitHub
[go: up one dir, main page]

Skip to content

Commit a578b10

Browse files
authored
Merge pull request nkolban#475 from Brisdo/master
Modified bootWiFi2 method to call m_wifi.connectAP forever or until it successfully connects
2 parents af664f0 + 2438e6d commit a578b10

File tree

5 files changed

+94
-12
lines changed

5 files changed

+94
-12
lines changed

cpp_utils/GeneralUtils.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,82 @@ const char* GeneralUtils::errorToString(esp_err_t errCode) {
434434
return "Unknown ESP_ERR error";
435435
} // errorToString
436436

437+
/**
438+
* @brief Convert a wifi_err_reason_t code to a string.
439+
* @param [in] errCode The errCode to be converted.
440+
* @return A string representation of the error code.
441+
*
442+
* @note: wifi_err_reason_t values as of April 2018 are: (1-24, 200-204) and are defined in ~/esp-idf/components/esp32/include/esp_wifi_types.h.
443+
*/
444+
const char* GeneralUtils::wifiErrorToString(uint8_t errCode) {
445+
if (errCode == ESP_OK)
446+
return "ESP_OK (received SYSTEM_EVENT_STA_GOT_IP event)";
447+
if (errCode == UINT8_MAX)
448+
return "Not Connected (default value)";
449+
450+
switch((wifi_err_reason_t) errCode) {
451+
case WIFI_REASON_UNSPECIFIED:
452+
return "WIFI_REASON_UNSPECIFIED";
453+
case WIFI_REASON_AUTH_EXPIRE:
454+
return "WIFI_REASON_AUTH_EXPIRE";
455+
case WIFI_REASON_AUTH_LEAVE:
456+
return "WIFI_REASON_AUTH_LEAVE";
457+
case WIFI_REASON_ASSOC_EXPIRE:
458+
return "WIFI_REASON_ASSOC_EXPIRE";
459+
case WIFI_REASON_ASSOC_TOOMANY:
460+
return "WIFI_REASON_ASSOC_TOOMANY";
461+
case WIFI_REASON_NOT_AUTHED:
462+
return "WIFI_REASON_NOT_AUTHED";
463+
case WIFI_REASON_NOT_ASSOCED:
464+
return "WIFI_REASON_NOT_ASSOCED";
465+
case WIFI_REASON_ASSOC_LEAVE:
466+
return "WIFI_REASON_ASSOC_LEAVE";
467+
case WIFI_REASON_ASSOC_NOT_AUTHED:
468+
return "WIFI_REASON_ASSOC_NOT_AUTHED";
469+
case WIFI_REASON_DISASSOC_PWRCAP_BAD:
470+
return "WIFI_REASON_DISASSOC_PWRCAP_BAD";
471+
case WIFI_REASON_DISASSOC_SUPCHAN_BAD:
472+
return "WIFI_REASON_DISASSOC_SUPCHAN_BAD";
473+
case WIFI_REASON_IE_INVALID:
474+
return "WIFI_REASON_IE_INVALID";
475+
case WIFI_REASON_MIC_FAILURE:
476+
return "WIFI_REASON_MIC_FAILURE";
477+
case WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT:
478+
return "WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT";
479+
case WIFI_REASON_GROUP_KEY_UPDATE_TIMEOUT:
480+
return "WIFI_REASON_GROUP_KEY_UPDATE_TIMEOUT";
481+
case WIFI_REASON_IE_IN_4WAY_DIFFERS:
482+
return "WIFI_REASON_IE_IN_4WAY_DIFFERS";
483+
case WIFI_REASON_GROUP_CIPHER_INVALID:
484+
return "WIFI_REASON_GROUP_CIPHER_INVALID";
485+
case WIFI_REASON_PAIRWISE_CIPHER_INVALID:
486+
return "WIFI_REASON_PAIRWISE_CIPHER_INVALID";
487+
case WIFI_REASON_AKMP_INVALID:
488+
return "WIFI_REASON_AKMP_INVALID";
489+
case WIFI_REASON_UNSUPP_RSN_IE_VERSION:
490+
return "WIFI_REASON_UNSUPP_RSN_IE_VERSION";
491+
case WIFI_REASON_INVALID_RSN_IE_CAP:
492+
return "WIFI_REASON_INVALID_RSN_IE_CAP";
493+
case WIFI_REASON_802_1X_AUTH_FAILED:
494+
return "WIFI_REASON_802_1X_AUTH_FAILED";
495+
case WIFI_REASON_CIPHER_SUITE_REJECTED:
496+
return "WIFI_REASON_CIPHER_SUITE_REJECTED";
497+
498+
case WIFI_REASON_BEACON_TIMEOUT:
499+
return "WIFI_REASON_BEACON_TIMEOUT";
500+
case WIFI_REASON_NO_AP_FOUND:
501+
return "WIFI_REASON_NO_AP_FOUND";
502+
case WIFI_REASON_AUTH_FAIL:
503+
return "WIFI_REASON_AUTH_FAIL";
504+
case WIFI_REASON_ASSOC_FAIL:
505+
return "WIFI_REASON_ASSOC_FAIL";
506+
case WIFI_REASON_HANDSHAKE_TIMEOUT:
507+
return "WIFI_REASON_HANDSHAKE_TIMEOUT";
508+
}
509+
return "Unknown ESP_ERR error";
510+
} // wifiErrorToString
511+
512+
437513

438514
/**
439515
* @brief Convert a string to lower case.

cpp_utils/GeneralUtils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class GeneralUtils {
2323
static void dumpInfo();
2424
static bool endsWith(std::string str, char c);
2525
static const char* errorToString(esp_err_t errCode);
26+
static const char* wifiErrorToString(uint8_t value);
2627
static void hexDump(const uint8_t* pData, uint32_t length);
2728
static std::string ipToString(uint8_t* ip);
2829
static std::vector<std::string> split(std::string source, char delimiter);

cpp_utils/WiFi.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ WiFi::WiFi()
5555
m_eventLoopStarted = false;
5656
m_initCalled = false;
5757
//m_pWifiEventHandler = new WiFiEventHandler();
58-
m_apConnected = false; // Are we connected to an access point?
58+
m_apConnectionStatus = UINT8_MAX; // Are we connected to an access point?
5959
} // WiFi
6060

6161

@@ -153,12 +153,12 @@ void WiFi::setDNSServer(int numdns, ip_addr_t ip) {
153153
* @param [in] ssid The network SSID of the access point to which we wish to connect.
154154
* @param [in] password The password of the access point to which we wish to connect.
155155
* @param [in] waitForConnection Block until the connection has an outcome.
156-
* @return N/A.
156+
* @returns ESP_OK if successfully receives a SYSTEM_EVENT_STA_GOT_IP event. Otherwise returns wifi_err_reason_t - use GeneralUtils::wifiErrorToString(uint8_t errCode) to print the error.
157157
*/
158-
bool WiFi::connectAP(const std::string& ssid, const std::string& password, bool waitForConnection){
158+
uint8_t WiFi::connectAP(const std::string& ssid, const std::string& password, bool waitForConnection){
159159
ESP_LOGD(LOG_TAG, ">> connectAP");
160160

161-
m_apConnected = false;
161+
m_apConnectionStatus = UINT8_MAX;
162162
init();
163163

164164
if (ip != 0 && gw != 0 && netmask != 0) {
@@ -206,7 +206,7 @@ bool WiFi::connectAP(const std::string& ssid, const std::string& password, bool
206206
m_connectFinished.give();
207207

208208
ESP_LOGD(LOG_TAG, "<< connectAP");
209-
return m_apConnected; // Return true if we are now connected and false if not.
209+
return m_apConnectionStatus; // Return ESP_OK if we are now connected and wifi_err_reason_t if not.
210210
} // connectAP
211211

212212

@@ -228,7 +228,7 @@ void WiFi::dump() {
228228
* @brief Returns whether wifi is connected to an access point
229229
*/
230230
bool WiFi::isConnectedToAP() {
231-
return m_apConnected;
231+
return m_apConnectionStatus;
232232
} // isConnected
233233

234234

@@ -255,10 +255,11 @@ bool WiFi::isConnectedToAP() {
255255
// If the event we received indicates that we now have an IP address or that a connection was disconnected then unlock the mutex that
256256
// indicates we are waiting for a connection complete.
257257
if (event->event_id == SYSTEM_EVENT_STA_GOT_IP || event->event_id == SYSTEM_EVENT_STA_DISCONNECTED) {
258-
if (event->event_id == SYSTEM_EVENT_STA_GOT_IP) { // If we connected and have an IP, change the flag.
259-
pWiFi->m_apConnected = true;
258+
259+
if (event->event_id == SYSTEM_EVENT_STA_GOT_IP) { // If we connected and have an IP, change the status to ESP_OK. Otherwise, change it to the reason code.
260+
pWiFi->m_apConnectionStatus = ESP_OK;
260261
} else {
261-
pWiFi->m_apConnected = false;
262+
pWiFi->m_apConnectionStatus = event->event_info.disconnected.reason;
262263
}
263264
pWiFi->m_connectFinished.give();
264265
}

cpp_utils/WiFi.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class WiFi {
116116
uint8_t m_dnsCount=0;
117117
bool m_eventLoopStarted;
118118
bool m_initCalled;
119-
bool m_apConnected; // Are we connected to an access point?
119+
uint8_t m_apConnectionStatus; // ESP_OK = we are connected to an access point. Otherwise receives wifi_err_reason_t.
120120
FreeRTOS::Semaphore m_connectFinished = FreeRTOS::Semaphore("ConnectFinished");
121121

122122
public:
@@ -130,7 +130,7 @@ class WiFi {
130130
void setDNSServer(int numdns, ip_addr_t ip);
131131
struct in_addr getHostByName(const std::string& hostName);
132132
struct in_addr getHostByName(const char* hostName);
133-
bool connectAP(const std::string& ssid, const std::string& password, bool waitForConnection=true);
133+
uint8_t connectAP(const std::string& ssid, const std::string& password, bool waitForConnection=true);
134134
void dump();
135135
bool isConnectedToAP();
136136
static std::string getApMac();

networking/bootwifi/BootWiFi.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,11 @@ void BootWiFi::bootWiFi2() {
285285
connectionInfo.ipInfo.gw.addr,
286286
connectionInfo.ipInfo.netmask.addr
287287
);
288-
m_wifi.connectAP(connectionInfo.ssid, connectionInfo.password); // Connect to the access point.
288+
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+
};
289293

290294
} else {
291295
// We do NOT have connection information. Let us now become an access

0 commit comments

Comments
 (0)
0