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

Sorting of scanned wifi networks #661

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

tyeth
Copy link
Contributor

@tyeth tyeth commented Nov 13, 2024

Addresses #659

@tyeth
Copy link
Contributor Author

tyeth commented Nov 14, 2024

Works on Pico, which is what I was after as it was affecting my testing of other things.

No known network sorted response details

---- Reopened serial port COM40 ----
Found single wifi network in secrets.json
Adafruit.io WipperSnapper
-------Device Information-------
Firmware Version: 1.0.0-beta.92
Board ID: rpi-pico-w
Adafruit.io User: tyeth
WiFi Network: free4all_2G
MAC Address: 28:CD:C1:0E:17:E8
-------------------------------
Generating device's MQTT topics...
Running Network FSM...
Establishing network connection...
Performing a WiFi scan for SSID...ERROR: Your requested WiFi network was not found!
WipperSnapper found these WiFi networks: 
free4all -50dB
free4all -52dB
free4all -55dB
Tenda_60C06C -79dB
free4all -82dB
ERROR [WDT RESET]: ERROR: Unable to find WiFi network, rebooting soon...
ERROR [WDT RESET]: ERROR: Unable to find WiFi network, rebooting soon...

And success (failing on connect attempt #0, but succeeding on # 1 aka 2nd attempt):

Details

---- Opened the serial port COM40 ----
Connecting to WiFi (attempt #1)
Connected to WiFi!
Connecting to AIO MQTT (attempt #0)
WiFi Status: 20
Registering hardware with WipperSnapper...
Registering hardware with IO...
Encoding registration request...Encoding registration msg...Published!
Polling for registration message response...2
GOT Registration Response Message:
Hardware Response Msg:
        GPIO Pins: 31
        Analog Pins: 4
        Reference voltage: 3.30v
Completed registration process, configuration next!
Polling for message containing hardware configuration...
Polling for message containing hardware configuration...
Polling for message containing hardware configuration...
cbSignalTopic: New Msg on Signal Topic
2 bytes.
decodeSignalMsg
cbSignalMsg
Sub-messages found: 1
Signal Msg Tag: Pin Configuration
Initial Pin Configuration Complete!
Publishing to pin config complete...
Hardware configured successfully!
Registration and configuration complete!
Running application...
Sending MQTT PING: SUCCESS!
WiFi RSSI: -36
Sending MQTT PING: SUCCESS!
WiFi RSSI: -44
Sending MQTT PING: SUCCESS!
WiFi RSSI: -53
Sending MQTT PING: SUCCESS!
WiFi RSSI: -43
Sending MQTT PING: SUCCESS!

@tyeth tyeth marked this pull request as ready for review November 14, 2024 20:51
@tyeth tyeth changed the title DRAFT CI Assets - Initial sorting of scanned wifi networks Sorting of scanned wifi networks Nov 14, 2024
@tyeth tyeth requested a review from brentru November 15, 2024 15:16
@tyeth
Copy link
Contributor Author

tyeth commented Nov 15, 2024

@brentru this is ready for review. I should retest on the other platforms, especially samd.

Copy link
Member

@brentru brentru left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tyeth I have a few review notes below and also some questions:

  1. Do we take the network with the highest RSSI?
  2. If we fail to connect to the defined network, do we try again with the next network in our list of networks?
  3. What is the purpose of sorting by RSSI if we are not going to take the network with the largest RSSI or if its not defined?

/****************************************************************/
struct WiFiNetwork {
char ssid[33]; /*!< SSID (Max 32 characters + null terminator */
int rssi; /*!< Received Signal Strength Indicator */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RSSI should be type int32_t to match the WiFi library

@@ -119,22 +145,38 @@ class Wippersnapper_AIRLIFT : public Wippersnapper {
return false;
}

// Was the network within secrets.json found?
// Dynamically allocate memory for the network list
std::vector<WiFiNetwork> networks(n);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure about using a vector/dynamic alloc. here, allowing too many possible networks will increase sort time.

Instead, could you define a global MAX_WIFI_NETWORK macro of 15 possible network options?

*/
/****************************************************************/
struct WiFiNetwork {
char ssid[33]; /*!< SSID (Max 32 characters + null terminator */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like SSID is a string in the WiFi library, so we should match the type
https://github.com/espressif/arduino-esp32/blob/master/libraries/WiFi/src/WiFiScan.h#L48

WS_DEBUG_PRINT("SSID found! RSSI: ");
WS_DEBUG_PRINTLN(WiFi.RSSI(i));
strncpy(networks[i].ssid, WiFi.SSID(i), sizeof(networks[i].ssid) - 1);
networks[i].ssid[sizeof(networks[i].ssid) - 1] =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you switch SSID to a String type, it will be null-terminated during compilation.

@@ -26,6 +26,8 @@
#include "SPI.h"
#include "WiFiNINA.h"
#include "Wippersnapper.h"
#include <algorithm>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want this here, or moved to be included by WipperSnapper.h or another file (networking.h?) so it's included by all the drivers?

std::sort(networks.begin(), networks.end(), compareByRSSI);

// Was the network within secrets.json found?
for (const auto &network : networks) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we using autotype assignment here? Instead of this overhead, we could iterate until the end of the array

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants