8000 Wait for client.available() to prevent ESP32 crashes (#3154) · espressif/arduino-esp32@4638628 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4638628

Browse files
paynterfme-no-dev
authored andcommitted
Wait for client.available() to prevent ESP32 crashes (#3154)
* Wait for client.available() to prevent ESP32 crashes * Removed user-specific SSID & passphrase
1 parent 6f70e27 commit 4638628

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

libraries/WiFi/examples/WiFiClientBasic/WiFiClientBasic.ino

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ void setup()
3636

3737
void loop()
3838
{
39-
const uint16_t port = 80;
40-
const char * host = "192.168.1.1"; // ip or dns
39+
// const uint16_t port = 80;
40+
// const char * host = "192.168.1.1"; // ip or dns
41+
const uint16_t port = 1337;
42+
const char * host = "192.168.1.10"; // ip or dns
4143

4244
Serial.print("Connecting to ");
4345
Serial.println(host);
@@ -53,16 +55,33 @@ void loop()
5355
}
5456

5557
// This will send a request to the server
56-
client.print("Send this data to the server");
57-
58+
//uncomment this line to send an arbitrary string to the server
59+
//client.print("Send this data to the server");
60+
//uncomment this line to send a basic document request to the server
61+
client.print("GET /index.html HTTP/1.1\n\n");
62+
63+
int maxloops = 0;
64+
65+
//wait for the server's reply to become available
66+
while (!client.available() && maxloops < 1000)
67+
{
68+
maxloops++;
69+
delay(1); //delay 1 msec
70+
}
71+
if (client.available() > 0)
72+
{
5873
//read back one line from the server
5974
String line = client.readStringUntil('\r');
60-
client.println(line);
75+
Serial.println(line);
76+
}
77+
else
78+
{
79+
Serial.println("client.available() timed out ");
80+
}
6181

6282
Serial.println("Closing connection.");
6383
client.stop();
6484

6585
Serial.println("Waiting 5 seconds before restarting...");
6686
delay(5000);
6787
}
68-

0 commit comments

Comments
 (0)
0