Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESP32 to ESP32 GET Request = connection refused #1700

Open
SkHCrusher opened this issue Jan 9, 2024 · 13 comments
Open

ESP32 to ESP32 GET Request = connection refused #1700

SkHCrusher opened this issue Jan 9, 2024 · 13 comments
Labels
Question User Question member to member support

Comments

@SkHCrusher
Copy link

Basic Infos

Hardware

WiFimanager Branch/Release: Master (from 01-09-2024)
Esp8266/Esp32: ESP32
Hardware: ESP32-S3, ESP32-S2, AZ Delivery ESP32

Description

I have two ESP32 modules with the Wifi Manager. One ESP32 offers an additional endpoint and the second ESP should trigger this endpoint.

The ESP32 always reports "connection refused" after the request.
If I call this with a tool like Insomnia from a Windows PC, I get a valid response.

At first I thought that I had conflicts with other libs in the project. However, my example here is reduced to the minimum and I can also reproduce the problem here.

I have also tested several modules (ESP32 Dev, ESP32-S2, ESP32-S3). The same result everywhere.

I hope that I am overlooking something simple the last few evenings and someone has a solution to the problem.

Sketch Server

#include <Arduino.h>
#include <WiFiManager.h>

WiFiManager wm;
char wifiName[32] = "ESP-Test-Server";
char wifiPassword[32] = "YourDefaultPassword";

void demoRequest() {
  Serial.println("Req call");
  wm.server->send(200);
}
void bindServerCallback() { wm.server->on("/demo", demoRequest); }

void setup() {
  Serial.begin(115200);

  wm.setConfigPortalBlocking(false);
  wm.setWebServerCallback(bindServerCallback);

  bool res = wm.autoConnect(wifiName, wifiPassword);
  if (!res) {
    ESP.restart();
  }

  delay(500);

  wm.startWebPortal();
}

void loop() { wm.process(); }

Sketch Requester

Comment: Yes the IP address is fixed set to "192.168.178.156" in my network. But also with a dynamic address, it doesn't work.

#include <Arduino.h>
#include <HTTPClient.h>
#include <WiFiManager.h>

HTTPClient http;

WiFiManager wm;
char wifiName[32] = "ESP-Test-Requester";
char wifiPassword[32] = "YourDefaultPassword";

void setup() {
  Serial.begin(115200);

  bool res = wm.autoConnect(wifiName, wifiPassword);
  if (!res) {
    ESP.restart();
  }
}

void loop() {
  delay(2000);

  http.begin("192.168.178.156", 80, "/demo");

  int httpResponseCode = http.GET();

  if (httpResponseCode > 0) {
    Serial.printf("[HTTP] GET... code: %d\n", httpResponseCode);

    if (httpResponseCode == HTTP_CODE_OK) {
      String payload = http.getString();
      Serial.println(payload);
    }
  } else {
    Serial.printf("[HTTP] GET... failed, error: %s\n",
                  http.errorToString(httpResponseCode).c_str());
  }
}

Debug Messages ESP Requester: Does NOT work

ESP-ROM:esp32s3-20210327
Build:Mar 27 2021
rst:0x1 (POWERON),boot:0x8 (SPI_FAST_FLASH_BOOT)
SPIWP:0xee
mode:DIO, clock div:1
load:0x3fce3808,len:0x44c
load:0x403c9700,len:0xbd8
load:0x403cc700,len:0x2a80
entry 0x403c98d0
*wm:AutoConnect 
*wm:Connecting to SAVED AP: Crusher-Net-Oben
*wm:connectTimeout not set, ESP waitForConnectResult... 
*wm:AutoConnect: SUCCESS 
*wm:STA IP Address: 192.168.178.148
[HTTP] GET... failed, error: connection refused
[HTTP] GET... failed, error: connection refused
[HTTP] GET... failed, error: connection refused
[HTTP] GET... failed, error: connection refused
[HTTP] GET... failed, error: connection refused

Debug Messages Insomnia: works 100%


* Preparing request to http://192.168.178.156/demo
* Current time is 2024-01-09T18:21:58.626Z
* Enable automatic URL encoding
* Using default HTTP version
* Enable SSL validation
* Hostname in DNS cache was stale, zapped
*   Trying 192.168.178.156:80...
* Connected to 192.168.178.156 (192.168.178.156) port 80 (#10)

> GET /demo HTTP/1.1
> Host: 192.168.178.156
> User-Agent: Insomnia/2023.5.7
> Accept: */*

* Mark bundle as not supporting multiuse

< HTTP/1.1 200 OK
< Content-Type: text/html
< Content-Length: 0
< Connection: close
@tablatronix
Copy link
Collaborator

What about a basic example like the ntp one

@SkHCrusher
Copy link
Author

What about a basic example like the ntp one

Firstly, thank you for your answer.

I'm not sure if I've understood you correctly.
Would you like to know if the basic example from the Lib works on the devices from my network?
https://www.arduino.cc/reference/en/libraries/ntpclient/

If that's the question, yes it works... here is the sketch and the result:

NTP Basic Example

#include <NTPClient.h>
// change next line to use with another board/shield
//#include <ESP8266WiFi.h>
#include <WiFi.h> // for WiFi shield
//#include <WiFi101.h> // for WiFi 101 shield or MKR1000
#include <WiFiUdp.h>

const char *ssid     = "****";
const char *password = "****";

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);

void setup(){
  Serial.begin(115200);

  WiFi.begin(ssid, password);

  while ( WiFi.status() != WL_CONNECTED ) {
    delay ( 500 );
    Serial.print ( "." );
  }

  timeClient.begin();
}

void loop() {
  timeClient.update();

  Serial.println(timeClient.getFormattedTime());

  delay(1000);
}

Serial Monitor

07:46:34
07:46:35
07:46:36
07:46:37
07:46:38
07:46:39
07:46:40
07:46:41
07:46:42
07:46:43
07:46:44
07:46:45
07:46:46
07:46:47
07:46:48

@tablatronix
Copy link
Collaborator

My guess is there is an issue with httpclient and wm, oddly though you are only running autoconnect, so nothing should be started by wm if connect is ok.

@SkHCrusher
Copy link
Author

My guess is there is an issue with httpclient and wm, oddly though you are only running autoconnect, so nothing should be started by wm if connect is ok.

But the crazy thing is that it works from a windows computer but not from the esp…
That is confusing. So there must be something different between those both methods… but I can not figure out what it is… and the process looks so stupid simple ;)

@tablatronix
Copy link
Collaborator

tablatronix commented Jan 11, 2024

Have you enabled full esp debugging wifi and core level 5?

umm * Enable SSL validation ? but not https right, I mean the server sure seems to be refusing the connection.
wrong http version, missing headers perhaps?

you can hit it with curl ?

I will see if I can test this some day, seems simple enough, cant imagine what it could be, try sending different http resp?
404, redirects etc?

Does esp webserver have a max clients of 1?

Missing content type/length?
wm.server->send(200, "text/plain", "hello from user code");

@tablatronix tablatronix added the Question User Question member to member support label Jan 11, 2024
@SkHCrusher
Copy link
Author

SkHCrusher commented Jan 13, 2024

@tablatronix I take your notes and do some tests. The result: Still the same problem.
First: Yes i try only http... so no SSL/Securty problem should be the error here.

  1. I changed the response to your demo-response from
wm.server->send(200);

to

wm.server->send(200, "text/plain", "hello from user code");

Still "connection refused". But i leave this for the next tests.

  1. Test the curl from a windows pc. Works direct with the answer:
C:\Users\Crusher>curl http://192.168.178.156/demo
hello from user code
  1. I enabled on borth sides the debugging level

Debug from Server:

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:1184
load:0x40078000,len:13232
load:0x40080400,len:3028
entry 0x400805e4
[     7][D][esp32-hal-cpu.c:244] setCpuFrequencyMhz(): PLL: 480 / 2 = 240 Mhz, APB: 80000000 Hz
*wm:AutoConnect 
[    72][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 0 - WIFI_READY
[   145][V][WiFiGeneric.cpp:340] _arduino_event_cb(): STA Started
[   146][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 2 - STA_START
*wm:Connecting to SAVED AP: Crusher-Net-Oben
[   661][V][WiFiGeneric.cpp:97] set_esp_interface_ip(): Configuring Station static IP: 0.0.0.0, MASK: 0.0.0.0, GW: 0.0.0.0
*wm:connectTimeout not set, ESP waitForConnectResult... 
[   727][V][WiFiGeneric.cpp:355] _arduino_event_cb(): STA Connected: SSID: Crusher-Net-Oben, BSSID: d4:5d:64:eb:2f:c0, Channel: 10, Auth: WPA2_PSK
[   729][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 4 - STA_CONNECTED
[   768][V][WiFiGeneric.cpp:369] _arduino_event_cb(): STA Got New IP:192.168.178.156
[   769][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 7 - STA_GOT_IP
[   772][D][WiFiGeneric.cpp:1102] _eventCallback(): STA IP: 192.168.178.156, MASK: 255.255.255.0, GW: 192.168.178.1
*wm:AutoConnect: SUCCESS 
*wm:STA IP Address: 192.168.178.156
*wm:Starting Web Portal 
[  1294][V][WiFiServer.h:42] WiFiServer(): WiFiServer::WiFiServer(port=80, ...)
[  1294][V][WebServer.cpp:87] WebServer(): WebServer::Webserver(port=80)

If the Request from the other ESP is send there is no debug output.
If i send the curl request from the windows computer i get this info:

[ 18825][V][WebServer.cpp:296] handleClient(): New client: client.localIP()=192.168.178.156
[ 18826][V][Parsing.cpp:122] _parseRequest(): method: GET url: /demo search: 
[ 18829][V][Parsing.cpp:226] _parseRequest(): headerName: Host
[ 18834][V][Parsing.cpp:227] _parseRequest(): headerValue: 192.168.178.156
[ 18841][V][Parsing.cpp:226] _parseRequest(): headerName: User-Agent
[ 18847][V][Parsing.cpp:227] _parseRequest(): headerValue: curl/8.4.0
[ 18853][V][Parsing.cpp:226] _parseRequest(): headerName: Accept
[ 18859][V][Parsing.cpp:227] _parseRequest(): headerValue: */*
[ 18865][V][Parsing.cpp:254] _parseArguments(): args: 
[ 18870][V][Parsing.cpp:237] _parseRequest(): Request: /demo
[ 18875][V][Parsing.cpp:238] _parseRequest():  Arguments: 
Req call

So strange and different.

After this I enabled also the debug for the requester ESP. Here is the log:

ESP-ROM:esp32s3-20210327
Build:Mar 27 2021
rst:0x1 (POWERON),boot:0x8 (SPI_FAST_FLASH_BOOT)
SPIWP:0xee
mode:DIO, clock div:1
load:0x3fce3808,len:0x44c
load:0x403c9700,len:0xbd8
load:0x403cc700,len:0x2a80
entry 0x403c98d0
*wm:AutoConnect 
[   218][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 0 - WIFI_READY
[   251][V][WiFiGeneric.cpp:340] _arduino_event_cb(): STA Started
*wm:[   252][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 2 - STA_START
Connecting to SAVED AP: Crusher-Net-Oben
[   765][V][WiFiGeneric.cpp:97] set_esp_interface_ip(): Configuring Station static IP: 0.0.0.0, MASK: 0.0.0.0, GW: 0.0.0.0
*wm:connectTimeout not set, ESP waitForConnectResult... 
[   802][V][WiFiGeneric.cpp:355] _arduino_event_cb(): STA Connected: SSID: Crusher-Net-Oben, BSSID: d4:5d:64:eb:2f:c0, Channel: 10, Auth: WPA2_PSK
[   804][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 4 - STA_CONNECTED
[   819][V][WiFiGeneric.cpp:369] _arduino_event_cb(): STA Got New IP:192.168.178.148
[   820][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 7 - STA_GOT_IP
[   826][D][WiFiGeneric.cpp:1102] _eventCallback(): STA IP: 192.168.178.148, MASK: 255.255.255.0, GW: 192.168.178.1
*wm:AutoConnect: SUCCESS 
*wm:STA IP Address: 192.168.178.148

For each request, this log added additionaly:

[  2874][D][HTTPClient.cpp:321] begin(): host: 192.168.178.156 port: 80 uri: /demo
[  2875][D][HTTPClient.cpp:598] sendRequest(): request type: 'GET' redirCount: 0
[  7880][I][WiFiClient.cpp:260] connect(): select returned due to timeout 5000 ms for fd 48
[  7881][D][HTTPClient.cpp:1163] connect(): failed connect to 192.168.178.156:80
[  7884][W][HTTPClient.cpp:1483] returnError(): error(-1): connection refused
[HTTP] GET... failed, error: connection refused

So it is crazy... there is a timeout....
I checked also that both are in the correct network. Also the ip addresses fits... so there is from my understanding not the issue.

On my Windows computer the first request tooks also longer then the next requests. So my next idea was:

  1. Set the timeout time higher to maybe 20000ms.
    The debug shows that the request timeout was after 5000ms. so i found that this should work (https://forum.arduino.cc/t/webserver-http-get-request-timeout/1011353):
http.setTimeout(20000);
client.setTimeout(20000);

I tried the frist on:

http.setTimeout(20000);

But nothing changed. Still timeout after 5000ms.
My client is the "wm" variable so i added also:

wm.setTimeout(20000);

Compiling and upload works... but still timeout after 5000ms.
I searched also in the WiFiManager.h for timeout functions... but all looks more for the portal and not for this request.

  1. As last test i tried the timeout functions that i found. Only to check if one function is in the background related to my issue...
    So this is my code:
  http.setTimeout(20000);
  wm.setTimeout(20000);
  wm.setConfigPortalTimeout(11000);
  wm.setConnectTimeout(13000);
  wm.setSaveConnect(15000);
  http.begin("192.168.178.156", 80, "/demo");

I chosse 11000, 13000 and 15000 only to see if the timout time changed which was the function concerned.
But still:

[190586][I][WiFiClient.cpp:260] connect(): select returned due to timeout 5000 ms for fd 48

Hope there is another idea :-/

@tablatronix
Copy link
Collaborator

I have not had a chance to load these up and test here, and see if wireshark catches anything.
Very strange indeed.

It sounds like the request is not actually going out. maybe try a different port?

@SkHCrusher
Copy link
Author

Sorry for the delay.

I have tried to set the port to e.g. 8080... This change does not change the behaviour either.

wm.setHttpPort(8080);

From the PC I can make the call with :8080. Not from the other ESP.

@SkHCrusher
Copy link
Author

Additional information: I am originally trying to communicate with an AZ Node MCU and an ESP-S3.
However, I have also tested the same setup with two NodeMCU's and the result is identical here.

:-(

@SkHCrusher
Copy link
Author

I have a few more insights, but they don't really solve my problem.

The simplest first: I was able to set the timeout successfully with the following command:
http.setConnectTimeout(15000);

The "Refuse" problem seems to be a problem with some networks.
When I connect the ESPs to my Fritz-BOX WLAN, I get these error messages and it only works from PC to ESP32.

If I install a router without Internet and connect both ESP32s there, both can communicate without problems.

Unfortunately, I'm not a WLAN expert, which is why the ESPs don't work in my normal network.

@tablatronix
Copy link
Collaborator

Do you have multiple access points, are you talking across aps, or subnets?

@SkHCrusher
Copy link
Author

My Structure:

Fritzbox 6591 Cable > Asus GT-AX11000

Fritzbox act as router and Asus as AP. Fritzbox delivers services like DHCP.
On my Fritzbox there is a 2.4Ghz Wifi like Wifi1 enabled and on the Asus a 2.4 like Wifi2.
I'm connected over the Asus with Wifi2.
I disabled the autorouting between 2.5 and 5Ghz and all SSIDs are separated. So i can be sure that Wifi2 is only on the Asus AP
There are no special Subnets. All acts in a 192.168.178.* net with fritzbox on 192.168.178.1.

Hope this answers the question :-)

@tablatronix
Copy link
Collaborator

The only thing I was thinking was some mdns or multicast issue but for regular http/tcp this shouldn't even be an issue. Have you tried anything like setting your esp to use a speciic phy mode?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question User Question member to member support
Projects
None yet
Development

No branches or pull requests

2 participants