Skip to content

Instantly share code, notes, and snippets.

@peyanski
Created April 18, 2023 10:56
Show Gist options
  • Save peyanski/48691397d4cb1acfc4af56c0232342e2 to your computer and use it in GitHub Desktop.
Save peyanski/48691397d4cb1acfc4af56c0232342e2 to your computer and use it in GitHub Desktop.
esphome:
name: soundsensor
friendly_name: SoundSensor
esp8266:
board: d1_mini
# Enable logging
logger:
# Enable Home Assistant API
api:
ota:
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Soundsensor Fallback Hotspot"
password: "subscribe"
captive_portal:
# Example configuration entry
web_server:
port: 80
globals:
- id: esphome_sensitivity
type: float
initial_value: '36.5'
restore_value: yes
- id: esphome_volume
type: int
sensor:
- platform: adc
pin: A0
id: esphome_db
device_class: signal_strength
name: "Db SoundEsp"
icon: "mdi:volume-vibrate"
unit_of_measurement: "db"
update_interval: 2s
raw: true
filters:
- lambda: |-
unsigned int sample;
unsigned long startMillis= millis();
float peakToPeak = 0;
unsigned int signalMax = 0;
unsigned int signalMin = 1024;
while (millis() - startMillis < 500) {
sample = analogRead(A0);
if (sample < 1024){
if (sample > signalMax){
signalMax = sample;
}
else if (sample < signalMin){
signalMin = sample;
}
}
}
peakToPeak = map((signalMax - signalMin),1,1024,1.5,1024);
id(esphome_volume) = peakToPeak;
float state = id(esphome_sensitivity)*log10(peakToPeak)+15;
return(state);
- platform: template
name: "Volume SoundEsp"
icon: "mdi:volume-high"
unit_of_measurement: "%"
update_interval: 2s
lambda: return(map((id(esphome_db).state),15,150,0,100));
- platform: template
name: "RAW SoundEsp"
icon: "mdi:volume-source"
unit_of_measurement: "%"
update_interval: 2s
lambda: return(map(id(esphome_volume),1,1024,0,100));
number:
- platform: template
id: sensitivity_slider
name: "Sensitivity SoundEsp"
icon: "mdi:knob"
update_interval: 5s
initial_value: "36.5"
step: 0.1
min_value: 20
max_value: 40
mode: slider
set_action:
then:
- lambda: id(esphome_sensitivity) = x;
@YorkshireHillbilly
Copy link

I'm getting an error installing, any ideas?

INFO ESPHome 2023.8.2
INFO Reading configuration /config/esphome/esphome-web-33fbf3.yaml...
INFO Generating C++ source...
INFO Compiling app...
Processing soundsensor (board: d1_mini; framework: arduino; platform: platformio/espressif8266@3.2.0)
--------------------------------------------------------------------------------
HARDWARE: ESP8266 80MHz, 80KB RAM, 4MB Flash
Dependency Graph
|-- ESPAsyncTCP-esphome @ 1.2.3
|-- ESPAsyncWebServer-esphome @ 2.1.0
|-- DNSServer @ 1.1.1
|-- ESP8266WiFi @ 1.0
|-- ESP8266mDNS @ 1.2
|-- ArduinoJson @ 6.18.5
Compiling /data/soundsensor/.pioenvs/soundsensor/src/esphome/components/api/api_connection.cpp.o
xtensa-lx106-elf-g++: fatal error: Killed signal terminated program cc1plus
compilation terminated.
*** [/data/soundsensor/.pioenvs/soundsensor/src/esphome/components/api/api_connection.cpp.o] Error 1
========================= [FAILED] Took 78.43 seconds =========================

@Lice2
Copy link

Lice2 commented Dec 14, 2023

Hello, I have a problem with this script.
I used an ESP32 nodemcu-32s.
And get these messages.
[17:04:12][W][component:214]: Component adc.sensor took a long time for an operation (0.51 s).
[17:04:12][W][component:215]: Components should block for at most 20-30ms.

Is there a solution for that?

Mein Code

``esphome:
  name: esp32-bad-oben
  friendly_name: esp32_bad_oben

esp32:
  board: nodemcu-32s
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: "+++++"

ota:
  password: "+++++++"

wifi:
  ssid: "++++++++"
  password: "++++++++++t"
  manual_ip:
    static_ip: 192.168.0.+++
    gateway: 192.168.0.+++
    subnet: 255.255.255.0
    dns1: 192.168.0.+++

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Esp32-Bad-Oben Fallback Hotspot"
    password: "+++++++++"

captive_portal:

switch:
  - platform: restart
    name: "ESP32-Bad-Oben Restart"
    id: "esp32_bad_oben_restart_switch"

globals:

  - id: esphome_sensitivity
    type: float
    initial_value: '36.5'
    restore_value: yes

  - id: esphome_volume
    type: int

i2c:
  sda: 21
  scl: 22
  scan: true
  id: bus_a
  #frequency: 200kHz

sensor:
  - platform: aht10
    variant: aht20
    i2c_id: bus_a
    id: aht_20
    address: 0x38
    temperature:
      id: 'aht_temp'
      name: "Bad Oben Temperatur"
    humidity:
      id: 'aht_humid'
      name: "Bad Oben Luftfeuchtigkeit"
    update_interval: 15s

  - platform: adc
    pin: A0
    id: esphome_db
    device_class: signal_strength
    name: "Dezibel (DB) BadOben"
    icon: "mdi:volume-vibrate"
    unit_of_measurement: "db"
    update_interval: 2s
    raw: true
    filters:
      - lambda: |-
          unsigned int sample;
          unsigned long startMillis= millis(); 
          float peakToPeak = 0; 
          unsigned int signalMax = 0;
          unsigned int signalMin = 1024;
          while (millis() - startMillis < 500) {
            sample = analogRead(A0);
            if (sample < 1024){
                if (sample > signalMax){
                    signalMax = sample;
                }
                else if (sample < signalMin){
                    signalMin = sample;
                }
              }
          }
          peakToPeak = map((signalMax - signalMin),1,1024,1.5,1024);
          id(esphome_volume) = peakToPeak;
          float state = id(esphome_sensitivity)*log10(peakToPeak)+15;  
          return(state);


  - platform: template
    name: "Lautstärke BadOben"
    icon: "mdi:volume-high"
    unit_of_measurement: "%"
    update_interval: 2s
    lambda: return(map((id(esphome_db).state),15,150,0,100));

  - platform: template
    name: "RAW SoundEsp"
    icon: "mdi:volume-source"
    unit_of_measurement: "%"
    update_interval: 2s
    lambda: return(map(id(esphome_volume),1,1024,0,100));

  - platform: internal_temperature
    name: "Interne BAD CPU Temperatur"
    update_interval: 60s

number:
  - platform: template
    id: sensitivity_slider
    name: "Empfindlichkeit BadOben"
    icon: "mdi:knob"
    update_interval: 5s
    initial_value: "36.5"
    step: 0.1
    min_value: 20
    max_value: 40
    mode: slider
    set_action:
      then:
        - lambda:  id(esphome_sensitivity) = x;

external_components:
  - source: github://pr#5198
    components: [ aht10 ]

The Log:
[17:11:05][D][sensor:094]: 'Lautstärke BadOben': Sending state -11.00000 % with 1 decimals of accuracy [17:11:06][D][sensor:094]: 'Dezibel (DB) BadOben': Sending state -inf db with 2 decimals of accuracy [17:11:06][W][component:214]: Component adc.sensor took a long time for an operation (0.51 s). [17:11:06][W][component:215]: Components should block for at most 20-30ms. [17:11:06][D][sensor:094]: 'RAW SoundEsp': Sending state 0.00000 % with 1 decimals of accuracy [17:11:07][D][sensor:094]: 'Lautstärke BadOben': Sending state -11.00000 % with 1 decimals of accuracy [17:11:08][D][sensor:094]: 'Dezibel (DB) BadOben': Sending state 15.00000 db with 2 decimals of accuracy [17:11:08][W][component:214]: Component adc.sensor took a long time for an operation (0.51 s). [17:11:08][W][component:215]: Components should block for at most 20-30ms. [17:11:08][D][sensor:094]: 'RAW SoundEsp': Sending state 0.00000 % with 1 decimals of accuracy [17:11:09][D][sensor:094]: 'Lautstärke BadOben': Sending state 0.00000 % with 1 decimals of accuracy [17:11:10][D][sensor:094]: 'Dezibel (DB) BadOben': Sending state -inf db with 2 decimals of accuracy [17:11:10][W][component:214]: Component adc.sensor took a long time for an operation (0.51 s). [17:11:10][W][component:215]: Components should block for at most 20-30ms. [17:11:10][D][sensor:094]: 'RAW SoundEsp': Sending state 0.00000 % with 1 decimals of accuracy [17:11:11][D][sensor:094]: 'Lautstärke BadOben': Sending state -11.00000 % with 1 decimals of accuracy [17:11:12][D][sensor:094]: 'Dezibel (DB) BadOben': Sending state -inf db with 2 decimals of accuracy [17:11:12][W][component:214]: Component adc.sensor took a long time for an operation (0.50 s). [17:11:12][W][component:215]: Components should block for at most 20-30ms. [17:11:12][D][sensor:094]: 'RAW SoundEsp': Sending state 0.00000 % with 1 decimals of accuracy [17:11:13][D][sensor:094]: 'Lautstärke BadOben': Sending state -11.00000 % with 1 decimals of accuracy [17:11:14][D][sensor:094]: 'Dezibel (DB) BadOben': Sending state -inf db with 2 decimals of accuracy [17:11:14][W][component:214]: Component adc.sensor took a long time for an operation (0.51 s). [17:11:14][W][component:215]: Components should block for at most 20-30ms.

@sixoremenos
Copy link

@Lice2 Did you find a solution? Having the same problem.

@Lice2
Copy link

Lice2 commented Jan 22, 2024

No, unfortunately not.
I now have an outlet in front of my dryer that I monitor.

@sixoremenos
Copy link

Nevertheless, seems to be working. It's a known issue. Thanks for answering.

@Lice2
Copy link

Lice2 commented Jan 23, 2024

Yes gladly. I then gave up on the sound sensor.

@nzrutman
Copy link

@Lice2 I assume that error is from the code in the lambda while (millis() - startMillis < 500)
which looks like it will block for half a second. You could try changing that 500 to 20, which will probably get rid of the warning - but it means the sensor would only be listening for 20ms every update_interval seconds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment