ESPHome automation causing rebooting of ESP32

Hi everyone,
I am having a simple siren automation on ESP32 Wroom. I want to run the siren for a minute or a little longer. Five seconds after starting the script ESP32 reboots. The logs don’t have much useful information for debugging.

output:
  - platform: ledc
    pin: 13
    id: buzzer

switch:
  - platform: output
    output: buzzer
    name: "Door Alarm"

script:
  - id: siren
    then:
      - lambda: |-
          auto b = id(buzzer);

          b->turn_on();
          b->set_level(0.1);

          for (int i = 0; i < 10; i++) {
            b->update_frequency(2000);
            delay(500);
            b->update_frequency(2500);
            delay(500);
          }
          b->turn_off();

binary_sensor:
  - platform: gpio
    pin:
      number: 4
    name: "Door opened"
    device_class: tamper
    on_state:
      then:
        - script.execute: siren
[20:29:59][V][ledc.output:114]: Setting duty: 65535 on channel 0
[20:29:59][V][ledc.output:114]: Setting duty: 6554 on channel 0
[20:29:59][D][ledc.output:055]: Calculating resolution bit-depth for frequency 2000.000000
[20:29:59][D][ledc.output:060]: Resolution calculated as 15
[20:29:59][V][ledc.output:191]: Using Arduino API - Trying to define channel, frequency and bit depth...
[20:29:59][V][ledc.output:198]: Trying initialize channel 0 with frequency 2000.0 and bit depth of 15...
[20:29:59][V][ledc.output:203]: Configured frequency: 1996 with bit depth: 15
[20:29:59][V][ledc.output:114]: Setting duty: 3277 on channel 0
[20:30:00][D][ledc.output:055]: Calculating resolution bit-depth for frequency 2500.000000
[20:30:00][D][ledc.output:060]: Resolution calculated as 14
[20:30:00][V][ledc.output:191]: Using Arduino API - Trying to define channel, frequency and bit depth...
[20:30:00][V][ledc.output:198]: Trying initialize channel 0 with frequency 2500.0 and bit depth of 14...
[20:30:00][V][ledc.output:203]: Configured frequency: 2500 with bit depth: 14
[20:30:00][V][ledc.output:114]: Setting duty: 1638 on channel 0
[20:30:00][D][ledc.output:055]: Calculating resolution bit-depth for frequency 2000.000000
[20:30:00][D][ledc.output:060]: Resolution calculated as 15
[20:30:01][V][ledc.output:191]: Using Arduino API - Trying to define channel, frequency and bit depth...
[20:30:01][V][ledc.output:198]: Trying initialize channel 0 with frequency 2000.0 and bit depth of 15...
[20:30:01][V][ledc.output:203]: Configured frequency: 1996 with bit depth: 15
[20:30:01][V][ledc.output:114]: Setting duty: 3277 on channel 0
[20:30:01][D][ledc.output:055]: Calculating resolution bit-depth for frequency 2500.000000
[20:30:01][D][ledc.output:060]: Resolution calculated as 14
[20:30:01][V][ledc.output:191]: Using Arduino API - Trying to define channel, frequency and bit depth...
[20:30:01][V][ledc.output:198]: Trying initialize channel 0 with frequency 2500.0 and bit depth of 14...
[20:30:01][V][ledc.output:203]: Configured frequency: 2500 with bit depth: 14
[20:30:01][V][ledc.output:114]: Setting duty: 1638 on channel 0
[20:30:02][D][ledc.output:055]: Calculating resolution bit-depth for frequency 2000.000000
[20:30:02][D][ledc.output:060]: Resolution calculated as 15
[20:30:02][V][ledc.output:191]: Using Arduino API - Trying to define channel, frequency and bit depth...
[20:30:02][V][ledc.output:198]: Trying initialize channel 0 with frequency 2000.0 and bit depth of 15...
[20:30:02][V][ledc.output:203]: Configured frequency: 1996 with bit depth: 15
[20:30:02][V][ledc.output:114]: Setting duty: 3277 on channel 0
[20:30:02][D][ledc.output:055]: Calculating resolution bit-depth for frequency 2500.000000
[20:30:02][D][ledc.output:060]: Resolution calculated as 14
[20:30:02][V][ledc.output:191]: Using Arduino API - Trying to define channel, frequency and bit depth...
[20:30:02][V][ledc.output:198]: Trying initialize channel 0 with frequency 2500.0 and bit depth of 14...
[20:30:02][V][ledc.output:203]: Configured frequency: 2500 with bit depth: 14
[20:30:02][V][ledc.output:114]: Setting duty: 1638 on channel 0
[20:30:03][D][ledc.output:055]: Calculating resolution bit-depth for frequency 2000.000000
[20:30:03][D][ledc.output:060]: Resolution calculated as 15
[20:30:03][V][ledc.output:191]: Using Arduino API - Trying to define channel, frequency and bit depth...
[20:30:03][V][ledc.output:198]: Trying initialize channel 0 with frequency 2000.0 and bit depth of 15...
[20:30:03][V][ledc.output:203]: Configured frequency: 1996 with bit depth: 15
[20:30:03][V][ledc.output:114]: Setting duty: 3277 on channel 0
[20:30:03][D][ledc.output:055]: Calculating resolution bit-depth for frequency 2500.000000
[20:30:03][D][ledc.output:060]: Resolution calculated as 14
[20:30:03][V][ledc.output:191]: Using Arduino API - Trying to define channel, frequency and bit depth...
[20:30:03][V][ledc.output:198]: Trying initialize channel 0 with frequency 2500.0 and bit depth of 14...
[20:30:03][V][ledc.output:203]: Configured frequency: 2500 with bit depth: 14
[20:30:03][V][ledc.output:114]: Setting duty: 1638 on channel 0
[20:30:04][D][ledc.output:055]: Calculating resolution bit-depth for frequency 2000.000000
[20:30:04][D][ledc.output:060]: Resolution calculated as 15
[20:30:04][V][ledc.output:191]: Using Arduino API - Trying to define channel, frequency and bit depth...
[20:30:04][V][ledc.output:198]: Trying initialize channel 0 with frequency 2000.0 and bit depth of 15...
[20:30:04][V][ledc.output:203]: Configured frequency: 1996 with bit depth: 15
[20:30:04][V][ledc.output:114]: Setting duty: 3277 on channel 0
[20:30:04][D][ledc.output:055]: Calculating resolution bit-depth for frequency 2500.000000
[20:30:04][D][ledc.output:060]: Resolution calculated as 14
[20:30:04][V][ledc.output:191]: Using Arduino API - Trying to define channel, frequency and bit depth...
[20:30:04][V][ledc.output:198]: Trying initialize channel 0 with frequency 2500.0 and bit depth of 14...
[20:30:04][V][ledc.output:203]: Configured frequency: 2500 with bit depth: 14
[20:30:04][V][ledc.output:114]: Setting duty: 1638 on channel 0
WARNING door-alarm @ 192.168.4.225: Connection error occurred: [Errno 104] Connection reset by peer
INFO Processing unexpected disconnect from ESPHome API for door-alarm @ 192.168.4.225
WARNING Disconnected from API
INFO Successfully connected to door-alarm @ 192.168.4.225 in 0.009s
INFO Successful handshake with door-alarm @ 192.168.4.225 in 0.111s

What buzzer/siren and how is it connected?

My guess is that it draws so much current that it causes your power supply voltage to sag, resetting the ESP.

But without knowing specifics, this is only a guess.

1 Like

Yeah - what Tom said.

Switching LEDC at the rates you have done works fine - I tested it using yaml rather that lambda but the result is the exact same (without the crash):

output:
  - platform: ledc
    pin: 13
    id: buzzer

script:
  - id: siren
    then:
      - output.turn_on: buzzer
      - output.set_level:
          id: buzzer
          level: "10%" 
      - repeat:
          count: 10
          then:
            - output.ledc.set_frequency:
                id: buzzer
                frequency: "2000Hz"
            - delay: .5s  
            - output.ledc.set_frequency:
                id: buzzer
                frequency: "2500Hz" 
            - delay: .5s 
      - output.turn_off: buzzer          
1 Like

I don’t understand the lower level stuff but I believe you have to be careful using delays in lambdas too.

So worth trying zoogara’s approach.

1 Like

You may be right - delays in lambda are blocking, the yaml delay: isn’t. Maybe that’s causing some grief?

2 Likes

Thanks for your reply. It works! I was also suspecting the delays were causing troubles. Was not sure about script execution implementation. Was curious whether scripts called synchronously or async.