Closed
Description
Basic Infos
- This issue complies with the issue POLICY doc.I have read the documentation at readthedocs and the issue is not addressed there.I have tested that the issue is present in current master branch (aka latest git).I have searched the issue tracker for a similar issue.If there is a stack dump, I have decoded it.I have filled out all fields below.
Platform
- Hardware: [ESP8266EX]
- Core Version: [2.5.2]
- Development Env: [Arduino IDE]
- Operating System: [Windows]
Settings in IDE
- Module: [Generic ESP8266 Module]
- Flash Mode: [DOUT (COMPATIBLE)]
- Flash Size: [1MB]
- lwip Variant: [v2 Lower Memory]
- Reset Method: [ck]
- Flash Frequency: [40Mhz]
- CPU Frequency: [80Mhz]
- Upload Using: [SERIAL]
- Upload Speed: [115200]
- Espressif FW: [nono-sdk 2.2.1]
Problem Description
Hello, I have the following problem:
I am using a esp8266, via an httpclient to connect to a server, but after some time (random), it returns me the following error:
httpCode: -1 (Connection Refused). With wireshark I noticed that it [TCP PORT numbers reused] 57490-> 8000 [SYN].
I would like to know what I am doing wrong in my code to receive this error.
[Sketch]
#include <Arduino.h>
#include <ESP8266WiFi.h> /// fix http://blog.flynnmetrics.com/uncategorized/esp8266-exception-3/
#include <ESP8266HTTPClient.h>
//#include <WiFiClient.h>
#include <ESPAsyncWebServer.h>
#include <ArduinoJson.h>
#include <ESP8266httpUpdate.h>
#include <Hash.h>
#include <FS.h>
#include <user_interface.h> //Biblioteca necessaria para acessar os Timer`s.
#include "OneButton.h" // https://github.com/mathertel/OneButton
#include <EEPROM.h>
#include <WiFiUdp.h>
bool connect2WiFI()
{
WiFi.setAutoReconnect( true );
#if (ADEBUG == 1 )
Serial.printf("Wifi State changed to %s\n", WlStatusToStr(WiFi.status()));
#endif
WiFi.persistent(false); // Solve possible wifi init errors (re-add at 6.2.1.16 #4044, #4083)
WiFi.disconnect(true); // Delete SDK wifi config
delay(200);
WiFi.mode(WIFI_STA); // Disable AP mode
WiFi.setSleepMode(WIFI_MODEM_SLEEP); // Disable sleep (Esp8288/Arduino core and sdk default)
WiFi.begin("DEMO", "DEMO");
uint32_t AwifiTimeout = 20000;
uint32_t maxTime = millis() + AwifiTimeout;
while ((WiFi.status() != WL_CONNECTED) && (millis() < maxTime)) {
yield();
}
if (WiFi.status() != WL_CONNECTED)
{
return false;
}
Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer
return true;
}
bool test()
{
more_send_data:
int txlen = 0;
WiFiClient client;
HTTPClient http;
http.setTimeout(500); // 500ms
String md_ip = "192.168.0.31";
String md_port = "8000";
String path = "http://" + md_ip + ":" + md_port + "/index.php";
const char * headerkeys[] = {"Set-Cookie", "Cookie"} ;
size_t headerkeyssize = sizeof(headerkeys) / sizeof(char*);
http.begin(client, path); //Specify request destination
// http.begin(path);
http.addHeader("Content-Type", "application/json"); //Specify content-type header
http.collectHeaders(headerkeys, headerkeyssize);
int httpCode = http.POST("Hello"); //Send the request
String payload = http.getString(); //Get the response payload
if (httpCode > 0)
{
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
http.end(); //Close connection
return true;
} // end payload != ""
client.stop();
http.end(); //Close connection
return true;
}
Serial.print("httpCode:");
Serial.println(httpCode, DEC );
http.end(); //Close connection
return false;
}
void setup() {
// put your setup code here, to run once:
WiFi.mode(WIFI_STA);
EEPROM.begin(512);
Serial.begin(115200);
connect2WiFI();
}
void loop() {
// put your main code here, to run repeatedly:
test();
delay(500);
}
Debug Messages
16:33:26.338 -> 192.168.0.192
16:33:34.254 -> pm open,type:2 0
16:33:37.970 -> httpCode:-1
wireshark report :
https://mega.nz/#!jJ4wRIyQ!6ayDTjoxEgaxO82yBo-0I4PwqH33TpPVC9XijBe2EXg
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
devyte commentedon Jun 6, 2019
Please don't post links to external sources, especially to files uploaded, because there is no assurance the files will live in the future. Instead, reduce the log by filtering out unrelevant things (if you haven't already done so) and post the log directly here with markup.
jpboucher commentedon Jun 14, 2019
You are probably running out of sockets after a while. Try calling http.setReuse(true) before you call http.begin
d-a-v commentedon Jun 17, 2019
Same sketch is running flawlessly here.
I think your server is busy for more than 0.5 seconds and the esp times out as you asked it to.
Failing request starts:
lots of https at the same time:
esp times out:
next requests are honored:
Be nicer with your server, increase ESP timeout.
TD-er commentedon Jun 18, 2019
@jpboucher
I've looked into the code and it seems it does keep the connection open.
What's the default for max. open connections?
What will happen if that maximum is hit?
jpboucher commentedon Jun 18, 2019
The default is 5. It can be increased up to 15 using espconn_tcp_set_max_con() from the ESP SDK.
If the maximum is hit, you will not be able to create a new socket and the connection will fail. The request will return HTTPC_ERROR_CONNECTION_REFUSED (-1)
d-a-v commentedon Jun 18, 2019
It doesn't. Variable scope implies the destructor is called and the connection closed at every loop (on
test()
ending, becauseWiFiClient
andHTTPClient
are declared in there).We don't depend on that. It's for
espconn
API. Your only limitation is RAM.jpboucher commentedon Jun 18, 2019
My bad. Since everything is indeed cleared at every loop I would also suspect that the server is too slow.
TD-er commentedon Jun 18, 2019
I meant the code of HTTPClient.
So as long as the client object does exist, it will keep the connection open so it seems. (with
_reuse
set)devyte commentedon Feb 2, 2020
This was reported for 2.5.2. There have been several critical stability fixes merged since then. Please retest with 2.6.3 or latest git.
earlephilhower commentedon May 26, 2020
Closing as last update was Jun 18, there have been many HTTPClient updates, and there's been no response to @devyte's request for a confirmation this is still an issue in 2.7+ for ~4 months. If it does still show up in latest master, please open a new bug with MCVE/etc. so we can look at it.
beicnet commentedon Jun 12, 2020
@earlephilhower , @devyte The issue with -1 still persists, went today from 2.3.0 to 2.7.1, but it's got worse, now I have delay, freezing issues too.
This function is called every 10 seconds:
earlephilhower commentedon Jun 12, 2020
@beicnet, Can you please open a new issue with MCVE (and public file maybe on your GH acct or something so we can run it in our own environments) and also enable full debugging to get more useful logs? This is a closed issue from a long time ago, so it won't get attention otherwise.
beicnet commentedon Jun 12, 2020
Hey @earlephilhower thank you for your fast response! ;)
MCVE? Huh, there are 12 library included in my project, and also there are a lot of modules attached to the WeMos D1 mini board.
Can you tell me which one is the full debug in Tools > Debug level?
I'm still using Arduino 1.8.2 with 2.3.0 and everything was fine except that HTTP random response -1, and -2 code (as we speak I got -11 too).
30 remaining items
beicnet commentedon Jun 21, 2020
@TD-er Can you please verify for me something?
TD-er commentedon Jun 21, 2020
@beicnet What do you want me to verify?
beicnet commentedon Jun 21, 2020
@TD-er Do you have a PN532 module by your side?
TD-er commentedon Jun 21, 2020
Not sure, have to dig in the drawer(s) of sensors.
beicnet commentedon Jun 21, 2020
@TD-er I think there is an incompatibility in Adafruits PN532 and HTTPClient library, but I'm not quite sure, because there is no other person to confirm it, that's why I asked you for the module and to test it in SPI mode.
I can give you the MCVE to test it if you would willing to do it for me.
You need WeMos D1 mini, PN532 module and a 6-pin Dupont cable.
earlephilhower commentedon Jun 21, 2020
I've gone over this and what I get as the end result is that with a specific NFC sensor placed next to the 8266 is enabled, WiFi has issues which are reflected in HTTPClient returning (correctly) failure occasionally. I'm not seeing any core issue here, more of a RF or power supply one.
Given that, this is not the right place for a HW discussion like this, so I suggest moving to https://esp8266.com or https://gitter.im/esp8266/Arduino . Closing.
beicnet commentedon Jun 21, 2020
@earlephilhower If you put the NFC reader to 10cm dupont cable, you got the same results.
Did you test it or? How did you come to that conclusion?
beicnet commentedon Jun 26, 2020
@earlephilhower @d-a-v I also tried across 20cm Dupont cable and the results are the same.
So, I don't think it's a hardware nor power supply issue.
I think it's library incompatibility (some sort of timing) issue.
You can read my full description on the forum.
d-a-v commentedon Jun 26, 2020
To summarize: when library is not used, httpclient works.
When library is used, httpclient sometimes does not work.
When it does not work, it is unrelated to the sensor library: httpclient request is asking for some version.
Relevant code is
Can you enable terminal timestamp and show an error with all debug options enabled ?
Better, also with these prints (so we know when things happen)
even better, with the wireshark log matching timestamp (netdump, netdumpv2 would have been useful here)
beicnet commentedon Jun 26, 2020
@d-a-v Thank you for replying, your summation is 99% correct.
with Function Enabled:
with Function Disabled:
Also I tried to implement the
ArduinoHttpClient.h
library from another source, but got almost the same results.d-a-v commentedon Jun 26, 2020
With console timestamps + wireshark, or with one of the two netdumps, as described above, we will be able to check if packets are arriving onto the esp during the http timeout.
beicnet commentedon Jun 26, 2020
@d-a-v I accidentally found out the solution,...
I put my hand onto PN532 module and it's started to give correct reading/requests.
If I removed my hand from it then I got bad reading/requests again.
And it's no matter how long cable you will put on the PN532 module (10cm or 20cm) or distance you put the module from ESP device.
Even after I taped Copper foil on the back of the PN532 module wouldn't do nothing.
@earlephilhower was almost right about one thing, but it was not the RF signal interference, it was the EMI impulses so hard that it was killing the whole ESP device.
So, I made an ESD cage for the PN532 module from Aluminium and Copper adhesive tape over Plexiglas stand.
Sorry for bother you, now we know a little bit more about red PN532 module and how can be used properly.
earlephilhower commentedon Jun 27, 2020
Thanks for the follow up, @beicnet ! You may want to post something about this on the Adafruit library github so they can put it in their
readme.md
.beicnet commentedon Jun 27, 2020
@earlephilhower I already did that before 6 days ago, nobody replayed (nor interested), so I closed the issue yesterday on the Adafruits GIT page.
Anyway I got immediate response and better support here! 🥇 😉