I’ve recently made a motion activated lighting system to illuminate my stairs during the night. The circuit runs on battery power since there is no easy access to the mains where I want to install it.
The circuit has a connector for the LEDs, two PIR sensors and one BH1750 light sensor. Everything is controlled by an ESP32 WROOM 32E.
The ESP should spend most of the time sleeping to conserve battery life and only come on when motion is detected, take a reading from the BH1750 sensor and turn the lights on / go back to sleep depending on what the light level is.
The issue I believe I have is that the I2C connection to the BH1750 takes too long and the check of the BH1750’s state returns nan when waking from deep sleep first. I’ve tried delays up to 1s to allow the sensor to initialize without luck. Is there a solution that would allow the sensor to connect and be read quicker or some other way of implementing this functionality?
Here is my configuration:
esphome:
name: treppenlichter-oben
friendly_name: Treppenlichter Oben
esp32:
board: esp32dev
framework:
type: arduino
# Enable logging
logger:
ota:
- platform: esphome
password: "***"
wifi:
fast_connect: true
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Treppenlichter-Oben"
password: "***"
mqtt:
broker: !secret mqtt_broker
username: mqtt-user
password: !secret mqtt_password
topic_prefix: homeassistant/treppenhaus
i2c:
sda: GPIO21
scl: GPIO22
sensor:
- platform: bh1750
name: "Treppenlicht Oben Helligkeit"
id: treppenlicht_oben_helligkeit
address: 0x23
update_interval: never
- platform: adc
name: "Treppenlicht Oben Spannung"
pin: GPIO35
attenuation: auto
filters:
- multiply:
1.33
binary_sensor:
- platform: gpio
id: PIR_unten
pin:
number: GPIO2
allow_other_uses: true
publish_initial_state: true
on_press:
- component.update: treppenlicht_oben_helligkeit
- if:
condition:
lambda: |-
return id(treppenlicht_oben_helligkeit).state < 30;
then:
script.execute: script_pir_unten
else:
deep_sleep.enter:
id: deep_sleep_treppenlicht_oben
- platform: gpio
id: PIR_oben
pin:
number: GPIO34
allow_other_uses: true
publish_initial_state: true
on_press:
- component.update: treppenlicht_oben_helligkeit
- if:
condition:
lambda: |-
return id(treppenlicht_oben_helligkeit).state < 30;
then:
script.execute: script_pir_oben
else:
deep_sleep.enter:
id: deep_sleep_treppenlicht_oben
output:
- platform: ledc
pin: GPIO32
id: LED_1_OUTPUT
- platform: ledc
pin: GPIO33
id: LED_2_OUTPUT
- platform: ledc
pin: GPIO25
id: LED_3_OUTPUT
- platform: ledc
pin: GPIO14
id: LED_4_OUTPUT
- platform: ledc
pin: GPIO27
id: LED_5_OUTPUT
- platform: ledc
pin: GPIO26
id: LED_6_OUTPUT
- platform: ledc
pin: GPIO13
id: LED_7_OUTPUT
- platform: ledc
pin: GPIO23
id: LED_8_OUTPUT
- platform: ledc
pin: GPIO19
id: LED_9_OUTPUT
light:
- platform: monochromatic
output: LED_1_OUTPUT
id: LED_1
internal: true
- platform: monochromatic
output: LED_2_OUTPUT
id: LED_2
internal: true
- platform: monochromatic
output: LED_3_OUTPUT
id: LED_3
internal: true
- platform: monochromatic
output: LED_4_OUTPUT
id: LED_4
internal: true
- platform: monochromatic
output: LED_5_OUTPUT
id: LED_5
internal: true
- platform: monochromatic
output: LED_6_OUTPUT
id: LED_6
internal: true
- platform: monochromatic
output: LED_7_OUTPUT
id: LED_7
internal: true
- platform: monochromatic
output: LED_8_OUTPUT
id: LED_8
internal: true
- platform: monochromatic
output: LED_9_OUTPUT
id: LED_9
internal: true
deep_sleep:
id: deep_sleep_treppenlicht_oben
esp32_ext1_wakeup:
pins:
- number: GPIO2
allow_other_uses: true
- number: GPIO34
allow_other_uses: true
mode: ANY_HIGH
wakeup_pin_mode: KEEP_AWAKE
script:
- id: script_pir_unten
then:
- delay: 10ms
- light.turn_on:
id: LED_1
brightness: 25%
- delay: 100ms
- light.turn_on:
id: LED_2
brightness: 25%
- delay: 100ms
- light.turn_on:
id: LED_3
brightness: 25%
- delay: 100ms
- light.turn_on:
id: LED_4
brightness: 25%
- delay: 100ms
- light.turn_on:
id: LED_5
brightness: 25%
- delay: 100ms
- light.turn_on:
id: LED_6
brightness: 25%
- delay: 100ms
- light.turn_on:
id: LED_7
brightness: 25%
- delay: 100ms
- light.turn_on:
id: LED_8
brightness: 25%
- delay: 100ms
- light.turn_on:
id: LED_9
brightness: 25%
- delay: 30s
- light.turn_off: LED_1
- light.turn_off: LED_2
- light.turn_off: LED_3
- light.turn_off: LED_4
- light.turn_off: LED_5
- light.turn_off: LED_6
- light.turn_off: LED_7
- light.turn_off: LED_8
- light.turn_off: LED_9
- delay: 1s
- deep_sleep.enter:
id: deep_sleep_treppenlicht_oben
- id: script_pir_oben
then:
- delay: 10ms
- light.turn_on:
id: LED_9
brightness: 25%
- delay: 100ms
- light.turn_on:
id: LED_8
brightness: 25%
- delay: 100ms
- light.turn_on:
id: LED_7
brightness: 25%
- delay: 100ms
- light.turn_on:
id: LED_6
brightness: 25%
- delay: 100ms
- light.turn_on:
id: LED_5
brightness: 25%
- delay: 100ms
- light.turn_on:
id: LED_4
brightness: 25%
- delay: 100ms
- light.turn_on:
id: LED_3
brightness: 25%
- delay: 100ms
- light.turn_on:
id: LED_2
brightness: 25%
- delay: 100ms
- light.turn_on:
id: LED_1
brightness: 25%
- delay: 30s
- light.turn_off: LED_9
- light.turn_off: LED_8
- light.turn_off: LED_7
- light.turn_off: LED_6
- light.turn_off: LED_5
- light.turn_off: LED_4
- light.turn_off: LED_3
- light.turn_off: LED_2
- light.turn_off: LED_1
- delay: 1s
- deep_sleep.enter:
id: deep_sleep_treppenlicht_oben