Connect to specific preconfigured WiFI AP form a script

Hi community.

I am trying to set up a script that should do this:

  • disconnect from the current WiFI AP
  • connect to the secondary WIFI AP
  • do some stuff on that network
  • reconnect to the primary (first) WiFi AP

Is this possible?

Background:
I am attempting to set up a shelly that was reset to AP mode back to the correct WiFi by making one of my Esphome devices connect to it, setting up the device with a http.post, and reconnecting to the default WiFI AP.

Disclaimer: I am far away from the site and have no other option but to reconnect the shelly to the network.

I already figured out the http.post part but don’t seem able to ensure connection to a specific WiFi AP.

I could also make the shelly’s WiFi AP primary and assume that the setup will work on the first attempt (that will make that network unavailable), but then I could end up losing access to my EspHome device stuck on the shelly’s AP.

Any help is appreciated.

I would be intersted in same. I ve got an old vaccuum robot which loses wifi connection every once in awhile , when disconnected it opens up an access point to start reconnecting (reconnecting could be done with API). I would like to have it automated. Once cleaner should be accessed and is not accessible (lost connection) reconnect automated (with timeout 2-3times, could be empty out of battery)-

So far, I have been able to draft a script that will post the data to the API once it is connected.
Switching AP’s might not be possible, so the script should handle a lot of scenarios while positing to the device’s API in order to not fail and not get stuck on the AP.
My plan is to set this up on the current WiFi AP using a dummy server with a dummy API to fully test the script and after that probably give a shot on the device.

EspHome supports multiple AP setups with priority levels. If one fails it lowers it’s priority. So theoretically we can set two AP’s making the device’s AP one step higher than the regular AP’s for example 6 and 5. So if the script fails the EspHome board will eventually reconnect.
We can also have a common script that fires on the wifi.connected event and checks if an IP or something is available in order to determine if it needs to reconnect and try to reset wifi to enforce connect again.

A lot of ifs.

I will post my config here as soon as it will make sense