Created
April 18, 2023 10:56
-
-
Save peyanski/48691397d4cb1acfc4af56c0232342e2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@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.