WifiClientBasic connects to host, but doesn't send anything
Posted: Sun Aug 25, 2019 3:18 pm
I am trying to get the ESP32 WifiClientBasic example to work, but I can't get the ESP32 to actually send anything to the host.
Using Node.js I set up a localhost server on my Windows 10 laptop so I can monitor requests to the localhost as they are received. I tested this by typing 'http://127.0.0.1:1337/index.html' into my browser's URL window, and getting the appropriate page displayed. In addition, the localhost monitor shows the appropriate request ( "GET /index.html" ).
However, when I run the WifiClientBasic example, the ESP32 crashes with a 'Guru Meditation Error'. After some investigation, it appears that the reason for the crash is that the example is attempting to read bytes from the localhost but there aren't any to read. If I check for client.available() before the read, I can see that there are 0 bytes available. If I guard the read line with 'if(client.available()), then the ESP32 doesn't crash.
So, it seems pretty clear that the ESP32 isn't actually sending anything to the localhost server, but I don't have a clue why. I have tried all sorts of variations on the text in the client.println() line, but nothing seems to work, and I can't see any activity on the localhost monitor.
Any idea what I'm doing wrong here? I have included the code (minus my actual SSID and password) below.
Using Node.js I set up a localhost server on my Windows 10 laptop so I can monitor requests to the localhost as they are received. I tested this by typing 'http://127.0.0.1:1337/index.html' into my browser's URL window, and getting the appropriate page displayed. In addition, the localhost monitor shows the appropriate request ( "GET /index.html" ).
However, when I run the WifiClientBasic example, the ESP32 crashes with a 'Guru Meditation Error'. After some investigation, it appears that the reason for the crash is that the example is attempting to read bytes from the localhost but there aren't any to read. If I check for client.available() before the read, I can see that there are 0 bytes available. If I guard the read line with 'if(client.available()), then the ESP32 doesn't crash.
So, it seems pretty clear that the ESP32 isn't actually sending anything to the localhost server, but I don't have a clue why. I have tried all sorts of variations on the text in the client.println() line, but nothing seems to work, and I can't see any activity on the localhost monitor.
Any idea what I'm doing wrong here? I have included the code (minus my actual SSID and password) below.
Code: Select all
/*
* This sketch sends a message to a TCP server
*
*/
//#include <WiFi.h>
#include <WiFiMulti.h>
WiFiMulti WiFiMulti;
void setup()
{
Serial.begin(115200);
delay(10);
Serial.println("Hello");
// We start by connecting to a WiFi network
WiFiMulti.addAP("SSID", "password");
Serial.println();
Serial.println();
Serial.print("Waiting for WiFi... ");
while(WiFiMulti.run() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
delay(500);
}
char buffer[100] = { "some buffer contents" };
void loop()
{
const uint16_t port = 1337;
const char * host = "http://127.0.0.1"; // ip or dns
Serial.print("Connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient client;
if (!client.connect(host, port)) {
Serial.println("Connection failed.");
Serial.println("Waiting 5 seconds before retrying...");
delay(5000);
return;
}
// This will send a request to the server
client.print("GET ./index.html");
//read back one line from the server
//String line = client.readStringUntil('\r');
//client.println(line);
int numbytesavail = client.available();
if (numbytesavail > 0)
{
int numbytes = 0;
numbytes = client.readBytes(buffer, sizeof(buffer)); // read chars from stream into buffer
Serial.print("got "); Serial.print(numbytes); Serial.print(": "); Serial.println(buffer);
}
else
{
Serial.print("client.available() returned "); Serial.println(numbytesavail);
}
Serial.println("Closing connection.");
client.stop();
Serial.println("Waiting 5 seconds before restarting...");
delay(5000);
}