-
-
Notifications
You must be signed in to change notification settings - Fork 100
WifiS3 - Non blocking Wifi connect #461
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
Changes from all commits
99df703
7350bbb
a260388
2d6b21e
61b798f
b233fe0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -26,12 +26,17 @@ | |||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| #include "arduino_secrets.h" | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| #define MaximumConnections 2 | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| ///////please enter your sensitive data in the Secret tab/arduino_secrets.h | ||||||||||||||||||||||||||||||
| char ssid[] = SECRET_SSID; // your network SSID (name) | ||||||||||||||||||||||||||||||
| char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) | ||||||||||||||||||||||||||||||
| int keyIndex = 0; // your network key index number (needed only for WEP) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| int connectionCount = 0; | ||||||||||||||||||||||||||||||
| bool clientConnected = false; | ||||||||||||||||||||||||||||||
| int status = WL_IDLE_STATUS; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // if you don't want to use DNS (and reduce your sketch size) | ||||||||||||||||||||||||||||||
| // use the numeric IP instead of the name for the server: | ||||||||||||||||||||||||||||||
| //IPAddress server(74,125,232,128); // numeric IP for Google (no DNS) | ||||||||||||||||||||||||||||||
|
|
@@ -62,30 +67,19 @@ void setup() { | |||||||||||||||||||||||||||||
| if (fv < WIFI_FIRMWARE_LATEST_VERSION) { | ||||||||||||||||||||||||||||||
| Serial.println("Please upgrade the firmware"); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // attempt to connect to WiFi network: | ||||||||||||||||||||||||||||||
| while (status != WL_CONNECTED) { | ||||||||||||||||||||||||||||||
| Serial.print("Attempting to connect to SSID: "); | ||||||||||||||||||||||||||||||
| Serial.println(ssid); | ||||||||||||||||||||||||||||||
| // Connect to WPA/WPA2 network. Change this line if using open or WEP network: | ||||||||||||||||||||||||||||||
| status = WiFi.begin(ssid, pass); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // wait 10 seconds for connection: | ||||||||||||||||||||||||||||||
| delay(10000); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| printWifiStatus(); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| Serial.println("\nStarting connection to server..."); | ||||||||||||||||||||||||||||||
| // if you get a connection, report back via serial: | ||||||||||||||||||||||||||||||
| if (client.connect(server, 80)) { | ||||||||||||||||||||||||||||||
| Serial.println("connected to server"); | ||||||||||||||||||||||||||||||
| // Make a HTTP request: | ||||||||||||||||||||||||||||||
| client.println("GET /search?q=arduino HTTP/1.1"); | ||||||||||||||||||||||||||||||
| client.println("Host: www.google.com"); | ||||||||||||||||||||||||||||||
| client.println("Connection: close"); | ||||||||||||||||||||||||||||||
| client.println(); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // 3 second wait for connection | ||||||||||||||||||||||||||||||
| client.setTimeout(3000); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| void connectToWifi() { | ||||||||||||||||||||||||||||||
| if (status != WL_IDLE_STATUS) | ||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| Serial.print("Attempting to connect to SSID: "); | ||||||||||||||||||||||||||||||
| Serial.println(ssid); | ||||||||||||||||||||||||||||||
| // Connect to WPA/WPA2 network. Change this line if using open or WEP network: | ||||||||||||||||||||||||||||||
| status = WiFi.begin(ssid, pass); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /* just wrap the received data up to 80 columns in the serial print*/ | ||||||||||||||||||||||||||||||
|
|
@@ -109,16 +103,56 @@ void read_response() { | |||||||||||||||||||||||||||||
| /* -------------------------------------------------------------------------- */ | ||||||||||||||||||||||||||||||
| void loop() { | ||||||||||||||||||||||||||||||
| /* -------------------------------------------------------------------------- */ | ||||||||||||||||||||||||||||||
| read_response(); | ||||||||||||||||||||||||||||||
| // do some processing | ||||||||||||||||||||||||||||||
| Serial.println("loop processing"); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // only allowed to connect n times | ||||||||||||||||||||||||||||||
| if (connectionCount >= MaximumConnections) { | ||||||||||||||||||||||||||||||
| delay(2000); | ||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // if the server's disconnected, stop the client: | ||||||||||||||||||||||||||||||
| if (!client.connected()) { | ||||||||||||||||||||||||||||||
| Serial.println(); | ||||||||||||||||||||||||||||||
| Serial.println("disconnecting from server."); | ||||||||||||||||||||||||||||||
| client.stop(); | ||||||||||||||||||||||||||||||
| //connect and wait for connection to be made | ||||||||||||||||||||||||||||||
| connectToWifi(); | ||||||||||||||||||||||||||||||
| status = WiFi.isConnected(); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // do nothing forevermore: | ||||||||||||||||||||||||||||||
| while (true); | ||||||||||||||||||||||||||||||
| if (status == WL_CONNECTING) { | ||||||||||||||||||||||||||||||
| Serial.println("Connecting to wifi"); | ||||||||||||||||||||||||||||||
| delay(200); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
| // Handle WiFi connection failure | |
| if (status == WL_CONNECT_FAILED) { | |
| Serial.println("WiFi connection failed!"); | |
| status = WL_IDLE_STATUS; | |
| return; | |
| } |
Copilot
AI
Dec 4, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line uses tabs for indentation while the rest of the file uses spaces. This creates inconsistent formatting. Please use spaces to match the surrounding code style.
| status = WL_IDLE_STATUS; | |
| status = WL_IDLE_STATUS; |
Copilot
AI
Dec 4, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The condition if (clientConnected) on line 146 is redundant. At this point, we're already inside the if (clientConnected) block from line 132. This condition will always be true here. This redundant check should be removed, and the code inside should be dedented accordingly.
| if (clientConnected) { | |
| // if the server's disconnected, stop the client: | |
| if (!client.connected()) { | |
| Serial.println(); | |
| Serial.println("disconnecting from server."); | |
| client.stop(); | |
| status = WL_IDLE_STATUS; | |
| } | |
| // if the server's disconnected, stop the client: | |
| if (!client.connected()) { | |
| Serial.println(); | |
| Serial.println("disconnecting from server."); | |
| client.stop(); | |
| status = WL_IDLE_STATUS; |
Copilot
AI
Dec 4, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the server connection fails, the status is not reset. This means the code will stay in the WL_CONNECTED block and keep trying to connect to the server on every loop iteration. While the if (clientConnected) block won't execute, the loop will still continuously attempt to connect without the proper state management. Consider adding an else clause that handles the connection failure and potentially resets the status or adds a delay.
| } | |
| } | |
| } else { | |
| // Handle server connection failure | |
| Serial.println("Failed to connect to server."); | |
| status = WL_IDLE_STATUS; | |
| delay(1000); // Add a delay to avoid rapid retries |
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -14,11 +14,18 @@ | |||||||||||||||
|
|
||||||||||||||||
| #include "arduino_secrets.h" | ||||||||||||||||
|
|
||||||||||||||||
| // maximum number of times the uri will be checked | ||||||||||||||||
| #define MaximumConnections 2 | ||||||||||||||||
|
|
||||||||||||||||
| ///////please enter your sensitive data in the Secret tab/arduino_secrets.h | ||||||||||||||||
| char ssid[] = SECRET_SSID; // your network SSID (name) | ||||||||||||||||
| char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) | ||||||||||||||||
|
|
||||||||||||||||
|
|
||||||||||||||||
| int connectionCount = 0; | ||||||||||||||||
| bool clientConnected = false; | ||||||||||||||||
|
||||||||||||||||
| bool clientConnected = false; |
Copilot
AI
Dec 4, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The serial baud rate has been changed from 115200 to 9600. This change appears unrelated to the non-blocking WiFi connection feature and slows down serial communication significantly. Unless there's a specific reason for this change, consider reverting to 115200 to maintain consistency with other examples and provide faster serial debugging.
| Serial.begin(9600); | |
| Serial.begin(115200); |
Copilot
AI
Dec 4, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The WL_CONNECT_FAILED state is not handled. If the WiFi connection fails (either immediately or after timeout), the code doesn't reset the status to WL_IDLE_STATUS to allow retry. Consider adding a check: if (status == WL_CONNECT_FAILED) { Serial.println("WiFi connection failed!"); status = WL_IDLE_STATUS; return; } before the WL_CONNECTED check.
| if (status == WL_CONNECT_FAILED) { | |
| Serial.println("WiFi connection failed!"); | |
| status = WL_IDLE_STATUS; | |
| return; | |
| } |
Copilot
AI
Dec 4, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WiFiSSLClient connects to server over TLS without any server certificate validation or hostname verification. An on-path attacker (e.g., rogue AP on local network) can perform a MITM and intercept/modify traffic because the client does not verify the peer; fix by loading and enforcing a trusted CA or server certificate/fingerprint before client.connect, e.g., by calling the appropriate API to set a root CA or pinned certificate and validating the hostname.
Copilot
AI
Dec 4, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the server connection fails, the status is not reset to WL_IDLE_STATUS. This means the code will continuously stay in the WL_CONNECTED block and keep trying to connect to the server on every loop iteration without any delay, potentially causing rapid repeated connection attempts. Add status = WL_IDLE_STATUS; after the error message to reset the state.
| Serial.println("Connection to server failed!"); | |
| Serial.println("Connection to server failed!"); | |
| status = WL_IDLE_STATUS; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
WL_CONNECTINGstate is checked but there's noreturnstatement after the delay. This means the code will continue to execute theif (status == WL_CONNECTED)block on line 125 in the same loop iteration, even when still connecting. Add areturnstatement after the delay on line 121 to prevent continuing execution when in the connecting state.