8000 added a way to connect to a Station while keeping the AP running · Olaclemmy/esp32-snippets@7e1cf23 · GitHub
[go: up one dir, main page]

8000
Skip to content

Commit 7e1cf23

Browse files
committed
added a way to connect to a Station while keeping the AP running
1 parent 4743dfc commit 7e1cf23

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

cpp_utils/WiFi.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,16 +146,24 @@ void WiFi::setDNSServer(int numdns, ip_addr_t ip) {
146146

147147

148148
/**
149-
* @brief Connect to an external access point.
149+
* @see WiFi::connectWithMode - this one defaults the mode to WIFI_MODE_AP
150+
*/
151+
uint8_t WiFi::connectAP(const std::string& ssid, const std::string& password, bool waitForConnection){
152+
return this->connectWithMode(ssid, password, waitForConnection, WIFI_MODE_AP);
153+
}
154+
155+
/**
156+
* @brief Connect to an external access point and specify the mode (WIFI_MODE_AP or WIFI_MODE_APSTA).
150157
*
151158
* The event handler will be called back with the outcome of the connection.
152159
*
153160
* @param [in] ssid The network SSID of the access point to which we wish to connect.
154161
* @param [in] password The password of the access point to which we wish to connect.
155162
* @param [in] waitForConnection Block until the connection has an outcome.
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.
163+
* @param [in] mode WIFI_MODE_AP for normal or WIFI_MODE_APSTA if you want to keep an Access Point running while you connect
164+
* @return N/A.
157165
*/
158-
uint8_t WiFi::connectAP(const std::string& ssid, const std::string& password, bool waitForConnection){
166+
uint8_t WiFi::connectAP(const std::string& ssid, const std::string& password, bool waitForConnection, wifi_mode_t mode){
159167
ESP_LOGD(LOG_TAG, ">> connectAP");
160168

161169
m_apConnectionStatus = UINT8_MAX;
@@ -172,7 +180,7 @@ uint8_t WiFi::connectAP(const std::string& ssid, const std::string& password, bo
172180
::tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_STA, &ipInfo);
173181
}
174182

175-
esp_err_t errRc = ::esp_wifi_set_mode(WIFI_MODE_STA);
183+
esp_err_t errRc = ::esp_wifi_set_mode(mode);
176184
if (errRc != ESP_OK) {
177185
ESP_LOGE(LOG_TAG, "esp_wifi_set_mode: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
178186
abort();

cpp_utils/WiFi.h

Lines changed: 1 addition & 0 deletions
Original file line number 6551 Diff line numberDiff line change
@@ -131,6 +131,7 @@ class WiFi {
131131
struct in_addr getHostByName(const std::string& hostName);
132132
struct in_addr getHostByName(const char* hostName);
133133
uint8_t connectAP(const std::string& ssid, const std::string& password, bool waitForConnection=true);
134+
uint8_t connectWithMode(const std::string& ssid, const std::string& password, bool waitForConnection, wifi_mode_t mode);
134135
void dump();
135136
bool isConnectedToAP();
136137
static std::string getApMac();

0 commit comments

Comments
 (0)
0