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

Web Server not working on AP mode when a WiFi network is configured #3956

Closed
paviro opened this issue Dec 22, 2022 · 14 comments · Fixed by esphome/esphome#6631
Closed

Web Server not working on AP mode when a WiFi network is configured #3956

paviro opened this issue Dec 22, 2022 · 14 comments · Fixed by esphome/esphome#6631

Comments

@paviro
Copy link

paviro commented Dec 22, 2022

The problem

The web server does not seem to be started on AP mode when there is a WiFi network configured but out of reach. When not configuring a WiFi network and only AP mode the server is normally started.

My use case is a portable heater that can be used with home assistant when in reach of the network but can still be used with the build in web server if not. This does not seem possible right now and I would have to choose to either only allow home assistant access or only access via the build in AP.

As the web server does indeed work over the AP when no network is configured this seems like a bug and not like an intended limitation?

Which version of ESPHome has the issue?

v2022.12.3

What type of installation are you using?

Docker

Which version of Home Assistant has the issue?

2022.12.8

What platform are you using?

ESP32

Board

No response

Component causing the issue

Web Server

Example YAML snippet

No response

Anything in the logs that might be useful for us?

No response

Additional information

No response

@Conclusio
Copy link

Currently, I try similar things (with an ESP8266) and want to have access to the web server while in AP mode.

The main issue here is the setup priority of the web server.

According to web_server.cpp we have float WebServer::get_setup_priority() const {return setup_priority::WIFI - 1.0f; }. This means the web server is initialized after the WiFi connection is established.

In contrast to this, the captive_portal is configured as setup_priority::WIFI + 1.0f (i.e. before WiFi), since it needs to be available in AP mode as well. This might be a design choice or nobody thought about the web server being available in AP mode.

Anyway, changing the setup priority does resolve the issue, BUT there are other issues as well:

While in AP mode, the ESP tries to constantly scan for WiFi networks (if fast_connect is false), or just tries to connect directly (if fast_connect is true). Both variants introduce a delay which (for some reason) can interrupt the HTTP connection, meaning server-sent events are stopped from being received at the client (browser). In case of sensor readings, the browser would need to be refreshed constantly. For your portable heater setup this might not be a problem.

Related issue #3253

@paviro
Copy link
Author

paviro commented Dec 31, 2022

Awesome thank you for the insights! I was looking through the code but couldn't quite piece that part together.

@paviro
Copy link
Author

paviro commented Jan 3, 2023

@Conclusio this does not seem to work for me btw. I only get boot loops when changing the Webserver to ?setup_priority::WIFI + 1.0f

[C][web_server:099]: Setting up web server...

assert failed: tcpip_api_call IDF/components/lwip/lwip/src/api/tcpip.c:497 (Invalid mbox)


Backtrace:0x40083e45:0x3ffb23d00x4008ca29:0x3ffb23f0 0x4009237d:0x3ffb2410 0x40115221:0x3ffb2540 0x4016f798:0x3ffb2570 0x400f3efa:0x3ffb25c0 0x400e33b1:0x3ffb25e0 0x400e340a:0x3ffb2630 0x400e349e:0x3ffb2670 0x40181935:0x3ffb2690 0x401819e5:0x3ffb26b0 0x400e9c54:0x3ffb26d0 0x400ed526:0x3ffb2700 0x400fa56e:0x3ffb2820 




ELF file SHA256: 0000000000000000

Rebooting...
ets Jul 29 2019 12:21:46

rst:0xc (SW_CPU_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:13132
load:0x40080400,len:3036
entry 0x400805e4
[I][logger:258]: Log initialized
[C][ota:469]: There have been 2 suspected unsuccessful boot attempts.

@Conclusio
Copy link

This could be ESP32-specific, but honestly I don't know. The wifi component has platform-specific implementations (ESP32, ESP8266, ...).

If you don't configure a WiFi network at all does the same error occur?

@brgaulin
Copy link

If you don't configure a WiFi network at all does the same error occur?

In my case, thats exactly what it was. Once I removed the wifi config, it started working ok. This still sounds like a bug, but if it is a hardware limitation we should note that in the WebServer docs. Maybe near the local flag. Or in the Wifi docs if it is more global.

I was using a D1 mini.

@Brausmith
Copy link

I don't have anything technical to add, but I have been experiencing the same today.
As soon as I removed the wifi credentials, the web UI via the AP started to work again.

@Hiekkaharju
Copy link

Hi,
thanks for this Issue and the information in it. Sorry for long text...

1st one clarification: webserver already runs with AP if either the device is first started with wifi available or "sta" wifi ssid is not defined. It just does not start if wifi is defined and a reset occurs when wifi if not available.

My use case is a bluetooth enabled battery in my RV. When I'm home, I want to see the status of the RV in Homeassistant. When traveling, I use separate Android apps to monitor the things -I'm not planning setting up a HA node that would travel with the RV.
I'm not aware of any HA integration that could access this type of BMS, and I have not dared to try to create one. I managed to modify an Add-on to handle this BMS, but it's restricted by the bluetooth range of my HA machine. With the information I got from making that, I managed to get an Atom Lite ESP32 talking to the BMS and supplying sensors to HA via wifi.
But then came this Issue. When I'm out of range of home wifi, the esphome device will run it's web server on its AP only until it for any reason resets.
Based on what @Conclusio wrote I found out that Captive Portal is not actually started based on its setup priority, it's setup() is empty. It's started by a call from wifi-component to start().
Thus I:
-Changed the setup priority to ...+1 - similar to captive portal
-moved Webserver setup() code to a new start() - similar to captive portal
-made that run-once by a flag, because it will end up called multiple times when wifi availability comes and goes
-made wifi call webserver start() when AP is up, same place where Captive portal's start() is called
-made wifi call webserver start() when STA - wifi is connected, same place where global_captive_portal->end() is called to stop that
-and finally prevented wifi adapter restart on every 5th connection try, same way it is prevented if captive portal is active. I noticed no real problem from the wifi scan, but this restart naturally caused a big disruption.

With these I seem to have an (nearly) allways on webserver and HA updated with the sensors when the RV is in home wifi range.
Sorry, I'm not making a pull request: 1) I don't know how. 2) my code is REALLY quick and dirty. 3) I don't want to publish code in specifically github.

@udavst
Copy link

udavst commented Apr 23, 2024

With these I seem to have an (nearly) allways on webserver and HA updated with the sensors when the RV is in home wifi range. Sorry, I'm not making a pull request: 1) I don't know how. 2) my code is REALLY quick and dirty. 3) I don't want to publish code in specifically github.

How did you do that? I really need this too. I had to hang a router where there is no Internet and HA, just so that the device works normally at home and in the country. Tell me where and what is possible to fix. Unable to use because of these new (

@Hiekkaharju
Copy link

With these I seem to have an (nearly) allways on webserver and HA updated with the sensors when the RV is in home wifi range. Sorry, I'm not making a pull request: 1) I don't know how. 2) my code is REALLY quick and dirty. 3) I don't want to publish code in specifically github.

How did you do that? I really need this too. I had to hang a router where there is no Internet and HA, just so that the device works normally at home and in the country. Tell me where and what is possible to fix. Unable to use because of these new (

Sorry, but I believe I kind of already did that in the above post. A good C++ coder should be able turn that list of changes to working code in rather short time, utilizing the references I made to Captive portal. I changed only three files, web_server.h, web_server.cpp and wifi_component.cpp.
On the other hand I believe that chances that someone who has not made C-something programs would be able to make those changes, and get code that compiles, with any reasonable amount of guidance, are low.
Rest are somewhere in between those.
And then there are the things related to, and multiple ways of, using non-release code when building an Esphome FW.
And the maintenance as Esphome is updated.
Also note that I do not know if the same would work with ESP8266.
Would someone be doing a PR for this, I'd be happy to answer questions about specific details, should a such person want e.g. confirmation on something.

@ssieb
Copy link
Member

ssieb commented Apr 24, 2024

Why do you have the captive portal configured if you don't want to use it? Remove it from the config and the web server will work fine.

@ssieb ssieb closed this as not planned Won't fix, can't repro, duplicate, stale Apr 24, 2024
@udavst
Copy link

udavst commented Apr 25, 2024

My captive portal is turned off, but the web-server starts working only after connecting to the SSID, or by removing SSID from the firmware (

@Hiekkaharju
Copy link

Why do you have the captive portal configured if you don't want to use it? Remove it from the config and the web server will work fine.

That is not true. Read the explanations.

@ssieb
Copy link
Member

ssieb commented Apr 25, 2024

My captive portal is turned off, but the web-server starts working only after connecting to the SSID, or by removing SSID from the firmware

I missed that part.

@ssieb
Copy link
Member

ssieb commented Apr 25, 2024

Try that PR.

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

Successfully merging a pull request may close this issue.

7 participants