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.