Skip to content

Commit 6f27f6f

Browse files
committed
ZEP-55: 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 76a3152 commit 6f27f6f

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

libraries/SocketWrapper/WiFi.h

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,36 @@ class WiFiClass: public NetworkInterface
1919
WiFiClass() {}
2020
~WiFiClass() {}
2121

22-
int begin(const char* ssid, const char* passphrase, wl_enc_type security = ENC_TYPE_UNKNOWN, bool blocking = true) {
22+
int begin(const char* ssid, const char* passphrase, wifi_security_type security = WIFI_SECURITY_TYPE_NONE, bool blocking = true) {
2323
sta_iface = net_if_get_wifi_sta();
2424
netif = sta_iface;
2525
sta_config.ssid = (const uint8_t *)ssid;
2626
sta_config.ssid_length = strlen(ssid);
2727
sta_config.psk = (const uint8_t *)passphrase;
2828
sta_config.psk_length = strlen(passphrase);
2929

30+
// The user might provide the security type as well
31+
if(security != WIFI_SECURITY_TYPE_NONE){
32+
sta_config.security = security;
33+
}else{
34+
sta_config.security = WIFI_SECURITY_TYPE_PSK;
35+
}
36+
sta_config.channel = WIFI_CHANNEL_ANY;
37+
sta_config.band = WIFI_FREQ_BAND_2_4_GHZ;
38+
sta_config.bandwidth = WIFI_FREQ_BANDWIDTH_20MHZ;
39+
3040
// Register the Wi-Fi event callback
3141
net_mgmt_init_event_callback(&wifiCb, scanEventDispatcher, NET_EVENT_WIFI_SCAN_RESULT | NET_EVENT_WIFI_SCAN_DONE);
3242

3343
net_mgmt_add_event_callback(&wifiCb);
3444

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

37-
// Check if the network we were seekin was found and attempt to connect to it
38-
if(getSoughtNetworkFound() != true)
3952
{
4053
int ret = net_mgmt(NET_REQUEST_WIFI_CONNECT, sta_iface, &sta_config,
4154
sizeof(struct wifi_connect_req_params));

libraries/SocketWrapper/examples/WiFiWebClient/WiFiWebClient.ino

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ void setup() {
4949
Serial.println(ssid);
5050
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
5151
status = WiFi.begin(ssid, pass);
52+
Serial.print("Wifi begin status: ");
5253
Serial.println(status);
53-
// wait 3 seconds for connection:
54-
delay(3000);
54+
// wait 6 seconds for connection:
55+
delay(6000);
5556
}
5657
Serial.println("Connected to wifi");
5758
printWifiStatus();

0 commit comments

Comments
 (0)