Skip to content

Wait for client.available() to prevent ESP32 crashes #3154

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

Merged
merged 3 commits into from
Oct 2, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions libraries/WiFi/examples/WiFiClientBasic/WiFiClientBasic.ino
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ void setup()

void loop()
{
const uint16_t port = 80;
const char * host = "192.168.1.1"; // ip or dns
// const uint16_t port = 80;
// const char * host = "192.168.1.1"; // ip or dns
const uint16_t port = 1337;
const char * host = "192.168.1.10"; // ip or dns
Copy link
Member

Choose a reason for hiding this comment

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

also unrelated change


Serial.print("Connecting to ");
Serial.println(host);
Expand All @@ -53,16 +55,33 @@ void loop()
}

// This will send a request to the server
client.print("Send this data to the server");

//uncomment this line to send an arbitrary string to the server
//client.print("Send this data to the server");
//uncomment this line to send a basic document request to the server
client.print("GET /index.html HTTP/1.1\n\n");

int maxloops = 0;

//wait for the server's reply to become available
while (!client.available() && maxloops < 1000)
{
maxloops++;
delay(1); //delay 1 msec
}
if (client.available() > 0)
{
//read back one line from the server
String line = client.readStringUntil('\r');
Copy link
Member

Choose a reason for hiding this comment

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

so the issue is that readStringUntil timeouts

client.println(line);
Serial.println(line);
}
else
{
Serial.println("client.available() timed out ");
}

Serial.println("Closing connection.");
client.stop();

Serial.println("Waiting 5 seconds before restarting...");
delay(5000);
}