Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SSID scan and validation #377

Merged
merged 11 commits into from
Nov 30, 2022
Merged
21 changes: 16 additions & 5 deletions src/Wippersnapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ void Wippersnapper::set_ssid_pass() {
WS_DEBUG_PRINTLN("ERROR: Please define a network interface!");
}

bool Wippersnapper::check_valid_ssid() {
WS_DEBUG_PRINTLN("ERROR: Please define a network interface!");
return false;
}

/****************************************************************************/
/*!
@brief Configures the device's Adafruit IO credentials. This method
Expand Down Expand Up @@ -1836,24 +1841,29 @@ void Wippersnapper::runNetFSM() {
while (fsmNetwork != FSM_NET_CONNECTED) {
switch (fsmNetwork) {
case FSM_NET_CHECK_MQTT:
// WS_DEBUG_PRINTLN("FSM_NET_CHECK_MQTT");
if (WS._mqtt->connected()) {
WS_DEBUG_PRINTLN("Connected to Adafruit IO!");
fsmNetwork = FSM_NET_CONNECTED;
return;
}
fsmNetwork = FSM_NET_CHECK_NETWORK;
break;
case FSM_NET_CHECK_NETWORK:
// WS_DEBUG_PRINTLN("FSM_NET_CHECK_NETWORK");
if (networkStatus() == WS_NET_CONNECTED) {
// WS_DEBUG_PRINTLN("Connected to WiFi");
WS_DEBUG_PRINTLN("Connected to WiFi!");
fsmNetwork = FSM_NET_ESTABLISH_MQTT;
break;
}
fsmNetwork = FSM_NET_ESTABLISH_NETWORK;
break;
case FSM_NET_ESTABLISH_NETWORK:
// WS_DEBUG_PRINTLN("FSM_NET_ESTABLISH_NETWORK");
WS_DEBUG_PRINTLN("Attempting to connect to WiFi");
// Perform a WiFi scan and check if SSID within
// secrets.json is within the scanned SSIDs
if (!check_valid_ssid())
haltError("ERROR: Unable to find WiFi network, rebooting soon...",
WS_LED_STATUS_WIFI_CONNECTING);

// Attempt to connect to wireless network
maxAttempts = 5;
while (maxAttempts > 0) {
Expand All @@ -1871,14 +1881,15 @@ void Wippersnapper::runNetFSM() {
break;
maxAttempts--;
}

// Validate connection
if (networkStatus() != WS_NET_CONNECTED)
haltError("ERROR: Unable to connect to WiFi, rebooting soon...",
WS_LED_STATUS_WIFI_CONNECTING);
fsmNetwork = FSM_NET_CHECK_NETWORK;
break;
case FSM_NET_ESTABLISH_MQTT:
WS_DEBUG_PRINTLN("FSM_NET_ESTABLISH_MQTT");
WS_DEBUG_PRINTLN("Attempting to connect to Adafruit IO...");
WS._mqtt->setKeepAliveInterval(WS_KEEPALIVE_INTERVAL_MS / 1000);
// Attempt to connect
maxAttempts = 5;
Expand Down
1 change: 1 addition & 0 deletions src/Wippersnapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ class Wippersnapper {
virtual void set_user_key();
virtual void set_ssid_pass(const char *ssid, const char *ssidPassword);
virtual void set_ssid_pass();
virtual bool check_valid_ssid();

virtual void _connect();
virtual void _disconnect();
Expand Down
37 changes: 37 additions & 0 deletions src/network_interfaces/Wippersnapper_AIRLIFT.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,43 @@ class Wippersnapper_AIRLIFT : public Wippersnapper {
_pass = WS._network_pass;
}

/***********************************************************/
/*!
@brief Performs a scan of local WiFi networks.
@returns True if `_network_ssid` is found, False otherwise.
*/
/***********************************************************/
bool check_valid_ssid() {
// Disconnect WiFi from an AP if it was previously connected
WiFi.disconnect();
delay(100);

// Perform a network scan
int n = WiFi.scanNetworks();
if (n == 0) {
WS_DEBUG_PRINTLN("ERROR: No WiFi networks found!");
return false;
}

// Was the network within secrets.json found?
for (int i = 0; i < n; ++i) {
if (strcmp(_ssid, WiFi.SSID(i)) == 0)
return true;
}

// User-set network not found, print scan results to serial console
WS_DEBUG_PRINTLN("ERROR: Your requested WiFi network was not found!");
WS_DEBUG_PRINTLN("WipperSnapper found these WiFi networks: ");
for (int i = 0; i < n; ++i) {
WS_DEBUG_PRINT(WiFi.SSID(i));
WS_DEBUG_PRINT(" ");
WS_DEBUG_PRINT(WiFi.RSSI(i));
WS_DEBUG_PRINTLN("dB");
}

return false;
}

/********************************************************/
/*!
@brief Sets the WiFi client.
Expand Down
39 changes: 39 additions & 0 deletions src/network_interfaces/Wippersnapper_ESP32.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,45 @@ class Wippersnapper_ESP32 : public Wippersnapper {
_pass = WS._network_pass;
}

/***********************************************************/
/*!
@brief Performs a scan of local WiFi networks.
@returns True if `_network_ssid` is found, False otherwise.
*/
/***********************************************************/
bool check_valid_ssid() {
// Set WiFi to station mode and disconnect from an AP if it was previously
// connected
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);

// Perform a network scan
int n = WiFi.scanNetworks();
if (n == 0) {
WS_DEBUG_PRINTLN("ERROR: No WiFi networks found!");
return false;
}

// Was the network within secrets.json found?
for (int i = 0; i < n; ++i) {
if (strcmp(_ssid, WiFi.SSID(i).c_str()) == 0)
return true;
}

// User-set network not found, print scan results to serial console
WS_DEBUG_PRINTLN("ERROR: Your requested WiFi network was not found!");
WS_DEBUG_PRINTLN("WipperSnapper found these WiFi networks: ");
for (int i = 0; i < n; ++i) {
WS_DEBUG_PRINT(WiFi.SSID(i));
WS_DEBUG_PRINT(" ");
WS_DEBUG_PRINT(WiFi.RSSI(i));
WS_DEBUG_PRINTLN("dB");
}

return false;
}

/********************************************************/
/*!
@brief Sets the ESP32's unique client identifier
Expand Down
39 changes: 39 additions & 0 deletions src/network_interfaces/Wippersnapper_ESP8266.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,45 @@ class Wippersnapper_ESP8266 : public Wippersnapper {
_pass = WS._network_pass;
}

/***********************************************************/
/*!
@brief Performs a scan of local WiFi networks.
@returns True if `_network_ssid` is found, False otherwise.
*/
/***********************************************************/
bool check_valid_ssid() {
// Set WiFi to station mode and disconnect from an AP if it was previously
// connected
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);

// Perform a network scan
int n = WiFi.scanNetworks();
if (n == 0) {
WS_DEBUG_PRINTLN("ERROR: No WiFi networks found!");
return false;
}

// Was the network within secrets.json found?
for (int i = 0; i < n; ++i) {
if (strcmp(_ssid, WiFi.SSID(i).c_str()) == 0)
return true;
}

// User-set network not found, print scan results to serial console
WS_DEBUG_PRINTLN("ERROR: Your requested WiFi network was not found!");
WS_DEBUG_PRINTLN("WipperSnapper found these WiFi networks: ");
for (int i = 0; i < n; ++i) {
WS_DEBUG_PRINT(WiFi.SSID(i));
WS_DEBUG_PRINT(" ");
WS_DEBUG_PRINT(WiFi.RSSI(i));
WS_DEBUG_PRINTLN("dB");
}

return false;
}

/********************************************************/
/*!
@brief Gets the ESP8266's unique client identifier.
Expand Down
39 changes: 39 additions & 0 deletions src/network_interfaces/Wippersnapper_WIFININA.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,45 @@ class Wippersnapper_WIFININA : public Wippersnapper {
WS._network_pass = _pass;
}

/***********************************************************/
/*!
@brief Performs a scan of local WiFi networks.
@returns True if `_network_ssid` is found, False otherwise.
*/
/***********************************************************/
bool check_valid_ssid() {
// Set WiFi to station mode and disconnect from an AP if it was previously
// connected
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);

// Perform a network scan
int n = WiFi.scanNetworks();
if (n == 0) {
WS_DEBUG_PRINTLN("ERROR: No WiFi networks found!");
return false;
}

// Was the network within secrets.json found?
for (int i = 0; i < n; ++i) {
if (strcmp(_ssid, WiFi.SSID(i)) == 0)
return true;
}

// User-set network not found, print scan results to serial console
WS_DEBUG_PRINTLN("ERROR: Your requested WiFi network was not found!");
WS_DEBUG_PRINTLN("WipperSnapper found these WiFi networks: ");
for (int i = 0; i < n; ++i) {
WS_DEBUG_PRINT(WiFi.SSID(i));
WS_DEBUG_PRINT(" ");
WS_DEBUG_PRINT(WiFi.RSSI(i));
WS_DEBUG_PRINTLN("dB");
}

return false;
}

/********************************************************/
/*!
@brief Sets the WiFi client.
Expand Down