ESP8266HTTPClient error -1
Posted: Tue Jun 04, 2019 10:58 am
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/uncategori ... ception-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!6ayDTjoxEgax ... 9XijBe2EXg
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/uncategori ... ception-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!6ayDTjoxEgax ... 9XijBe2EXg