ESP32 device only connecting to Wi-Fi when also directly connected via USB serial?

I have a watermeter setup with a LJ18A3-8-Z_BX - 5V proximity sensor connected to an ESP32 (Lolin Wemos S2 Mini v1.0.0).
The proximity sensor is directly soldered to three pins on the ESP32 and nothing else is connected.
This has been working fine for several weeks, but yesterday the connection with Home Assistant was suddenly lost.

I am not aware of any changes to the setup that could have triggered this problem.

After several hours of trouble shooting I come to the conclusion that the ESP now only connects to Home Assistant when I connect it directly to the Home Assistant server via USB, but when it is online like that it also connects via Wi-Fi, and I can then also connect to it from the ESPHome Dashboard via Wi-Fi and I can show the real-time logs, which are looking normal (no errors).
When the ESP32 is connected like that the Wi-Fi signal strength is -58 dBm and the WiFi quality is 90% to 100% in UniFi Network.

As soon as I connect the ESP to a stand-alone USB adapter it doesn’t connect to Home Assistant anymore and also does not connect to my Wi-Fi access points.

The problem started while I was at Home Assistant version 2023.8.0 and ESPHome version 2023.7.0 and upgrading to Home Assistant 2023.8.1 and ESPHome version 2023.7.1 did not make any difference.

I tried several different USB adapters and USB cables, and I reduced the ESPHome yaml code to the bare minimum, but nothing makes any difference.
An identical ESP32 board, with several sensors connected, is running rock-solid without any problems.

This is the current ESPHome yaml code:

# ======================================
# Created:            20230609
# Last modified:      20230806
# --------------------------------------
# MCU type: ESP32
# MCU board: ESP32 Wemos S2 Mini V1.0.0
# --------------------------------------
# Main usage:         Meterkast: Watermeter
# --------------------------------------
# Default Installed features:
# - API transport encryption
# - Friendly name substitution
# - ESP restart switch
# - System uptime sensor
# - WiFi IP address sensor
# - WiFi signal strength sensor
# --------------------------------------
# Additional Installed features:
# - LJ18A3-8-Z-BX 5V proximity sensor
# ======================================
esphome:
  name: s2v10-02
  on_boot:
    then:
      - pulse_counter.set_total_pulses:
          id: pulse_counter_water
          value: !lambda 'return id(global_water_total_from230718);'
  on_shutdown:
    then:
      - globals.set:
          id: global_water_total_from230718
          value: !lambda 'return id(water_total_liter_from230718).state;'

interval:
  - interval: 30s
    then:
      - globals.set:
          id: global_water_total_from230718
          value: !lambda 'return id(water_total_liter_from230718).state;'

esp32:
  board: lolin_s2_mini
  variant: ESP32S2
  framework:
    type: arduino

preferences:
  flash_write_interval: 60s

# Global variables
globals:
  - id: global_water_total_from230718
    type: int
    restore_value: yes

substitutions:
  friendly_name: S2V10-02

# Enable logging
logger:
  level: DEBUG

# Enable Home Assistant API
api:
  encryption:
    key: !secret esph_api_encryption_key

ota:
  password: !secret pwd_ota_s2v10_02

wifi:
  ssid: !secret ssid_iot_wlan
  password: !secret pwd_iot_wlan
  # networks:
  # - ssid: !secret ssid_iot_wlan
    # password: !secret pwd_iot_wlan
    # bssid: !secret bssid_iot_ap2
  # - ssid: !secret ssid_iot_wlan
    # password: !secret pwd_iot_wlan
    # bssid: !secret bssid_iot_ap1
  domain: !secret ip_domain
  manual_ip:
    static_ip: !secret ip_esp_s2v10_2
    gateway: !secret ip_gateway
    subnet: !secret ip_subnet
    dns1: !secret ip_dns1
    dns2: !secret ip_dns2
  ap:
    ssid: !secret ssid_fbap_wlan_s2v10_02
    password: !secret pwd_fbap_wlan_s2v10_02

captive_portal:

switch:
  # Expose an ESP32 restart button to HA
  - platform: restart
    name: $friendly_name Restart

text_sensor:

  # Send Wi-Fi Info
  - platform: wifi_info
    ip_address:
      name: $friendly_name IP Address
    ssid:
      name: $friendly_name SSID
    bssid:
      name: $friendly_name BSSID

# Sensors
sensor:

  # Send WiFi signal strength & uptime to HA
  - platform: wifi_signal
    name: $friendly_name WiFi Strength
    update_interval: 60s

  # Send a human readable uptime string
  - platform: uptime
    name: $friendly_name Uptime
    id: uptime_sensor
    update_interval: 60s

# ADDITIONAL SENSORS SETUP
  # Water meter setup
  - platform: pulse_counter
    pin: GPIO16
    update_interval : 6s
    name: $friendly_name Water Rate
    id: pulse_counter_water
    use_pcnt: true
    unit_of_measurement: "l/min"
    icon: "mdi:water-outline"
    total:
      name: $friendly_name Water Total Liter from230718
      id: water_total_liter_from230718
      device_class: water
      state_class: total_increasing
      accuracy_decimals: 0
      unit_of_measurement: "l"
      icon: "mdi:water-plus-outline"

  - platform: template
    name: $friendly_name Water Total from230718
    device_class: water
    state_class: total_increasing
    accuracy_decimals: 3
    unit_of_measurement: "m³"
    icon: "mdi:water-plus-outline"
    lambda: return (id(water_total_liter_from230718).state * 0.001);
    update_interval: 6s

Does anybody have an idea what could be causing this problem, and how I can solve it?

how are you supplying 5v when it’s not plugged into USB? sounds like a power/brown out issue. How much current do you require and how much current can the non-USB PSU supply? wifi is the first to quit when there isn’t enough power being supplied. The fact that it was working for a few weeks doesn’t mean it cant be a power issue. If you were running the PSU at it’s upper limit it will work for a short time before it fails to work anymore. This is a pretty common issue and 9 times out of 10 it’s your power suppy. It’s either bad or it’s not rated for what your using it for.

Thanks Justin,
I thought of that as well.
However, the prox sensor is rated for 10 mA 300mA max, and the original USB power supply that I was using is delivering 2.4 A max at 5V.
This is the sensor: LJ18A3-8-Z/BX - 5V
And amongst others I tried it with an Anker PowerPort II adapter as well, that delivers 3A max at 5V.
So shouldn’t that be enough?
And when I stick the USB cable in an USB port of an Windows 10 PC it doesn’t connect to WiFi either.

In all cases the prox sensor appears to be functioning normally, because when I trigger the prox sensor with some metal the red led on the sensor lights up.

The prox sensor is directly soldered to the ESP32 like this:

So connected to VBUS, GND and GPIO16
Is that incorrect?

I didn’t notice it was an esp32 c2 and i’m not as familiar with those boards as I stick with the tried and true esp32 boards. These boards are still very new and issues are still being worked out, plus they’re not even fully supported yet so you’re just asking for problems unless you like seeking out bugs. I still suspect it’s a power issue, if it’s not the PSU then the next logical place is the onboard regulator being the issue. If you have another board, i’d try it.Screenshot from 2023-08-07 08-21-10

Yes, I can change to another board, but I would prefer to understand the cause of this issue and hopefully solve it.
And the good news (at least for now) is that it is working again.
I tried two new things:
First I added “output_power: 10” to the “wifi:” settings in the ESPHome yaml code, but that didn’t seem to help, at least not right away.
Then I replaced the USB cable with yet another one, a 3 meter long one (the other cables were much shorter), and suddenly the ESP connected to WiFi again, using the original 2.4 A USB power adapter.
So it might be that one of these two things did the trick.
I will first wait it out for some time, and if it keeps working I will try to figure out what exactly was the cure.
When I find something new I will report back.

Just for reference: I forgot to mention that I also disabled two options in the ESPHome yaml code:

  # ap:
    # ssid: !secret ssid_fbap_wlan_s2v10_02
    # password: !secret pwd_fbap_wlan_s2v10_02

# captive_portal:

I think I did this before adding the “output_power: 10” setting, and it didn’t help as well at that moment.

Ofcourse figuring out the problem is always the goal. People mistakenly think trying the code on another board is “giving up” and they’d instead rather beat a dead horse. Switching boards is doing a systematic process of elimination, not giving up. You’re ruling out the current board hardware being the issue and narrowing down what the problem can be. The odds that 4 or 5 usb cords were all bad and causing the same problem/symptoms are extremely unlikely and it working with the longer cord is likely just anecdotal. Did you ever pull up the logs when you had it connected over serial? That would have told you a lot, especially since you chose to produce extra debug data in the logs.

Just for grins. Connect to the “failing” PSU, then connect the ESP ground to the Server ground. If it works now, try a different USB cable.

I am experiencing the same problem. I am using a Seeedstudio Xiao ESP32C with a expansion base and I have had this working for months. I went to create a test version of my deployed esp32c configuration and the new processor only connects to wifi if I keep it connected by usb to my computer that I flashed it with from Esphome web. Everything for the two esphome devices is the same (with the exception of device name and secrets). The newly flashed one starts to boot, displays the first page to a connected I2C display, and then reboots after about 10 sec. It fails to connect to wifi in that time, so I can’t see the logs from the device as to what is failing. Would love to know if you or anyone else has been experiencing this and if they have a resolution.

I did not have these Wi-Fi connection problems anymore after my last post in august.
However, I had other problems as well with this ESP32 S2 board (Wemos S2 Mini v1.0.0) in combination with that proximity sensor: every now and then I suddenly had large false positive signals from the sensor meaning that I supposedly had used hundreds of liters of water in a few minutes.
So in the end I swapped this ESP32 out with an apparently much more reliable Wemos D1 Mini ESP8266, and since then I have a rock solid water meter setup.

I have the same problem. When I connect my ESP32 to the PC via USB A/micro USB cable, the device establishes a connection to the Wifi within 15 seconds.
Without this connection with a normal power adapter, no connection is established. I have already tried different cables and adapters.
Any ideas?

This is my board:

I’m using ESPHOME v2023.11.2

LOG:

INFO ESPHome 2023.11.2
INFO Reading configuration /config/esphome/bluetoothbridge2.yaml...
INFO Starting log output from 192.168.0.39 using esphome API
WARNING Can't connect to ESPHome API for 192.168.0.39: Error connecting to ('192.168.0.39', 6053): [Errno 113] Connect call failed ('192.168.0.39', 6053) (SocketAPIError)
INFO Trying to connect to 192.168.0.39 in the background

I
CODE:

esphome:
  name: "bluetoothbridge2"

esp32:
  board: az-delivery-devkit-v4
  framework:
    type: esp-idf

# Enable logging
logger:
# level: none
# level: error
# level: warn
# level: info
  level: debug
# level: verbose
# level: very_verbose

# Enable Home Assistant API
api:

ota:

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  manual_ip: 
    static_ip: 192.168.0.39
    gateway: 192.168.0.1
    subnet: 255.255.255.0
    #dns1: 192.168.0.212

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  #ap:

# Enable Web server
#web_server:
#  port: 82

captive_portal:
esp32_ble_tracker:  

bluetooth_proxy:
  active: true

I made an account just to respond to this. I was having the same problem and solved it via this thread: ESPHome device does not restart without serial port active (apparently!) - #4 by AussieSusan

My device was either not flashing properly, or power cycling every 10 seconds. Simply removing the logger function completely resolved my issues. Either there is some sort of bug when serial output is not being viewed, or its power hungry, or something, but that is what completely fixed it for me.

1 Like

No, removing logger functions does not change anything for me.
Thanks anyway

Also having the same problem here with a wemos D1 mini pro. Commenting out Logger doesnt help.

1 Like

I had a WiFi connection issue with my ESP32 wroom dev board with Home Assistant using ESPHome. I have discovered that when it was connected via the usb-c onboard port to my PC (when communication -I guess serial- was available), it was connecting to WiFi more or less, not stable though. When the same usb-c cable was used in a usb power socket to power my ESP there was never WiFi connection. After playing around a bit, I connected my ESP32 to power only (so you either use a power only usb cable or the usb-c port on the dev extension board, or you supply power differently).
I think when there is no possibility for the ESP32 to communicate via cable, it will connect to WiFi without any issue.

So to sum it up:
Serial - NO WiFi
No Serial (power only) - WiFi

Removing the logger did resolve this issue for me. Like the poster in the linked issue I am also using the Seeed Xiao ESP32C3. Thanks!

A lot of Arduino code for the ESP32C will start the serial port with…
Serial.begin(115200), or some other baud rate, and then wait for
the serial port to get setup by the processor before continuing…

while (!Serial) {delay(10)); or some loop like this to wait until the
port becomes active. Without this wait, the serial port often won’t
work. However, if there is no USB listener then Serial will never
return true and the loop just waits forever and the code stops here.

Some programs just do a delay(1000); or some number of
seconds delay assuming that will be enough time. This is safer but
causes a longer startup time if the port initialized say in only 100 of
those milliseconds.

Removing any Serial “logging” functions may have just removed this
potential to wait indefinitely for the Serial port to find someone to talk to.

The best way is to wait with a timeout as shown here: Arduino wait for Serial ready with timeout · GitHub