How to connect to WiFi #1 or WiFi #2 (different subnets) or start AP (depending on WiFi availability)?

Hello!

My question is maybe very basic, but I have really not found out how to proceed despite reading the docs and other forum posts. I hope that somebody can provide me with some hints.

I want my ESP32 to be able to connect to 1 of 2 WiFi networks, which are on different subnets (gateways on 192.168.11.1 and 192.168.178.1, respectively). The ESP32 should have a static IP that matches to the corresponding subnet when connected:

  • If the ESP32 cannot connect to WiFi #1, then it should connect to WiFi #2.
  • If the ESP32 cannot connect to WiFi #2, then it should connect to WiFi #1.
  • If the ESP32 cannot connect to WiFi #1 and not to WiFi #2, then it should start an access point (also with a static IP; it should match the subnet of WiFi #1, even if it cannot connect to it).
  • If the ESP32 could connect to WiFi #1 and to WiFi #2 (i.e. both are availble), then it should connect to WiFi #1 (let’s call it primary WiFi). The signal strength of WiFi #1 is always better than that of WiFi #2, if both are available.

Below is my YAML code, but I suspect that use_address is not correctly used, because it does not seem to work. Sometimes, the ESP32 connects to #1, sometimes to #2; however, not as I described above but it seems rather “erratic”.

Thanks a lot for your help! :+1:

wifi:                               # WiFi Component (https://esphome.io/components/wifi.html)
  use_address: 192.168.11.5
  ap:                               # Set up a WiFi access point
    ap_timeout: 5min                # Enable AP fallback hotspot after defined time
    ssid: ${friendly_name}
    password: !secret esp32-ap_password
    manual_ip:
      static_ip: 192.168.11.5
      gateway: 192.168.11.1
      subnet: 255.255.255.0
      dns1: 192.168.11.1
      dns2: 192.168.11.1
  networks:
  - ssid: !secret wifi1_ssid
    password: !secret wifi1_password
    manual_ip:
      static_ip: 192.168.11.5
      gateway: 192.168.11.1
      subnet: 255.255.255.0
      dns1: 192.168.11.1
      dns2: 192.168.11.1
  - ssid: !secret wifi2_ssid
    password: !secret wifi2_password
    manual_ip:
      static_ip: 192.168.178.155
      gateway: 192.168.178.1
      subnet: 255.255.255.0
      dns1: 192.168.178.1
      dns2: 192.168.178.1

captive_portal:                     # Captive Portal (https://esphome.io/components/captive_portal.html)

use_address: is not used for that purpose. It’s for changing the name or IP address of an existing node - have a closer read of: ESPHome Core Configuration — ESPHome

This is how your config will work, however when both are up, it will connect to the network that it thinks has the stronger signal, which can change obviously.

Connecting to Multiple Networks

You can give ESPHome a number of WiFi networks to connect to. ESPHome will then attempt to connect to the one with the highest signal strength.

1 Like

Yes, I saw that it was mentioned in the docs and also in some forum posts.

However, when I comment the use_address line, validation fails:

INFO ESPHome 2024.9.2
INFO Reading configuration /config/esphome/esp32-bt-proxy.yaml...
Failed config

wifi: [source /config/esphome/esp32-bt-proxy.yaml:44]
  
  Must specify use_address when using multiple static IP addresses.
  ap: 
    ap_timeout: 5min
    ssid: ESP32 BT Proxy
    password: !secret esp32-ap_password
    manual_ip: 
      static_ip: 192.168.11.5
      gateway: 192.168.11.1
      subnet: 255.255.255.0
      dns1: 192.168.11.1
      dns2: 192.168.11.1
  networks: 
    - ssid: !secret wifi1_ssid

I.e. the parser complains with:

Must specify use_address when using multiple static IP addresses.

So does this not mean that use_address is required in my scenario?

No - it probably means that multiple static addresses in different subnets aren’t supported, with the exception of using use_address to change name/address.

Maybe post a question on the ESPHome Discord server - it sounds like a question the devs would have to answer and they hang out there more than in the forums.

1 Like

Thanks for our advice! I have posted my question there: