本帖最后由 pnjycn 于 2025-8-21 18:15 编辑
使用8266esp-01s+1节18650电池,做一个WiFi便捷.
使用01S内置状态指示 LED(GPIO2)做网络状态,第1次点击按钮。
Wi-Fi 连接中 → LED 快速闪烁 Wi-Fi 已连接 → LED 常亮 Wi-Fi 关闭 → LED 熄灭 这样知道联网状态,固定ip地址联网就1~3秒,就可以接下来操作。一分钟没有点击按钮,关闭WiFi进入睡眠模式省电。 直到下次点击激活WiFi,一级2000毫安的18650用个把月是没问题。
<div class="blockcode"><blockquote>esphome:
name: ld2450-4-00000000000
friendly_name: ld2450-4-00000000000
on_boot:
priority: -10
then:
- logger.log: "Booting—disabling Wi-Fi"
- wifi.disable
esp8266:
board: esp01_1m
# Enable logging
logger:
level: DEBUG
# Enable Home Assistant API
api:
password: ""
ota:
- platform: esphome
password: "1"
wifi:
ssid: "你的WiFi名称"
password: " WiFi密码"
fast_connect: true
power_save_mode: none
manual_ip:
static_ip: 192.168.1.130 # 改成你自己的ip,最好把你的路由器绑定这个设备的IP-MAC.避免其他设备占用这个ip地址,如果路由器不方便设置,那就把ip地址尽量往大了设置。
gateway: 192.168.1.1
subnet: 255.255.255.0
ap:
ssid: "cdd-wifi"
password: "12345678"
web_server:
port: 80
local: true
captive_portal:
output:
- platform: gpio
pin:
number: GPIO2
inverted: True
id: led_output
# 用变量保存闪烁状态
globals:
- id: led_state
type: bool
restore_value: no
initial_value: 'false'
binary_sensor:
- platform: gpio
pin:
number: GPIO3 #用于按钮接线
mode: INPUT_PULLUP
inverted: True
name: "WiFi Button"
on_press:
then:
- logger.log: "Button pressed, enabling Wi-Fi"
- wifi.enable
- script.execute: disable_wifi_timer
# 定时关闭 Wi-Fi
script:
- id: disable_wifi_timer
mode: restart
then:
- delay: 60s #设置关闭时间,主要是WiFi耗电,时间设置关系使用方便跟电池的续航。
- logger.log: "Disabling Wi-Fi"
- wifi.disable
# LED 状态
interval:
- interval: 500ms
then:
- if:
condition:
wifi.connected:
then:
- output.turn_on: led_output # 已连接 -> 常亮
else:
- if:
condition:
wifi.enabled:
then:
# Wi-Fi 正在启用但未连接 -> 闪烁
- lambda: |-
id(led_state) = !id(led_state);
if (id(led_state)) {
id(led_output).turn_on();
} else {
id(led_output).turn_off();
}
else:
- output.turn_off: led_output # Wi-Fi 关闭 -> 熄灭
|