From 7e1cf238767d5456235afba7c37830633968dc89 Mon Sep 17 00:00:00 2001 From: Damon Smith Date: Tue, 26 Jun 2018 22:33:59 +1000 Subject: [PATCH 1/3] added a way to connect to a Station while keeping the AP running --- cpp_utils/WiFi.cpp | 16 ++++++++++++---- cpp_utils/WiFi.h | 1 + 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/cpp_utils/WiFi.cpp b/cpp_utils/WiFi.cpp index 503e9235..2372dea6 100644 --- a/cpp_utils/WiFi.cpp +++ b/cpp_utils/WiFi.cpp @@ -146,16 +146,24 @@ void WiFi::setDNSServer(int numdns, ip_addr_t ip) { /** - * @brief Connect to an external access point. + * @see WiFi::connectWithMode - this one defaults the mode to WIFI_MODE_AP + */ +uint8_t WiFi::connectAP(const std::string& ssid, const std::string& password, bool waitForConnection){ + return this->connectWithMode(ssid, password, waitForConnection, WIFI_MODE_AP); +} + +/** + * @brief Connect to an external access point and specify the mode (WIFI_MODE_AP or WIFI_MODE_APSTA). * * The event handler will be called back with the outcome of the connection. * * @param [in] ssid The network SSID of the access point to which we wish to connect. * @param [in] password The password of the access point to which we wish to connect. * @param [in] waitForConnection Block until the connection has an outcome. - * @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. + * @param [in] mode WIFI_MODE_AP for normal or WIFI_MODE_APSTA if you want to keep an Access Point running while you connect + * @return N/A. */ -uint8_t WiFi::connectAP(const std::string& ssid, const std::string& password, bool waitForConnection){ +uint8_t WiFi::connectAP(const std::string& ssid, const std::string& password, bool waitForConnection, wifi_mode_t mode){ ESP_LOGD(LOG_TAG, ">> connectAP"); m_apConnectionStatus = UINT8_MAX; @@ -172,7 +180,7 @@ uint8_t WiFi::connectAP(const std::string& ssid, const std::string& password, bo ::tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_STA, &ipInfo); } - esp_err_t errRc = ::esp_wifi_set_mode(WIFI_MODE_STA); + esp_err_t errRc = ::esp_wifi_set_mode(mode); if (errRc != ESP_OK) { ESP_LOGE(LOG_TAG, "esp_wifi_set_mode: rc=%d %s", errRc, GeneralUtils::errorToString(errRc)); abort(); diff --git a/cpp_utils/WiFi.h b/cpp_utils/WiFi.h index a57360ca..09f3d56d 100644 --- a/cpp_utils/WiFi.h +++ b/cpp_utils/WiFi.h @@ -131,6 +131,7 @@ class WiFi { struct in_addr getHostByName(const std::string& hostName); struct in_addr getHostByName(const char* hostName); uint8_t connectAP(const std::string& ssid, const std::string& password, bool waitForConnection=true); + uint8_t connectWithMode(const std::string& ssid, const std::string& password, bool waitForConnection, wifi_mode_t mode); void dump(); bool isConnectedToAP(); static std::string getApMac(); From 18a03f9a20d55bfc55648f43964222725cab3e7e Mon Sep 17 00:00:00 2001 From: Damon Smith Date: Tue, 26 Jun 2018 22:42:01 +1000 Subject: [PATCH 2/3] fixed to just use default value params --- cpp_utils/WiFi.cpp | 7 ------- cpp_utils/WiFi.h | 3 +-- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/cpp_utils/WiFi.cpp b/cpp_utils/WiFi.cpp index 2372dea6..f1a07217 100644 --- a/cpp_utils/WiFi.cpp +++ b/cpp_utils/WiFi.cpp @@ -145,13 +145,6 @@ void WiFi::setDNSServer(int numdns, ip_addr_t ip) { } // setDNSServer -/** - * @see WiFi::connectWithMode - this one defaults the mode to WIFI_MODE_AP - */ -uint8_t WiFi::connectAP(const std::string& ssid, const std::string& password, bool waitForConnection){ - return this->connectWithMode(ssid, password, waitForConnection, WIFI_MODE_AP); -} - /** * @brief Connect to an external access point and specify the mode (WIFI_MODE_AP or WIFI_MODE_APSTA). * diff --git a/cpp_utils/WiFi.h b/cpp_utils/WiFi.h index 09f3d56d..c1b6bedc 100644 --- a/cpp_utils/WiFi.h +++ b/cpp_utils/WiFi.h @@ -130,8 +130,7 @@ class WiFi { void setDNSServer(int numdns, ip_addr_t ip); struct in_addr getHostByName(const std::string& hostName); struct in_addr getHostByName(const char* hostName); - uint8_t connectAP(const std::string& ssid, const std::string& password, bool waitForConnection=true); - uint8_t connectWithMode(const std::string& ssid, const std::string& password, bool waitForConnection, wifi_mode_t mode); + uint8_t connectAP(const std::string& ssid, const std::string& password, bool waitForConnection=true, wifi_mode_t mode=WIFI_MODE_STA); void dump(); bool isConnectedToAP(); static std::string getApMac(); From cc56443c45344fa48101403fb62cc1249c2e1f01 Mon Sep 17 00:00:00 2001 From: Damon Smith Date: Tue, 26 Jun 2018 22:43:51 +1000 Subject: [PATCH 3/3] fix up comments on connectAP --- cpp_utils/WiFi.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cpp_utils/WiFi.cpp b/cpp_utils/WiFi.cpp index f1a07217..09d59d55 100644 --- a/cpp_utils/WiFi.cpp +++ b/cpp_utils/WiFi.cpp @@ -146,7 +146,7 @@ void WiFi::setDNSServer(int numdns, ip_addr_t ip) { /** - * @brief Connect to an external access point and specify the mode (WIFI_MODE_AP or WIFI_MODE_APSTA). + * @brief Connect to an external access point. * * The event handler will be called back with the outcome of the connection. * @@ -154,7 +154,7 @@ void WiFi::setDNSServer(int numdns, ip_addr_t ip) { * @param [in] password The password of the access point to which we wish to connect. * @param [in] waitForConnection Block until the connection has an outcome. * @param [in] mode WIFI_MODE_AP for normal or WIFI_MODE_APSTA if you want to keep an Access Point running while you connect - * @return N/A. + * @return ESP_OK if we are now connected and wifi_err_reason_t if not. */ uint8_t WiFi::connectAP(const std::string& ssid, const std::string& password, bool waitForConnection, wifi_mode_t mode){ ESP_LOGD(LOG_TAG, ">> connectAP");