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

* Wait for client.available() to prevent ESP32 crashes

* Removed user-specific SSID & passphrase
This commit is contained in:
Frank Paynter 2019-10-02 07:45:42 -04:00 committed by Me No Dev
parent 6f70e27011
commit 4638628873

View File

@ -36,8 +36,10 @@ void setup()
void loop() void loop()
{ {
const uint16_t port = 80; // const uint16_t port = 80;
const char * host = "192.168.1.1"; // ip or dns // 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
Serial.print("Connecting to "); Serial.print("Connecting to ");
Serial.println(host); Serial.println(host);
@ -53,11 +55,29 @@ void loop()
} }
// This will send a request to the server // 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 //read back one line from the server
String line = client.readStringUntil('\r'); String line = client.readStringUntil('\r');
client.println(line); Serial.println(line);
}
else
{
Serial.println("client.available() timed out ");
}
Serial.println("Closing connection."); Serial.println("Closing connection.");
client.stop(); client.stop();
@ -65,4 +85,3 @@ void loop()
Serial.println("Waiting 5 seconds before restarting..."); Serial.println("Waiting 5 seconds before restarting...");
delay(5000); delay(5000);
} }