-
-
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 2 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 1 | ||||||||||||||||||
|
|
||||||||||||||||||
| ///////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) | ||||||||||||||||||
|
|
@@ -46,7 +51,7 @@ WiFiClient client; | |||||||||||||||||
| void setup() { | ||||||||||||||||||
| /* -------------------------------------------------------------------------- */ | ||||||||||||||||||
| //Initialize serial and wait for port to open: | ||||||||||||||||||
| Serial.begin(9600); | ||||||||||||||||||
| Serial.begin(115200); | ||||||||||||||||||
| while (!Serial) { | ||||||||||||||||||
| ; // wait for serial port to connect. Needed for native USB port only | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
@@ -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.setConnectionTimeout(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,55 @@ void read_response() { | |||||||||||||||||
| /* -------------------------------------------------------------------------- */ | ||||||||||||||||||
| void loop() { | ||||||||||||||||||
| /* -------------------------------------------------------------------------- */ | ||||||||||||||||||
| read_response(); | ||||||||||||||||||
| // do some processing | ||||||||||||||||||
| Serial.println("loop processing"); | ||||||||||||||||||
|
|
||||||||||||||||||
| // only allowed to connect n times | ||||||||||||||||||
| if (connectionCount >= MaximumConnections) { | ||||||||||||||||||
| delay(300); | ||||||||||||||||||
| 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); | ||||||||||||||||||
|
||||||||||||||||||
| delay(200); | |
| delay(200); | |
| 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.
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.
| // 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.
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 |
k3ldar marked this conversation as resolved.
Show resolved
Hide resolved
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -14,11 +14,17 @@ | |||||||||||||||
|
|
||||||||||||||||
| #include "arduino_secrets.h" | ||||||||||||||||
|
|
||||||||||||||||
| #define MaximumConnections 1 | ||||||||||||||||
|
|
||||||||||||||||
| ///////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; |
k3ldar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
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; | |
| } |
Uh oh!
There was an error while loading. Please reload this page.