Skip to content

Commit d93ba64

Browse files
bogdanarduinopennam
authored andcommitted
ZEP-55: WiFi: try to connect using default connection parameters
Device will try to connect using default connection parameters, if an SSID and a password were provided, even if the scan was unsuccessful.
1 parent 96c21a3 commit d93ba64

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

libraries/WiFi/examples/WiFiWebClient/WiFiWebClient.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ void setup() {
5050
Serial.println(ssid);
5151
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
5252
status = WiFi.begin(ssid, pass);
53-
// wait 3 seconds for connection:
54-
delay(3000);
53+
// wait 6 seconds for connection:
54+
delay(6000);
5555
}
5656
Serial.println("Connected to wifi");
5757
printWifiStatus();

libraries/WiFi/src/WiFi.cpp

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,41 @@ String WiFiClass::firmwareVersion() {
1212
#endif
1313
}
1414

15-
int WiFiClass::begin(const char *ssid, const char *passphrase, wl_enc_type security,
16-
bool blocking) {
15+
int begin(const char *ssid, const char *passphrase,
16+
wifi_security_type security = WIFI_SECURITY_TYPE_NONE, bool blocking = true) {
1717
sta_iface = net_if_get_wifi_sta();
1818
netif = sta_iface;
1919
sta_config.ssid = (const uint8_t *)ssid;
2020
sta_config.ssid_length = strlen(ssid);
2121
sta_config.psk = (const uint8_t *)passphrase;
2222
sta_config.psk_length = strlen(passphrase);
2323

24+
// The user might provide the security type as well
25+
if (security != WIFI_SECURITY_TYPE_NONE) {
26+
sta_config.security = security;
27+
} else {
28+
sta_config.security = WIFI_SECURITY_TYPE_PSK;
29+
}
30+
sta_config.channel = WIFI_CHANNEL_ANY;
31+
sta_config.band = WIFI_FREQ_BAND_2_4_GHZ;
32+
sta_config.bandwidth = WIFI_FREQ_BANDWIDTH_20MHZ;
33+
2434
// Register the Wi-Fi event callback
2535
net_mgmt_init_event_callback(&wifiCb, scanEventDispatcher,
2636
NET_EVENT_WIFI_SCAN_RESULT | NET_EVENT_WIFI_SCAN_DONE);
2737

2838
net_mgmt_add_event_callback(&wifiCb);
2939

30-
(void)scanNetworks();
40+
// If the network we are scanning for is found, the connection parameters will be updated
41+
// automatically;
42+
(void)scanNetworks(); // This is a blocking function call
43+
44+
// Attempt to connect with either default parameters, or the updated ones after the scan
45+
// completed
46+
if ((sta_config.ssid != NULL) && (sta_config.ssid_length != 0u) && (sta_config.psk != NULL) &&
47+
(sta_config.psk_length != 0u))
3148

32-
// Check if the network we were seekin was found and attempt to connect to it
33-
if (getSoughtNetworkFound() != true) {
49+
{
3450
int ret = net_mgmt(NET_REQUEST_WIFI_CONNECT, sta_iface, &sta_config,
3551
sizeof(struct wifi_connect_req_params));
3652
if (ret) {

libraries/WiFi/src/WiFi.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ class WiFiClass : public NetworkInterface {
2020
~WiFiClass() {
2121
}
2222

23-
int begin(const char *ssid, const char *passphrase, wl_enc_type security = ENC_TYPE_UNKNOWN,
24-
bool blocking = true);
23+
int begin(const char *ssid, const char *passphrase,
24+
wifi_security_type security = WIFI_SECURITY_TYPE_NONE, bool blocking = true);
2525
bool beginAP(char *ssid, char *passphrase, int channel = WIFI_CHANNEL_ANY,
2626
bool blocking = false);
2727

0 commit comments

Comments
 (0)