I'm using ESP32 with ENC28J60 ethernet connector.
I'm trying to retrieve HTTP get from server, but after a few response, http.responseStatusCode() always returns -3, TIMEOUT.
Please let me know how to fix it. Any answer would be greatly appreciated.
#include <WiFi.h>
#include <HttpClient.h>
#include <EthernetENC.h>
String sChipID;
char cChipID[40];
byte mac[6];
bool isConnected = false;
// Number of milliseconds to wait without receiving any data before we give up
const int kNetworkTimeout = 30*1000;
// Number of milliseconds to wait if no data is available before trying again
const int kNetworkDelay = 100;
void setup() {
// put your setup code here, to run once:
uint8_t chipid[6] = "";
int ethernetresult;
Serial.begin(9600);
WiFi.macAddress(chipid);
sprintf(cChipID, "%02X%02X%02X%02X%02X%02X", chipid[5], chipid[4], chipid[3], chipid[2], chipid[1], chipid[0]);
memcpy(mac, chipid, 6);
Serial.println("Trying DHCP...");
ethernetresult = Ethernet.begin(mac);// Use DHCP
if( ethernetresult != 0 )
{
Serial.print("Local IP : ");
Serial.println(Ethernet.localIP());
Serial.print("Subnet Mask : ");
Serial.println(Ethernet.subnetMask());
Serial.print("Gateway IP : ");
Serial.println(Ethernet.gatewayIP());
Serial.print("DNS Server : ");
Serial.println(Ethernet.dnsServerIP());
isConnected = true;
}
else
{
Serial.println("DHCP not available.");
}
}
void loop() {
// put your main code here, to run repeatedly:
EthernetClient eclient;
HttpClient http(eclient);
String response = "";
String httpServer = "192.168.10.250";
String httpURI = "/webjob/";
String http_request = "broker_wakeup.jsp?sn=Q123A-DA305-0008";
uint16_t httpPort = 8080;
int err;
int curmillis;
bool ret = false;
err = http.get(httpServer.c_str(), httpPort, (httpURI + http_request).c_str());
if( err == 0 )
{
Serial.println("startedRequest ok");
err = http.responseStatusCode();
if (err >= 0)
{
Serial.print("Got status code: ");
Serial.println(err);
// Usually you'd check that the response code is 200 or a
// similar "success" code (200-299) before carrying on,
// but we'll print out whatever response we get
err = http.skipResponseHeaders();
if (err >= 0)
{
int bodyLen = http.contentLength();
Serial.print("Response length: ");
Serial.println(bodyLen);
// Now we've got to the body, so we can print it out
unsigned long timeoutStart = millis();
char c;
// Whilst we haven't timed out & haven't reached the end of the body
while ( (http.connected() || http.available()) &&
((millis() - timeoutStart) < kNetworkTimeout) &&
(bodyLen > 0) )
{
if (http.available())
{
c = http.read();
response += c;
bodyLen--;
// We read something, reset the timeout counter
timeoutStart = millis();
}
else
{
// We haven't got any data, so let's pause to allow some to
// arrive
delay(kNetworkDelay);
}
}
if( bodyLen == 0 )
{
ret = true;
}
}
else
{
Serial.print("Failed to skip response headers: ");
Serial.println(err);
}
}
else
{
Serial.print("Getting response failed: ");
Serial.println(err);
}
}
else
{
Serial.print("Connect failed: ");
Serial.println(err);
}
http.stop();
if( err <= 0 )
{
}
if( ret )
{
Serial.println(response);
}
delay(1000);
}
Output:
Trying DHCP...
16:44:13.495 -> Local IP : 192.168.10.64
16:44:13.541 -> Subnet Mask : 255.255.255.0
16:44:13.541 -> Gateway IP : 192.168.10.1
16:44:13.587 -> DNS Server : 203.248.252.2
16:44:14.011 -> startedRequest ok
16:44:17.029 -> Got status code: 200
16:44:17.075 -> Response length: 44
16:44:17.075 ->
16:44:17.075 ->
16:44:17.075 -> INTERVAL=1
16:44:17.075 -> TIME=2022-12-12 16:44:14
16:44:17.121 ->
16:44:17.121 ->
16:44:18.255 -> startedRequest ok
16:44:21.234 -> Got status code: 200
16:44:21.281 -> Response length: 44
16:44:21.281 ->
16:44:21.281 ->
16:44:21.281 -> INTERVAL=1
16:44:21.328 -> TIME=2022-12-12 16:44:18
16:44:21.328 ->
16:44:21.328 ->
16:44:22.460 -> startedRequest ok
16:44:25.461 -> Got status code: 200
16:44:25.461 -> Response length: 44
16:44:25.507 ->
16:44:25.507 ->
16:44:25.507 -> INTERVAL=1
16:44:25.507 -> TIME=2022-12-12 16:44:22
16:44:25.554 ->
16:44:25.554 ->
16:44:26.683 -> startedRequest ok
16:44:29.655 -> Got status code: 200
16:44:29.701 -> Response length: 44
16:44:29.701 ->
16:44:29.701 ->
16:44:29.701 -> INTERVAL=1
16:44:29.701 -> TIME=2022-12-12 16:44:26
16:44:29.748 ->
16:44:29.748 ->
16:44:30.868 -> startedRequest ok
16:45:00.886 -> Getting response failed: -3
16:45:02.066 -> startedRequest ok
16:45:32.077 -> Getting response failed: -3
16:45:33.250 -> startedRequest ok