Esphome D1 Mini Keeps Disconnecting From Network

I used a D1 Mini to automate an air freshener following these instructions. It worked well for a month or so but now will not stay connected to my network. A snippet of my ESPHome log:

I tried reflashing the D1 Mini and it would work for a few minutes but then the same thing would happen. I figured I had a faulty D1 so started from scratch with a new D1 Mini. It worked for about 10 minutes then the same thing happened. Reflashed it and it worked again for about 20 minutes before failing. Does anyone have an idea of what might be going on? My code:

substitutions:
  bright_level: "50"
  dark_level: "10"

  dark_mode_sprays: "1" # Number of times to spray in Bathroom Mode

  # Set the Interval Timings here (Minutes)
  interval1: "5"
  interval2: "15"
  interval3: "30"
  interval4: "60"

globals:
  - id: sprays
    type: float
    restore_value: yes

esphome:
  name: "air-freshener"
  friendly_name: Air Freshener

esp8266:
  board: d1_mini 
  restore_from_flash: true

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "hx3CGH3gUAIOyMitRSEQR1twCW7Mxj+0svpoSHyviAo="

ota:


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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Esphome-Web-Ee2Ead"
    password: "jIAnWwI3nU0z"

captive_portal:

binary_sensor:
  - platform: gpio
    id: reset
    pin: D4
    filters:
      - invert:
    on_click:
      min_length: 500ms
      max_length: 5000ms
      then:
        - light.turn_on:
            id: led
            effect: flashfast
        - lambda: |-
            id(sprays) = 0;

select:
  - platform: template
    id: set_interval
    name: Interval
    icon: mdi:timer-outline
    optimistic: true
    restore_value: true
    options:
      - 0 (Bathroom Mode)
      - $interval1 Minutes
      - $interval2 Minutes
      - $interval3 Minutes
      - $interval4 Minutes
    on_value:
      - if:
          condition:
            - switch.is_on: enable
            - lambda: "return atoi(x.c_str()) >= 1;"
          then:
            - switch.turn_on: spray

sensor:
  - platform: template
    id: can_level
    name: "Can Level"
    device_class: pressure
    unit_of_measurement: "Can"
    update_interval: 2s
    lambda: |-
      return ( (2500-id(sprays) )/2500 ) * 100;

  - platform: adc
    id: brightness
    name: Lux
    device_class: illuminance
    unit_of_measurement: lx
    pin: A0
    update_interval: 1s
    filters:
      - lambda: return 85-(x * 150);
      - sliding_window_moving_average:
          window_size: 3
          send_every: 3

    on_value:
      - if:
          condition:
            for:
              time: 20min # Waits for this time before resetting 'Dark Mode'. Default 20min
              condition:
                and:
                  - switch.is_on: enable
                  - lambda: "return atoi(id(set_interval).state.c_str()) == 0;"
                  - lambda: "return id(brightness).state < $dark_level;"
                  - switch.is_off: its_dark
          then:
            - switch.turn_on: its_dark
      - if:
          condition:
            and:
              - switch.is_on: enable
              - lambda: "return atoi(id(set_interval).state.c_str()) == 0;"
              - lambda: "return id(brightness).state > $bright_level;"
              - switch.is_on: its_dark
          then:
            - switch.turn_on: spray
            - delay: !lambda return (1500 * $dark_mode_sprays);
            - switch.turn_off: its_dark

switch:
  - platform: gpio
    id: spray
    name: Spray
    icon: mdi:spray
    pin: D2
    on_turn_on:
      - light.turn_on:
          id: led
          effect: pulser
      - switch.turn_on: timer
      - delay: 0.14s
      - switch.turn_off: spray
      - lambda: |-
          id(sprays) += 1;

  - platform: template
    id: enable
    name: Enable
    icon: mdi:autorenew
    optimistic: true
    on_turn_on:
      - switch.turn_on: timer

  - platform: template
    id: its_dark
    name: Room is Dark
    icon: mdi:brightness-4
    optimistic: true

  - platform: template
    id: timer
    optimistic: true
    on_turn_on:
      - delay: !lambda return atoi(id(set_interval).state.c_str()) * (60000); # Default 60000
      - switch.turn_off: timer
      - if:
          condition:
            - switch.is_on: enable
            - lambda: "return atoi(id(set_interval).state.c_str()) >= 1;"
          then:
            - light.turn_on:
                id: led
                effect: flashfast
            - delay: 1.0s
            - light.turn_off: led
            - switch.turn_on: spray

light:
  - platform: monochromatic
    id: led
    name: LED
    output: out_led
    default_transition_length: 0s
    effects:
      - pulse:
          name: pulser
          transition_length: 0.0s
          update_interval: 0.25s
      - pulse:
          name: flashfast
          transition_length: 0.0s
          update_interval: 0.025s
    on_turn_on:
      - delay: 3s
      - light.turn_off: led

output:
  platform: esp8266_pwm
  pin:
    number: D3
    inverted: true
  id: out_led

HA Errors:

  • Can’t connect to ESPHome API for air-freshener @ 192.168.1.126: Handshake timed out (TimeoutAPIError)
  • Can’t connect to ESPHome API for air-freshener @ 192.168.1.126: Error connecting to [AddrInfo(family=<AddressFamily.AF_INET: 2>, type=<SocketKind.SOCK_STREAM: 1>, proto=6, sockaddr=IPv4Sockaddr(address=‘192.168.1.126’, port=6053))]: [Errno 111] Connect call failed (‘192.168.1.126’, 6053) (SocketAPIError)

Add one of these sensors:

What does it tell you?

Also did you buy the same D1 mini from the same place?

Some are known to be crap quality.

2 Likes

Did you try to reboot your router? I have prety much all my wifi modules from aliexpress and all work fine. I did have problems with connection, but it turned out that it’s router’s fault - you must tweak wifi settings and turn off all fancy non-standard things in 2.4GHz part. Also, every now and then if things start to drop i reboot my mesh and it’s fine again.

Could be worth either trying on a good quality esp32 and/or stripping back to a minimal config and adding features until it fails.

I added the sensor. Says -57dBm. The device is 10 feet away from my AP

1 Like

I did try that yesterday. I have over 25 ESPHome devices and this is the only one that is exhibiting this behavior.

Try to remove device from HA and re-add it. There are some topics with this problem on the forum (bad indicator, errno 11….) and some of them solved it with that, some with ha restart… check that topics out if any of that solutions work for you (do a search in google for error message)

I just had two defective of three ESP32S3 boards in a recent shipment from Amazon. Lost a couple of days to those issues. The first would not even connect to USB on my laptop. The second would boot, load code, then fail every single time attempting a WiFi connection. Definitely try another board. D1 mini’s are inexpensive, thankfully.

I flashed a brand new D1 Mini with the default sketch, checked the logs - all good.
I edited the file with my sketch, checked the logs and it immediately failed so I figured something in my sketch was causing the problem. So I started over with the same D1, installed the default sketch, checked the logs - all good. Then I added a few parts of my sketch and checked the logs after ever addition until the entire sketch was added. Checked the logs - all good!? How can that be? Let it go for 8 hours, still good. Soldered it into the air freshener and has still been working great now for 24 hours.