跳转到内容

ESP32 漏水检测器(带通知)

利用 ESP32 的电容式触摸 GPIO,使用 ESPHome 构建漏水检测器相对容易。 选择 M5StickC 作为平台,是因为它集成了 Grove 连接器,便于整洁的连接,而且它本身有 外壳。内置显示屏是加分项,但并非绝对必需。通知通过 Home Assistant 的 alert 和 notify 设施完成,并经由 Pushover 发送到 iOS 和 Android。

  • 带 USB-C 线缆的 USB 电源 (M5StickC 自带图片中可见的短黑色 USB 线缆)

由于探针自带 3.5mm 公头插头,我们在 Grove 端口连接了一个 3.5mm 母头插孔, 以便将来方便更换探针。橙色接橙色,蓝色接蓝色。探针另一侧的焊盘与正面内部 连接方式相同。3.5mm 插孔的中间两个环不使用。没有极性——只需将其中一个端子 (橙色或蓝色)连接到 Grove GND,另一个连接到 Grove G33。

NOTE

尽管 M5StickC 内部有电池(可能足以应对短时断电),你仍需要 24/7 让设备保持 USB 供电。

你需要将 OpenSans-Regular 字体(或你 选择的其他字体)与你的 ESPHome yaml 文件放在一起。参见 显示。

一切连接并刷写完成后,启用 esp32_touch: 的 setup_mode: true,然后在触摸式二值传感器 (GPIO33)上调整阈值设置,以找到适合你特定湿度传感器和线缆情况的适当数值。 拿一杯水来测试,再给自己倒一杯,然后边查看日志边反复蘸水测试。 你的目标是找到一个阈值,使其在水中触发二值传感器,而在其他情况下不触发。 更多信息请参阅 ESP32 触摸按键。


未展示:探针放置在房间最低处、角落的地板上,不碍事


substitutions:
devicename: basement_leak_detector
friendly_name: Basement Leak Detector
device_description: Leak detector in basement utility room
esphome:
name: $devicename
comment: ${device_description}
esp32:
board: m5stick-c
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
password: !secret fallback_ap_password
captive_portal:
logger:
# Enable Home Assistant API & OTA Updates
api:
ota:
platform: esphome
status_led:
pin:
number: GPIO10
inverted: true
spi:
clk_pin: GPIO13
mosi_pin: GPIO15
i2c:
- id: bus_a
sda: GPIO21
scl: GPIO22
scan: true
esp32_touch:
#setup_mode: true
iir_filter: 10ms
binary_sensor:
- platform: gpio
pin:
number: GPIO37
inverted: true
id: button_a
on_press:
then:
- switch.toggle: led1
- platform: gpio
pin:
number: GPIO39
inverted: true
id: button_b
on_press:
then:
- switch.toggle: led1
- platform: status
name: "${friendly_name} Status"
- platform: esp32_touch
name: "${friendly_name} Sensor"
device_class: moisture
id: leak
pin: GPIO33
threshold: 30
on_press:
- while:
condition:
binary_sensor.is_on: leak
then:
- switch.turn_on: led1
- delay: 200ms
- switch.turn_off: led1
- delay: 100ms
- switch.turn_on: led1
- delay: 200ms
- switch.turn_off: led1
- delay: 400ms
on_release:
then:
- switch.turn_off: led1
sensor:
- platform: uptime
name: "${friendly_name} Uptime"
# AXP192 power management - must be present to initialize TFT power on
- platform: axp192
address: 0x34
i2c_id: bus_a
update_interval: 30s
battery_level:
name: "${friendly_name} Battery Level"
id: "m5stick_batterylevel"
- platform: wifi_signal
name: "${friendly_name} WiFi Signal"
update_interval: 60s
text_sensor:
- platform: version
name: "${friendly_name} ESPHome version"
switch:
- platform: gpio
pin:
number: GPIO10
inverted: true
id: led1
font:
- file: 'OpenSans-Regular.ttf'
id: font1
size: 66
color:
- id: color_wet
red: 100%
green: 0%
blue: 0%
- id: color_dry
red: 0%
green: 100%
blue: 0%
# built-in 80x160 TFT
display:
- platform: st7735
model: "INITR_MINI160X80"
device_height: 160
device_width: 82
col_start: 0
row_start: 0
eight_bit_color: false
cs_pin: GPIO5
dc_pin: GPIO23
reset_pin: GPIO18
invert_colors: true
use_bgr: true
lambda: |-
if (id(leak).state) {
it.fill(COLOR_OFF);
it.print(42, -24, id(font1), id(color_wet), TextAlign::TOP_CENTER, "W");
it.print(42, 32, id(font1), id(color_wet), TextAlign::TOP_CENTER, "E");
it.print(42, 85, id(font1), id(color_wet), TextAlign::TOP_CENTER, "T");
} else {
it.fill(COLOR_OFF);
it.print(42, -24, id(font1), id(color_dry), TextAlign::TOP_CENTER, "D");
it.print(42, 32, id(font1), id(color_dry), TextAlign::TOP_CENTER, "R");
it.print(42, 85, id(font1), id(color_dry), TextAlign::TOP_CENTER, "Y");
}

共有两部分:一部分用于告警条件,另一部分用于通过 Pushover 发送通知。 更多详情请参阅 Home Assistant 的 Pushover 集成文档。

alert:
basement_water_leak:
name: Water detected in the basement
entity_id: binary_sensor.basement_leak_detector_sensor
state: 'on'
repeat: 30
can_acknowledge: true
notifiers:
- water_leak_notification_group
notify:
- name: pushover
platform: pushover
api_key: !secret pushover_api_key
user_key: !secret pushover_user_key
- name: water_leak_notification_group
platform: group
services:
- service: pushover
data:
data:
# Pushover-provided sound effect (https://pushover.net/api#sounds)
sound: alien

享受你这个本地控制、带推送通知的漏水检测器吧!