Skip to content

ESP8266HTTPClient error -1  #6180

Closed
Closed
@Loucotolo

Description

@Loucotolo

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

Activity

devyte

devyte commented on Jun 6, 2019

@devyte
Collaborator

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

jpboucher commented on Jun 14, 2019

@jpboucher

You are probably running out of sockets after a while. Try calling http.setReuse(true) before you call http.begin

d-a-v

d-a-v commented on Jun 17, 2019

@d-a-v
Collaborator

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:

17:33:37.439458 IP 192.168.0.192.49882 > 192.168.0.31.8000: Flags [S], seq 6630, win 2144, options [mss 536,nop,nop,sackOK], length 0
17:33:37.439571 IP 192.168.0.31.8000 > 192.168.0.192.49882: Flags [S.], seq 1154906697, ack 6631, win 65392, options [mss 1460,nop,nop,sackOK], length 0

lots of https at the same time:

17:33:37.623692 IP 149.11.65.227.https > 192.168.0.31.49821: Flags [P.], seq 22745748:22745784, ack 1, win 34, length 36
17:33:37.623693 IP 149.11.65.227.https > 192.168.0.31.49821: Flags [P.], seq 22745784:22746194, ack 1, win 34, length 410
17:33:37.623694 IP 149.11.65.227.https > 192.168.0.31.49821: Flags [P.], seq 22746194:22746230, ack 1, win 34, length 36
... (lots)
17:33:37.868468 IP 192.168.0.31.49821 > 149.11.65.227.https: Flags [.], ack 22752711, win 1024, length 0
17:33:37.916391 IP 149.11.65.227.https > 192.168.0.31.49821: Flags [P.], seq 22752711:22753078, ack 1, win 34, length 367
17:33:37.916445 IP 192.168.0.31.49821 > 149.11.65.227.https: Flags [.], ack 22753078, win 1023, length 0

esp times out:

17:33:37.944117 IP 192.168.0.192.49882 > 192.168.0.31.8000: Flags [R.], seq 1, ack 3140060599, win 24584, length 0

next requests are honored:

...
17:33:38.446020 IP 192.168.0.192.50004 > 192.168.0.31.8000: Flags [S], seq 6642, win 2144, options [mss 536,nop,nop,sackOK], length 0
17:33:38.446099 IP 192.168.0.31.8000 > 192.168.0.192.50004: Flags [S.], seq 1775072509, ack 6643, win 65392, options [mss 1460,nop,nop,sackOK], length 0
17:33:38.448943 IP 192.168.0.192.50004 > 192.168.0.31.8000: Flags [.], ack 1, win 2144, length 0
17:33:38.454408 IP 192.168.0.192.50004 > 192.168.0.31.8000: Flags [P.], seq 1:206, ack 1, win 2144, length 205
17:33:38.454408 IP 192.168.0.192.50004 > 192.168.0.31.8000: Flags [P.], seq 206:211, ack 1, win 2144, length 5
17:33:38.454437 IP 192.168.0.31.8000 > 192.168.0.192.50004: Flags [.], ack 211, win 65182, length 0
17:33:38.455149 IP 192.168.0.31.8000 > 192.168.0.192.50004: Flags [P.], seq 1:231, ack 211, win 65182, length 230
17:33:38.455223 IP 192.168.0.31.8000 > 192.168.0.192.50004: Flags [F.], seq 231, ack 211, win 65182, length 0
17:33:38.469175 IP 192.168.0.192.50004 > 192.168.0.31.8000: Flags [R.], seq 211, ack 232, win 24584, length 0
...

Be nicer with your server, increase ESP timeout.

TD-er

TD-er commented on Jun 18, 2019

@TD-er
Contributor

@jpboucher

You are probably running out of sockets after a while. Try calling http.setReuse(true) before you call http.begin

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

jpboucher commented on Jun 18, 2019

@jpboucher

@jpboucher

You are probably running out of sockets after a while. Try calling http.setReuse(true) before you call http.begin

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?

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

d-a-v commented on Jun 18, 2019

@d-a-v
Collaborator

I've looked into the code and it seems it does keep the connection open.

It doesn't. Variable scope implies the destructor is called and the connection closed at every loop (on test() ending, because WiFiClient and HTTPClient are declared in there).

The default is 5. It can be increased up to 15 using espconn_tcp_set_max_con() from the ESP SDK.

We don't depend on that. It's for espconn API. Your only limitation is RAM.

jpboucher

jpboucher commented on Jun 18, 2019

@jpboucher

My bad. Since everything is indeed cleared at every loop I would also suspect that the server is too slow.

TD-er

TD-er commented on Jun 18, 2019

@TD-er
Contributor

It doesn't. Variable scope implies the destructor is called and the connection closed at every loop (on test() ending, because WiFiClient and HTTPClient are declared in there).

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

devyte commented on Feb 2, 2020

@devyte
Collaborator

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.

added
waiting for feedbackWaiting on additional info. If it's not received, the issue may be closed.
on Feb 2, 2020
earlephilhower

earlephilhower commented on May 26, 2020

@earlephilhower
Collaborator

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

beicnet commented on Jun 12, 2020

@beicnet

@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.

esp_response_minus_one

This function is called every 10 seconds:

void checkForUpdates()
{
  if (WiFi.status() == WL_CONNECTED) {
    String fwVerURL = "";
    fwVerURL.concat("http://");
    fwVerURL.concat("xxxxxx.ddns.net");
    fwVerURL.concat(":");
    fwVerURL.concat("80");
    fwVerURL.concat("/xxx/version.asp");
   
    HTTPClient updHttp;
    updHttp.setReuse(true);
    updHttp.begin(fwVerURL);

    int httpCode = updHttp.GET();

    Serial.println("HTTP Respose Code: " + String(httpCode));

    if (httpCode == 200) {
      String newFwTmpVersion = updHttp.getString();
      // ===> Here I got 3 second whole ESP freezing <===
      Serial.println("HTTP Update Version: " + newFwTmpVersion);
    } else {
      Serial.println("Error HTTP Update!");
    }
    updHttp.end();
  }
}
earlephilhower

earlephilhower commented on Jun 12, 2020

@earlephilhower
Collaborator

@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

beicnet commented on Jun 12, 2020

@beicnet

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

beicnet commented on Jun 21, 2020

@beicnet

@TD-er Can you please verify for me something?

TD-er

TD-er commented on Jun 21, 2020

@TD-er
Contributor

@beicnet What do you want me to verify?

beicnet

beicnet commented on Jun 21, 2020

@beicnet

@TD-er Do you have a PN532 module by your side?

TD-er

TD-er commented on Jun 21, 2020

@TD-er
Contributor

Not sure, have to dig in the drawer(s) of sensors.

beicnet

beicnet commented on Jun 21, 2020

@beicnet

@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

earlephilhower commented on Jun 21, 2020

@earlephilhower
Collaborator

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

beicnet commented on Jun 21, 2020

@beicnet

@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

beicnet commented on Jun 26, 2020

@beicnet

@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

d-a-v commented on Jun 26, 2020

@d-a-v
Collaborator

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

    HTTPClient updHttp;
    updHttp.setReuse(true);
    updHttp.begin(fwVerURL);

    int httpCode = updHttp.GET();

    Serial.println("HTTP Respose Code: " + String(httpCode));

    if (httpCode == 200) {
      String newFwTmpVersion = updHttp.getString();
      Serial.println("HTTP Update Version: " + newFwTmpVersion);
    } else {
      Serial.println("Error HTTP Update!");
    }
    updHttp.end();

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)

Serial.print("t1");
    HTTPClient updHttp;
    updHttp.setReuse(true);
    updHttp.begin(fwVerURL);
Serial.print("t2");

    int httpCode = updHttp.GET();
Serial.print("t3");

    Serial.println("HTTP Respose Code: " + String(httpCode));

    if (httpCode == 200) {
      String newFwTmpVersion = updHttp.getString();
      Serial.println("HTTP Update Version: " + newFwTmpVersion);
    } else {
      Serial.println("Error HTTP Update!");
    }
    updHttp.end();
beicnet

beicnet commented on Jun 26, 2020

@beicnet

@d-a-v Thank you for replying, your summation is 99% correct.

with Function Enabled:

t1t2:ref 1
:ctmo
:abort
:ur 1
:dsrcv 0
:del
t3HTTP Response Code: -1
Error HTTP Update!
t1t2:ref 1
:ctmo
:abort
:ur 1
:dsrcv 0
:del
t3HTTP Response Code: -1
Error HTTP Update!
t1t2:ref 1
:ctmo
:abort
:ur 1
:dsrcv 0
:del
t3HTTP Response Code: -1
Error HTTP Update!

with Function Disabled:

t1t2:ref 1
:wr 185 0
:wrc 185 185 0
:ack 185
:rn 378
t3HTTP Response Code: 200
:c0 1, 378
HTTP Update Version: 3.9
:close
:ur 1
:dsrcv 0
:del
t1t2:ref 1
:wr 185 0
:wrc 185 185 0
:ack 185
:rn 378
t3HTTP Response Code: 200
:c0 1, 378
HTTP Update Version: 3.9
:close
:ur 1
:dsrcv 0
:del
t1t2:ref 1
:wr 185 0
:wrc 185 185 0
:ack 185
:rn 378
t3HTTP Response Code: 200
:c0 1, 378
HTTP Update Version: 3.9
:close
:ur 1
:dsrcv 0
:del

Also I tried to implement the ArduinoHttpClient.h library from another source, but got almost the same results.

d-a-v

d-a-v commented on Jun 26, 2020

@d-a-v
Collaborator

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

beicnet commented on Jun 26, 2020

@beicnet

@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

earlephilhower commented on Jun 27, 2020

@earlephilhower
Collaborator

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

beicnet commented on Jun 27, 2020

@beicnet

@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! 🥇 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    waiting for feedbackWaiting on additional info. If it's not received, the issue may be closed.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @earlephilhower@TD-er@d-a-v@devyte@beicnet

        Issue actions

          ESP8266HTTPClient error -1 · Issue #6180 · esp8266/Arduino