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

Re-work of the network manager #533

Merged
merged 33 commits into from
Mar 8, 2023

Conversation

jeremypoulter
Copy link
Collaborator

@jeremypoulter jeremypoulter commented Jan 29, 2023

This is a rework of the code to manage connecting to the network, WiFi or Wired. This code better manages the state we are in so hopefully there are less issues and is also more agressive at staying connected to the WiFi access point.

Fixes #420, fixes #460

All network changes are now handled in the main network loop and also added in some state management.

For Wired boards we try to connect only on wired for a period of time before bringing up the WiFi.

In AP mode, if we have client credentials continuosly try to connect the client.

Auto stop the AP after a successful client connection
Evaluate the state on startup to make sure the IP subsystems are initialised.

Only restart the WiFi on disconnect if we are not connected via a wired connection.

Fixes to the random seed to work on ESP32 (kind of, not really needed)
Hopefully will help with MQTT connections
@KipK
Copy link
Collaborator

KipK commented Jan 31, 2023

Does the wifi module switch now to AP + STA when wifi has been configured and previous mode was AP ?
I still have to finish the Wizard when this is ok

@jeremypoulter
Copy link
Collaborator Author

Going to do some work cleaning up the endpoints based on this and I will work on that area then

@glynhudson
Copy link
Collaborator

glynhudson commented Feb 13, 2023

Hi @jeremypoulter I've just done some testing and noticed that when a long press on the front push button is used to force the module to create a WiFi AP captive portal is not active.

To clarify: when the push button is pressed and help for about 5s the WiFi AP gets created, but the module remains connected to the existing Wifi network and the Wifi AP does not activate a captive portal when a user connects.

@jeremypoulter
Copy link
Collaborator Author

Will take a look

@KipK
Copy link
Collaborator

KipK commented Feb 21, 2023

I'm testing this now, and making change to UI wizard ( moving to /config endpoint )
When I set the Wifi + Pass while in AP mode, I see in the log it's looping in reconnection until Wifi module is rebooted or soft AP client is disconnected
After we reboot the Wifi module, it then goes in Client only and works ok:

Connecting to SSID: WifiNetwrk
Got Network event ARDUINO_EVENT_WIFI_STA_START
WiFi State AccessPointConnecting
WiFi Mode AP+STA
Set host name to openevse-c6cd
Got Network event ARDUINO_EVENT_WIFI_STA_DISCONNECTED
WiFi State AccessPointConnecting
WiFi Mode AP+STA
WiFi dissconnected: WIFI_DISCONNECT_REASON_NO_AP_FOUND
Got Network event ARDUINO_EVENT_WIFI_STA_STOP
WiFi State AccessPointConnecting
WiFi Mode AP
Connecting to SSID: WifiNetwrk
Got Network event ARDUINO_EVENT_WIFI_STA_START
WiFi State AccessPointConnecting
WiFi Mode AP+STA
Set host name to openevse-c6cd
Got Network event ARDUINO_EVENT_WIFI_STA_DISCONNECTED
WiFi State AccessPointConnecting
WiFi Mode AP+STA
WiFi dissconnected: WIFI_DISCONNECT_REASON_NO_AP_FOUND
Got Network event ARDUINO_EVENT_WIFI_STA_STOP
WiFi State AccessPointConnecting
WiFi Mode AP
Connecting to SSID: WifiNetwrk
Got Network event ARDUINO_EVENT_WIFI_STA_START
WiFi State AccessPointConnecting
WiFi Mode AP+STA
Set host name to openevse-c6cd
Got Network event ARDUINO_EVENT_WIFI_STA_DISCONNECTED
WiFi State AccessPointConnecting
WiFi Mode AP+STA
WiFi dissconnected: WIFI_DISCONNECT_REASON_NO_AP_FOUND
Got Network event ARDUINO_EVENT_WIFI_STA_STOP
WiFi State AccessPointConnecting
WiFi Mode AP

I think it keep the softAP channel when connecting as STA instead of changing channel.

@KipK
Copy link
Collaborator

KipK commented Feb 21, 2023

Other thing I have here, when scanning for network while in AP mode, It gives one time a result, then scanning again always return empty array.
Doesn't happened in CLI mode

@jeremypoulter
Copy link
Collaborator Author

@glynhudson I think I have resolved that issue, please can you check again

@KipK I think the connection issue you are seeing is expected behaviour. The configured SSID is not found (WIFI_DISCONNECT_REASON_NO_AP_FOUND) so we keep on retrying.

I also can not reproduce the AP scanning issue, looks to work for me.

@KipK
Copy link
Collaborator

KipK commented Feb 22, 2023

@jeremypoulter the esp is 3 meters from the AP. signal is strong.
It just doesn't connect to the wifi router until the ESP leave AP mode.
At reboot it also instantaneously connect to the router. I think ESP32 should switch channel to the router channel when connecting in AP+STA mode. ( My wifi is channel 13 here, ESP seems to always be on lower channels in AP mode )

It found it at first scan in AP mode then all the other results return the same ( either empty or a network in the lower band channel ). Scan works ok in STA mode.

I've tried to debug that yesterday without success. Also tryied using WiFiManager lib on the same dev ESP and had no such problem.

@KipK
Copy link
Collaborator

KipK commented Feb 22, 2023

more info on this, first scan takes few seconds to answer and gives full result. Second scan and all following ones instantaneously ( doesn't seem to scan really ) and always gives the same result thereafter ( can be empty or few networks around the same channel )

@KipK
Copy link
Collaborator

KipK commented Feb 22, 2023

I have submitted a PR on your branch allowing to restart mDNS at hostname change jeremypoulter#17

@KipK
Copy link
Collaborator

KipK commented Feb 22, 2023

so has discussed in pv, seems scanning in passive mode solve the scan issue.

I now see another problem now since I've moved to passive scan, wonder if I'm alone. When in AP mode, wifi not configured yet, when saving the wifi / pass on /config endpoint, I'm kicked out of the AP, AP seems disabled then and the ESP module don't connect to the WiFi network until I powercycle.

edit: If I comment the second scan at the end of the function:
//if(WiFi.scanComplete() == -2){ //WiFi.scanNetworks(true, false, true); //}
it stills disable AP when setting the wifi, but for the first time I receive the new IP, that's a good thing! I can at least redirect the page to new ip when device has reconnected to main wifi.

Other thing, when esp is in STA+AP and connected correctly to the wifi router, the IP adress from the /status endpoint is still AP one (192.168.4.1)
But I can reach the wifi module from its home network ip too.

@glynhudson
Copy link
Collaborator

Hi @jeremypoulter I've just done some testing and noticed that when a long press on the front push button is used to force the module to create a WiFi AP captive portal is not active.

To clarify: when the push button is pressed and help for about 5s the WiFi AP gets created, but the module remains connected to the existing Wifi network and the Wifi AP does not activate a captive portal when a user connects.

Hi @jeremypoulter, I've just tested this PR again and this issue remains the same. When the push button is held for 10s to force the module to create a WiFi AP this WiFi AP does not have a captive portal. When I connect to the AP, my phone just says "no internet' and doest not give a captive portal option to sign in:

Screenshot_20230222-122106

@jeremypoulter
Copy link
Collaborator Author

Oh I see, I fixed an issue with the AP not starting at all...

@KipK
Copy link
Collaborator

KipK commented Feb 25, 2023

When the push button is held for 10s to force the module to create a WiFi AP this WiFi AP does not have a captive portal.

@glynhudson , beware, 10sec is for factory resetting the esp not just set in ap mode. It's 2 sec for AP only

@jeremypoulter , got one little issue left, if I throw AP mode with the button, while already connected to STA ( switchting to AP+STA) the ipaddress in /status takes 192.168.4.1 instead of STA one.

@jeremypoulter
Copy link
Collaborator Author

@KipK I think, that is correct, the user has explicitly changed to AP mode, so they would want to know the IP to then connect to. Technically the ESP has two IPs, maybe we could return both, but I think that can be done separately.

@KipK
Copy link
Collaborator

KipK commented Feb 25, 2023

then other apps/ui/home automation connected to the api will also receive this new ip , but the evse is still connected to the network and works normally.

As it's more a process of maintenance, I don't think there's need to override the current ip by the AP one. Not sure there's a real use in redirecting UI to AP ip in this case
In AP only mode that is different.

@KipK
Copy link
Collaborator

KipK commented Feb 25, 2023

this jeremypoulter@f7b1009 fix @glynhudson captive portal not showing problem

KipK and others added 4 commits February 26, 2023 09:47
@KipK
Copy link
Collaborator

KipK commented Feb 27, 2023

I've submitted you the compile fix, sorry for that.

jeremypoulter#20

@jeremypoulter
Copy link
Collaborator Author

Thanks, I knew I should have waited for those builds to finish ;-)

@jeremypoulter
Copy link
Collaborator Author

@glynhudson can you test this again?

@KipK
Copy link
Collaborator

KipK commented Mar 7, 2023

Are we good for mergin it ?
I don't see any other issues than the one already fixed.

@glynhudson
Copy link
Collaborator

Great, just tested. Working now, thanks a lot 👍

@glynhudson glynhudson merged commit aa7fab5 into OpenEVSE:master Mar 8, 2023
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 this pull request may close these issues.

ESP gateway issues with factory config over serial Wifi connection loop: possible improvement.
3 participants