【Homeassistant 与Ultrasonic Distance超声波距离传感器握手】
1. 前言
1.1 超声波距离传感器
超声波距离传感器允许您使用简单的超声波传感器,如HC -SR04(数据表, SparkFun)和 ESPHome 来测量距离。这些传感器通常不能测量超过约两米的任何东西,有时可能会发出一些恼人的咔嗒声。
该传感器平台期望传感器可以在特定引脚上发送触发脉冲,并在进行测量后发出回波脉冲。因为有时(例如,如果没有检测到物体)回波脉冲永远不会返回,这个传感器还有一个超时选项,它指定等待值的时间。
HC -SR04 超声波距离传感器。
1.2 接线
采用esp8266
trigger_pin: D5
echo_pin: D6
Gnd:Gnd
VCC:5V
注意电源一定要接5V,接3.3V传感器失效,无法工作
2. 配置例程
配置基本引脚接口
sensor:
- platform: ultrasonic
trigger_pin: D5
echo_pin: D6
name: "Ultrasonic Sensor"
显示
2.1 配置变量:
- trigger_pin (必需, Pin Schema ): 定期向其发送触发脉冲的输出引脚。
- echo_pin (必需, Pin Schema ): 等待回显的输入引脚。
- name(必需,字符串):传感器的名称。
- update_interval ( Optional , Time ): 检查传感器的时间间隔。默认为60s.
- Sensor的所有其他选项。
2.2 高级选项:
- timeout (可选,浮动):超时的米数。大多数传感器最多只能感应 2 米。默认为 2
- pulse_time ( Optional , Time ): 触发引脚处于活动状态的持续时间。默认为10us.
- id ( Optional , ID ):手动指定用于代码生成的ID。
2.3 完整代码:
esphome:
name: nodemcu-test
esp8266:
board: nodemcuv2
# Enable logging
logger:
# Enable Home Assistant API
api:
ota:
password: "ced33e5545b140bfcb5e681179b9bf33"
wifi:
ssid: "J09 502"
password: "qwertyuiop111"
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Nodemcu-Test Fallback Hotspot"
password: "xzlKdVsXRfIy"
captive_portal:
# Example configuration entry
sensor:
# Example configuration entry(https://esphome.io/components/sensor/ultrasonic.html?highlight=hc)
- platform: ultrasonic
trigger_pin: D5
echo_pin: D6
name: "Saltlevel in percent"
update_interval: 10s
filters:
# Calculates in %
# Replace 0.42 by the height of your container. From the sensor to the bottom.
# I used this website to know how I should multiply my values :https://www.skillsyouneed.com/num/percent-change.html
- lambda: return (0.42-x)*(100/0.42);
unit_of_measurement: "%"
#(https://adonno.com/salt-level-sensor/)
- platform: ultrasonic
trigger_pin: D5
echo_pin: D6
name: "Saltlevel in cm"
update_interval: 10s
filters:
# Replace the 0.42 by the height of your container. From the sensor to the bottom.
# I multiplied by 100 in order to get CM since the sensor works in meters
- lambda: return (0.42-x)*100;
unit_of_measurement: "cm"
测量物体距离目标位置的绝对长度
4. 总结
非常感谢各位大佬的支持,特别是ESPhome,到这里Homeassistant 与Ultrasonic Distance超声波距离传感器握手就算完成了。大家快去探索距离读取的乐趣吧!🥳🥳🥳我们实现对外部世界进行感知🎏,充分认识这个有机与无机的环境🌻,科学地合理地进行创作和发挥效益🛹,然后为人类社会发展贡献一点微薄之力。🏃♂️🏃♂️🏃♂️
参考文献:
Salt level sensor for water softener in Home Assistant with ESPHome
Ultrasonic Distance Sensor