Skip to content

Commit

Permalink
Wait for client.available() to prevent ESP32 crashes (#3154)
Browse files Browse the repository at this point in the history
* Wait for client.available() to prevent ESP32 crashes

* Removed user-specific SSID & passphrase
  • Loading branch information
paynterf authored and me-no-dev committed Oct 2, 2019
1 parent 6f70e27 commit 4638628
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions libraries/WiFi/examples/WiFiClientBasic/WiFiClientBasic.ino
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

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');
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);
}

0 comments on commit 4638628

Please sign in to comment.