Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5f6d093

Browse filesBrowse files
authored
Add more controls over WiFi.begin and WiFi.scan (espressif#6651)
Added options to WiFi STA for connect: - setMinSecurity: default WIFI_AUTH_WPA2_PSK - setScanMethod: default WIFI_FAST_SCAN - setSortMethod: default WIFI_CONNECT_AP_BY_SIGNAL (required all channels scan method) Added parameters for SSID and BSSID to WiFi.scanNetworks() Fixes: espressif#6485 Fixes: Any issue about WiFi connecting slower now than in 1.0.x
1 parent 51bf183 commit 5f6d093
Copy full SHA for 5f6d093

File tree

Expand file treeCollapse file tree

5 files changed

+51
-12
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+51
-12
lines changed

‎libraries/WiFi/src/WiFiSTA.cpp

Copy file name to clipboardExpand all lines: libraries/WiFi/src/WiFiSTA.cpp
+37-8Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ static bool sta_config_equal(const wifi_config_t& lhs, const wifi_config_t& rhs)
8383
return true;
8484
}
8585

86-
static void wifi_sta_config(wifi_config_t * wifi_config, const char * ssid=NULL, const char * password=NULL, const uint8_t * bssid=NULL, uint8_t channel=0, wifi_scan_method_t scan_method=WIFI_ALL_CHANNEL_SCAN, wifi_sort_method_t sort_method=WIFI_CONNECT_AP_BY_SIGNAL, uint16_t listen_interval=0, bool pmf_required=false){
86+
static void wifi_sta_config(wifi_config_t * wifi_config, const char * ssid=NULL, const char * password=NULL, const uint8_t * bssid=NULL, uint8_t channel=0, wifi_auth_mode_t min_security=WIFI_AUTH_WPA2_PSK, wifi_scan_method_t scan_method=WIFI_ALL_CHANNEL_SCAN, wifi_sort_method_t sort_method=WIFI_CONNECT_AP_BY_SIGNAL, uint16_t listen_interval=0, bool pmf_required=false){
8787
wifi_config->sta.channel = channel;
8888
wifi_config->sta.listen_interval = listen_interval;
8989
wifi_config->sta.scan_method = scan_method;//WIFI_ALL_CHANNEL_SCAN or WIFI_FAST_SCAN
@@ -99,7 +99,7 @@ static void wifi_sta_config(wifi_config_t * wifi_config, const char * ssid=NULL,
9999
if(ssid != NULL && ssid[0] != 0){
100100
_wifi_strncpy((char*)wifi_config->sta.ssid, ssid, 32);
101101
if(password != NULL && password[0] != 0){
102-
wifi_config->sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
102+
wifi_config->sta.threshold.authmode = min_security;
103103
_wifi_strncpy((char*)wifi_config->sta.password, password, 64);
104104
}
105105
if(bssid != NULL){
@@ -115,6 +115,9 @@ static void wifi_sta_config(wifi_config_t * wifi_config, const char * ssid=NULL,
115115

116116
bool WiFiSTAClass::_autoReconnect = true;
117117
bool WiFiSTAClass::_useStaticIp = false;
118+
wifi_auth_mode_t WiFiSTAClass::_minSecurity = WIFI_AUTH_WPA2_PSK;
119+
wifi_scan_method_t WiFiSTAClass::_scanMethod = WIFI_FAST_SCAN;
120+
wifi_sort_method_t WiFiSTAClass::_sortMethod = WIFI_CONNECT_AP_BY_SIGNAL;
118121

119122
static wl_status_t _sta_status = WL_NO_SHIELD;
120123
static EventGroupHandle_t _sta_status_group = NULL;
@@ -243,12 +246,7 @@ wl_status_t WiFiSTAClass::begin(const char* ssid, const char *passphrase, int32_
243246
_wifi_strncpy(reinterpret_cast<char*>(conf.sta.password), passphrase, 64);
244247
}
245248

246-
if(channel == 0) {
247-
// If no specific channel specified, then do an slower WIFI_ALL_CHANNEL_SCAN
248-
wifi_sta_config(&conf, ssid, passphrase, bssid, channel, WIFI_ALL_CHANNEL_SCAN);
249-
}
250-
else
251-
wifi_sta_config(&conf, ssid, passphrase, bssid, channel, WIFI_FAST_SCAN);
249+
wifi_sta_config(&conf, ssid, passphrase, bssid, channel, _minSecurity, _scanMethod, _sortMethod);
252250

253251
wifi_config_t current_conf;
254252
if(esp_wifi_get_config((wifi_interface_t)ESP_IF_WIFI_STA, &current_conf) != ESP_OK){
@@ -404,6 +402,37 @@ bool WiFiSTAClass::isConnected()
404402
return (status() == WL_CONNECTED);
405403
}
406404

405+
/**
406+
* Set the minimum security for AP to be considered connectable
407+
* Must be called before WiFi.begin()
408+
* @param minSecurity wifi_auth_mode_t
409+
*/
410+
void WiFiSTAClass::setMinSecurity(wifi_auth_mode_t minSecurity)
411+
{
412+
_minSecurity = minSecurity;
413+
}
414+
415+
/**
416+
* Set the way that AP is chosen.
417+
* First SSID match[WIFI_FAST_SCAN] or Sorted[WIFI_ALL_CHANNEL_SCAN] (RSSI or Security)
418+
* Must be called before WiFi.begin()
419+
* @param scanMethod wifi_scan_method_t
420+
*/
421+
void WiFiSTAClass::setScanMethod(wifi_scan_method_t scanMethod)
422+
{
423+
_scanMethod = scanMethod;
424+
}
425+
426+
/**
427+
* Set the way that AP is sorted. (requires scanMethod WIFI_ALL_CHANNEL_SCAN)
428+
* By SSID[WIFI_CONNECT_AP_BY_SIGNAL] or Security[WIFI_CONNECT_AP_BY_SECURITY]
429+
* Must be called before WiFi.begin()
430+
* @param sortMethod wifi_sort_method_t
431+
*/
432+
void WiFiSTAClass::setSortMethod(wifi_sort_method_t sortMethod)
433+
{
434+
_sortMethod = sortMethod;
435+
}
407436

408437
/**
409438
* Setting the ESP32 station to connect to the AP (which is recorded)

‎libraries/WiFi/src/WiFiSTA.h

Copy file name to clipboardExpand all lines: libraries/WiFi/src/WiFiSTA.h
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ class WiFiSTAClass
6464

6565
uint8_t waitForConnectResult(unsigned long timeoutLength = 60000);
6666

67+
// Next group functions must be called before WiFi.begin()
68+
void setMinSecurity(wifi_auth_mode_t minSecurity);// Default is WIFI_AUTH_WPA2_PSK
69+
void setScanMethod(wifi_scan_method_t scanMethod);// Default is WIFI_FAST_SCAN
70+
void setSortMethod(wifi_sort_method_t sortMethod);// Default is WIFI_CONNECT_AP_BY_SIGNAL
71+
6772
// STA network info
6873
IPAddress localIP();
6974

@@ -96,6 +101,9 @@ class WiFiSTAClass
96101
protected:
97102
static bool _useStaticIp;
98103
static bool _autoReconnect;
104+
static wifi_auth_mode_t _minSecurity;
105+
static wifi_scan_method_t _scanMethod;
106+
static wifi_sort_method_t _sortMethod;
99107

100108
public:
101109
bool beginSmartConfig(smartconfig_type_t type = SC_TYPE_ESPTOUCH, char* crypt_key = NULL);

‎libraries/WiFi/src/WiFiScan.cpp

Copy file name to clipboardExpand all lines: libraries/WiFi/src/WiFiScan.cpp
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ void* WiFiScanClass::_scanResult = 0;
8181
* @param show_hidden show hidden networks
8282
* @return Number of discovered networks
8383
*/
84-
int16_t WiFiScanClass::scanNetworks(bool async, bool show_hidden, bool passive, uint32_t max_ms_per_chan, uint8_t channel)
84+
int16_t WiFiScanClass::scanNetworks(bool async, bool show_hidden, bool passive, uint32_t max_ms_per_chan, uint8_t channel, const char * ssid, const uint8_t * bssid)
8585
{
8686
if(WiFiGenericClass::getStatusBits() & WIFI_SCANNING_BIT) {
8787
return WIFI_SCAN_RUNNING;
@@ -95,8 +95,8 @@ int16_t WiFiScanClass::scanNetworks(bool async, bool show_hidden, bool passive,
9595
scanDelete();
9696

9797
wifi_scan_config_t config;
98-
config.ssid = 0;
99-
config.bssid = 0;
98+
config.ssid = (uint8_t*)ssid;
99+
config.bssid = (uint8_t*)bssid;
100100
config.channel = channel;
101101
config.show_hidden = show_hidden;
102102
if(passive){

‎libraries/WiFi/src/WiFiScan.h

Copy file name to clipboardExpand all lines: libraries/WiFi/src/WiFiScan.h
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class WiFiScanClass
3131

3232
public:
3333

34-
int16_t scanNetworks(bool async = false, bool show_hidden = false, bool passive = false, uint32_t max_ms_per_chan = 300, uint8_t channel = 0);
34+
int16_t scanNetworks(bool async = false, bool show_hidden = false, bool passive = false, uint32_t max_ms_per_chan = 300, uint8_t channel = 0, const char * ssid=nullptr, const uint8_t * bssid=nullptr);
3535

3636
int16_t scanComplete();
3737
void scanDelete();

‎libraries/WiFi/src/WiFiType.h

Copy file name to clipboardExpand all lines: libraries/WiFi/src/WiFiType.h
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#ifndef ESP32WIFITYPE_H_
2424
#define ESP32WIFITYPE_H_
2525

26+
#include "esp_wifi_types.h"
27+
2628
#define WIFI_SCAN_RUNNING (-1)
2729
#define WIFI_SCAN_FAILED (-2)
2830

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.