WiFiClient drops connection while waiting for response from server. :rcl
- Dominant language
- C++
- Stars
- 16.7k
- Forks
- 13.1k
- PR merge metrics
- No merged PRs in 30d
Description
### Basic Infos
- [X] This issue complies with the [issue POLICY doc](https://github.com/esp8266/Arduino/blob/master/POLICY.md).
- [X] I have read the documentation at [readthedocs](https://arduino-esp8266.readthedocs.io/en/latest) and the issue is not addressed there.
- [X] I have tested that the issue is present in current master branch (aka latest git).
- [X] I have searched the issue tracker for a similar issue.
- [ ] If there is a stack dump, I have decoded it.
- [X] I have filled out all fields below.
#### Platform
- Hardware: ESP8266
- Core Version: 2019-03-13
- Development Env: Arduino IDE
- Operating System: Windows and Fedora
### Settings in IDE
- Module: Wemos D1 mini r2
- Flash Mode: dio
- Flash Size: 4MB
- lwip Variant: v2 Lower Memory
- Reset Method: [ck|nodemcu]
- Flash Frequency: 40Mhz
- CPU Frequency: 80Mhz
- Upload Using: SERIAL
- Upload Speed: 115200
- Debug Port: Serial1
- Debug Level: CORE
### Problem Description
_Everything here deals with the ESP8266WiFi library_
While waiting for response to a HTTP request, something causes the pbuf* pb to become 0, closing the connection through abort()
As seen in the debug output, the request is sometimes successfull but mostly fails. Even the last example failed, as the body of the response was lost. You can see it almost printed after :abort the response is "OK"
### [MCVE](https://stackoverflow.com/help/mcve) Sketch
```cpp
extern "C" void system_set_os_print(uint8);
#define HTTP_DEBUG_PRINT(s) Serial.print(s);
#include "ESP8266WiFi.h"
#include "WiFiClient.h"
WiFiClient client;
String response;
const char host[] = "esp-rest-test.herokuapp.com";
int port = 80;
const char ssid[] = "fairBacon";
const char pass[] = "FuglenHenningFlyverIkke";
void setup() {
Serial.begin(115200);
Serial.setDebugOutput(true);
system_set_os_print(100);
WiFi.begin(ssid, pass);
while ( WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print('.');
}
Serial.println("\nWiFi connected");
Serial.println(get());
}
void loop() {
delay(0);
}
int get() {
HTTP_DEBUG_PRINT("HTTP: connect\n");
if (!client.connect(host, port)) {
HTTP_DEBUG_PRINT("HTTP Connection failed\n");
return 0;
}
HTTP_DEBUG_PRINT("HTTP: connected\n");
HTTP_DEBUG_PRINT("REQUEST: \n");
String request = String("GET") + " " + String("/") + " HTTP/1.1\r\n";
request += "Host: " + String(host) + ":" + String(port) + "\r\n";
request += "Connection: close\r\n";
request += "\r\n";
delay(0);
client.print(request.c_str());
HTTP_DEBUG_PRINT("\nEND REQUEST\n");
HTTP_DEBUG_PRINT("HTTP: call readResponse\n");
int statusCode = readResponse(&response);
HTTP_DEBUG_PRINT("HTTP: return readResponse\n");
//cleanup
HTTP_DEBUG_PRINT("HTTP: stop client\n");
client.stop();
delay(50);
HTTP_DEBUG_PRINT("HTTP: client stopped\n");
return statusCode;
}
int readResponse(String * response) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
boolean httpBody = false;
boolean inStatus = false;
char statusCode[4];
int i = 0;
int code = 0;
if (response == NULL) {
HTTP_DEBUG_PRINT("HTTP: NULL RESPONSE POINTER: \n");
} else {
HTTP_DEBUG_PRINT("HTTP: NON-NULL RESPONSE POINTER: \n");
}
HTTP_DEBUG_PRINT("HTTP: RESPONSE: \n");
void * http_client;
while (client.connected()) {
HTTP_DEBUG_PRINT(".");
delay(0);
if (client.available()) {
HTTP_DEBUG_PRINT(",");
delay(0);
char c = client.read();
HTTP_DEBUG_PRINT(c);
if (c == ' ' && !inStatus) {
inStatus = true;
}
if (inStatus && i < 3 && c != ' ') {
statusCode[i] = c;
i++;
}
if (i == 3) {
statusCode[i] = '\0';
code = atoi(statusCode);
}
if (httpBody) {
//only write response if its not null
if (response != NULL) response->concat(c);
} else {
if (c == '\n' && currentLineIsBlank) {
httpBody = true;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
} else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
}
HTTP_DEBUG_PRINT("HTTP: return readResponse3\n");
return code;
}
```
### Debug Messages
```
SDK:3.0.0-dev(c0f7b44)/Core:2.5.0=20500000/lwIP:STABLE-2_1_2_RELEASE/glue:1.1/BearSSL:6778687
sta config unchangedscandone
scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 7
cnt
connected with fairBacon, channel 11
dhcp client start...
...ip:192.168.0.132,mask:255.255.255.0,gw:192.168.0.1
.
WiFi connected
HTTP: connect
:ref 1
HTTP: connected
REQUEST:
:wr 75 0
:wrc 75 75 0
END REQUEST
HTTP: call readResponse
HTTP: NON-NULL RESPONSE POINTER:
HTTP: RESPONSE:
..............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................:ack 75
........................................................................:rn 172
,H.,T.:rcl
:abort
,THTTP: return readResponse3
HTTP: return readResponse
HTTP: stop client
HTTP: client stopped
0
SDK:3.0.0-dev(c0f7b44)/Core:2.5.0=20500000/lwIP:STABLE-2_1_2_RELEASE/glue:1.1/BearSSL:6778687
sta config unchangedscandone
scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 7
cnt
connected with fairBacon, channel 11
dhcp client start...
...ip:192.168.0.132,mask:255.255.255.0,gw:192.168.0.1
.
WiFi connected
HTTP: connect
:ref 1
HTTP: connected
REQUEST:
:wr 75 0
:wrc 75 75 0
END REQUEST
HTTP: call readResponse
HTTP: NON-NULL RESPONSE POINTER:
HTTP: RESPONSE:
............................................................................................................................................................................................................................................................................................................................................................................................................................................................................:ack 75
...............................................................................................:rn 172
,H.,T.,T.,P.,/.,1.,..:rcl
:abort
,1HTTP: return readResponse3
HTTP: return readResponse
HTTP: stop client
HTTP: client stopped
0
SDK:3.0.0-dev(c0f7b44)/Core:2.5.0=20500000/lwIP:STABLE-2_1_2_RELEASE/glue:1.1/BearSSL:6778687
sta config unchangedscandone
scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 7
cnt
connected with fairBacon, channel 11
dhcp client start...
...ip:192.168.0.132,mask:255.255.255.0,gw:192.168.0.1
.
WiFi connected
HTTP: connect
:ref 1
HTTP: connected
REQUEST:
:wr 75 0
:wrc 75 75 0
END REQUEST
HTTP: call readResponse
HTTP: NON-NULL RESPONSE POINTER:
HTTP: RESPONSE:
.............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................:ack 75
..................................................................:rn 172
,H.,T.,T.,P.,/.,1.,..,1., .,2.,0.,0., .:rcl
:abort
,OHTTP: return readResponse3
HTTP: return readResponse
HTTP: stop client
HTTP: client stopped
200
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.