HTTP request retry doesnt work on my acces point, but does on my mobile phone hotspot

Hello,

First of all thanks to all of you out there reading my post. I hope this is the right forum for my issue.

Here some background on my project:
I'm currently working on a project where I control my home heating setup by utilizing data from my solar cells (the inverter to be exact). For this project, I'm using an ESP32 (WROOM-32). To send API calls to the heating system, I need to get an access token, and this is where I encountered some weird behavior. Due to possible connection losses, I implemented a retry timer.

For the forum, I boiled the code down to the least amount of code needed for the problem to show: (GPT helped me a bit.)

Here is the code:

#include <WiFi.h>
#include <HTTPClient.h>

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

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }

  Serial.println("Connected to WiFi");
}

void loop() {
  // HTTP POST request
  HTTPClient http;

  http.begin("http://httpbin.org/post");  // httpbin endpoint for testing POST requests

  http.addHeader("Content-Type", "application/x-www-form-urlencoded");

  String post_data = "key1=value1&key2=value2"; // Test data.

  int httpCode = http.POST(post_data);

  if (httpCode > 0) {
    String payload = http.getString();
    Serial.println(httpCode);
    Serial.println(payload);
  } else {
    Serial.println("HTTP POST failed");
  }

  http.end();

  delay(5000);  // Wait for 5 seconds before making the next request
}

Let me explain what it does. I am testing an HTTP connection by sending a request to the test website https://httpbin.org/. I am just sending some test data and waiting for a response.

Now, the "error":

ESP32 connected to my access point (AP), and the access point is connected to my router:

If I upload the code to the ESP, the HTTP request works as I expected. If I disconnect the AP now from the router, the request fails, also as expected. When I reconnect the Ethernet cable to the AP, the HTTP request is working again.

Now, if I reset the ESP while having the AP disconnected from the router, the request fails. That part was totally predictable. (The Wi-Fi is still there and the esp32 is connected to it as the AP isn't turned off. It's just disconnected from the router.) But, when I reconnect the cable, it keeps failing? It can't do the HTTP request even if the internet is back on.

I tried this also with the hotspot of my mobile phone while turning the roaming data off and on. And if I start the ESP32 and connect it to my hotspot while having roaming off (no internet on the mobile phone but the hotspot is active) and turning the roaming back on, the request works?! This would be the expected bahaviour on my home network.

May this have something to do with DHCP? Or with my switch being between the AP and the direct connection (ethernet cable) to my router? Why does the ESP32 seem to need an active internet connection when starting it up on my home internet while working without any flaws on my mobile phone internet?

I hope my problem isn't too confusing. Maybe try it out for yourself. ((1)Connect with internet access and then disconnect the internet access and connect back on. (2)And then reset your ESP whilst having no internet access and then reconnect internet access.) I would be interested if you got the same issue.

Using:
AP: TP-Link TL-WA801N
Switch: TP-Link TL-SG105E
Router: Telekom Speedport Smart 4

Best regards

Marius

P.S.: To clarify any misconceptions from the beginning: I am not turning on and off the Wi-Fi! I simply disconnect the Ethernet cable from my AP or disable roaming on my phone. The Wi-Fi signal and connection are active throughout.

I moved your topic to a more appropriate forum category @marius989.

The Nano ESP32 category you chose is only used for discussions directly related to the Arduino Nano ESP32 board.

In the future, please take the time to pick the forum category that best suits the subject of your question. There is an "About the _____ category" topic at the top of each category that explains its purpose.

Thanks in advance for your cooperation.

1 Like

Hello pert,

sorry for choosing the wrong category and thank you for correcting that.

Greetings,

Marius

1 Like

Hello,

I just eliminated the switch from being part of the problem and connected the AP directly to my router. This yields the same behavior. If I power the ESP32 on while having the AP disconnected from the router (Wi-Fi is still on and connected to the ESP32!), the HTTP request fails. It keeps failing, even when I reconnect the AP by plugging in the Ethernet cable to the router again.

When I power the ESP32 while having the Ethernet cable plugged in, the request succeeds. It fails when I disconnect the Ethernet cable, but it succeeds again when I reconnect it. However, this only happens if the ESP32 was reset while having the AP connected to the router.

And this is only a problem with my home setup. Resetting the ESP32 with no internet access on my phone, but having the hotspot on, is no problem. The request fails from the beginning until I turn roaming back on. Then it succeeds like I would expect it in my home network!

Any help would be appreciated.

Best regards,

Marius

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.