目录
- 1.公共配置文件
- 2.设备2:台灯
- 3.控制器(http.post)
- 4.获取状态(http.get)
1.公共配置文件
#wifi.yaml
wifi:
networks:
- ssid: "123"
password: "www.123.com"
- ssid: "456"
password: "www.123.com"
# 当连接不上指定wifi,开启热点配网
ap:
ssid: "设备配网"
# 强制门户
captive_portal:
# web界面
web_server:
port: 80
2.设备2:台灯
主要是控制一个IO口
substitutions: { desc: 台灯, devicename: sensor }
esphome:
name: $devicename
platform: ESP8266
board: nodemcuv2
arduino_version: latest
# Enable logging
logger:
# Enable Home Assistant API
api:
encryption:
key: "MhXiJqKKyCXTqjZWqtegaP1tQSUpPtbnXP9iV1i2TzE="
ota:
password: "8e9c493c1fda598d0789f514507f3538"
packages:
wifi: !include common/wifi.yaml
output:
- pin: 2
id: led_pin2
platform: gpio
light:
- id: led
output: led_pin2
platform: binary
name: "${devicename}_led" # ${devicename}_led 的实际参数是 sensor_led
3.控制器(http.post)
esphome:
name: http
friendly_name: http
esp8266:
board: nodemcuv2
# Enable logging
logger:
# Enable Home Assistant API
api:
encryption:
key: "hGNx5NzLL+UnGsUWxs6tvYghEZDgVfQjOcsMQaMgrg4="
ota:
password: "65290e2cae0c69b31b50bdb80bcd4f4c"
packages:
wifi: !include common/wifi.yaml
http_request:
useragent: esphome/device
id: my_request
timeout: 10s
interval:
- interval: 2s
then:
- http_request.post: http://sensor.local/light/sensor_led/toggle
interval
启动了一个定时器,2s执行一次http_request.post
动作
当你的设备2:台灯
正常连接上网络,在局域网中,可以访问
http://sensor.local/light/sensor_led/toggle
获取设备2:台灯
的状态
因为我们使用的浏览器是get
,只能获取设备的状态,并不能控制设备
想要控制设备,就要使用到post
- http_request.post: http://sensor.local/light/sensor_led/toggle
和点击web网页上面的按钮效果是一样的
4.获取状态(http.get)
如果是想获取设备的状态,并不是控制设备,可以使用get
esphome:
name: http
friendly_name: http
esp8266:
board: nodemcuv2
# Enable logging
logger:
# Enable Home Assistant API
api:
encryption:
key: "hGNx5NzLL+UnGsUWxs6tvYghEZDgVfQjOcsMQaMgrg4="
ota:
password: "65290e2cae0c69b31b50bdb80bcd4f4c"
packages:
wifi: !include common/wifi.yaml
http_request:
useragent: esphome/device
id: my_request
timeout: 10s
interval:
- interval: 10s
then:
# - http_request.post: http://sensor.local/light/sensor_led/toggle
- http_request.get:
url: "http://sensor.local/light/sensor_led"
on_response:
- lambda: |-
ESP_LOGD("http_request", "data: %s", id(my_request).get_string());
如果访问成功,就将获取到的数据通过ESP_LOGD
打印出来