本文主要是介绍ESPHome不经过HA设备1直接控制设备2,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
- 1.公共配置文件
- 2.设备2:台灯
- 3.控制器(http.post)
- 4.获取状态(http.get)
- 5.提取Json数据
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: $devicenameplatform: ESP8266board: nodemcuv2arduino_version: latest# Enable logging
logger:# Enable Home Assistant API
api:encryption:key: "MhXiJqKKyCXTqjZWqtegaP1tQSUpPtbnXP9iV1i2TzE="ota:password: "8e9c493c1fda598d0789f514507f3538"packages:wifi: !include common/wifi.yamloutput:- pin: 2id: led_pin2platform: gpiolight:- id: ledoutput: led_pin2platform: binaryname: "${devicename}_led" # ${devicename}_led 的实际参数是 sensor_led
3.控制器(http.post)
esphome:name: httpfriendly_name: httpesp8266:board: nodemcuv2# Enable logging
logger:# Enable Home Assistant API
api:encryption:key: "hGNx5NzLL+UnGsUWxs6tvYghEZDgVfQjOcsMQaMgrg4="ota:password: "65290e2cae0c69b31b50bdb80bcd4f4c"packages:wifi: !include common/wifi.yamlhttp_request:useragent: esphome/deviceid: my_requesttimeout: 10sinterval:- interval: 2sthen:- 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: httpfriendly_name: httpesp8266:board: nodemcuv2# Enable logging
logger:# Enable Home Assistant API
api:encryption:key: "hGNx5NzLL+UnGsUWxs6tvYghEZDgVfQjOcsMQaMgrg4="ota:password: "65290e2cae0c69b31b50bdb80bcd4f4c"packages:wifi: !include common/wifi.yamlhttp_request:useragent: esphome/deviceid: my_requesttimeout: 10sinterval:- interval: 10sthen:# - 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
打印出来
5.提取Json数据
已经获取到Json数据,肯定需要将里面的数据提取出来
esphome:name: httpfriendly_name: httpesp8266:board: nodemcuv2# Enable logging
logger:# Enable Home Assistant API
api:encryption:key: "hGNx5NzLL+UnGsUWxs6tvYghEZDgVfQjOcsMQaMgrg4="ota:password: "65290e2cae0c69b31b50bdb80bcd4f4c"packages:wifi: !include common/wifi.yamlhttp_request:useragent: esphome/deviceid: my_requesttimeout: 10stext_sensor:- platform: templatename: "Switch State"id: switch_state_label- platform: templatename: "id name"id: id_name_label- platform: templatename: "color mode"id: color_mode_labelinterval:- interval: 10sthen:# - 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());json::parse_json(id(my_request).get_string(), [](JsonObject root) {id(switch_state_label).publish_state(root["state"]);id(id_name_label).publish_state(root["id"]);id(color_mode_label).publish_state(root["color_mode"]);});
这篇关于ESPHome不经过HA设备1直接控制设备2的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!