ESPHome是一个旨在使管理ESP板尽可能简单的工具,我们不用编程仅通过简单而强大的配置文件就可以控制ESP8266/ESP32系统,并可通过Home Assistant对它们进行远程控制。ESPHome支持ESP8266、ESP32、RP2040等开发平台,支持上百种传感器、控制组件,可帮助我们快速DIY智能家居系统。
ESPHome的工作原理:ESPHome读入YAML配置文件并创建自定义固件,安装到ESP设备上,ESPHome配置中添加的设备或传感器将自动显示在Home Assistant的界面中。
一、器件准备
1、ESP8266模块(ESP-01S)
2、DHT11温湿度传感器模块
3、USB转TTL-CH340模块
二、器件连接
CH340 |
ESP8266 |
ESP8266 |
DHT11 |
3.3V |
3.3V |
3.3V |
VCC |
RX |
TX |
||
TX |
RX |
||
GND |
GPIO0 |
||
GND |
GND |
GND |
GND |
GPIO2 |
DATA |
三、在Home Assistant安装ESPHome
在侧边栏点击“配置”菜单,选择“加载项”进入加载项管理页面,点击屏幕右下角“加载项商店”,搜索或直接选择“ESPHome”。
进入ESPHome安装页面后点击“安装”按钮。等安装完成后,按上图打开那四个选项,然后点击启动(原来安装的按键位置)。
此时Home Assistan的侧边栏就会出现esphome的图标,点击就会打开esphome的配置页面。
四、在ESPHome添加Esp设备
点击屏幕右下角“+ NEW DEVICE”,选择“CONTURE”,
输入要创建的设备名称,点击“NEXT”
选择要烧写的ESP模块型号,我这里选择ESP8266,点击“NEXT”
这里会为每个ESPHome设备生成一个唯一的加密密钥(可直接保存,也可后期通过配置文件调阅),后期需要此密钥才能将设备添加到Home Assistant中。选择“INSTALL”。
这时桌面会出现以下图标
并出现以下界面
五、给ESP设备刷新固件
上述界面分别提示了4种刷固件方式:
第一种,已经成功刷机后,通过wifi自动升级方式刷固件;
第二种,设备连接到本机USB,通过直连方式刷固件;
第三种,设备连接到服务器(如:树莓派)USB,在ESPHome仪表盘进行操作的方式刷固件;
第四种,通过手工下载固件,通过ESPHome的web方式刷固件。
建议先使用第三种方式下载和刷新固件,然后通过第二种方式进行远程升级刷固件。
在这里先选择“CANCEL”退出,进入ESPHome仪表盘后选择“EDIT”进行配置。
原始配置如下:
esphome:
name: esp8266-zl3
esp8266:
board: esp01_1m
# Enable logging
logger:
# Enable Home Assistant API
api:
encryption:
key: "vVWZozV8A08B3kNuFd0LYMb6tJkRdULCdghfgtDF="
ota:
password: "9fde296283bdad02a4d781c63k8ad154"
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Esp8266-Zl2 Fallback Hotspot"
password: "Ds4EAp1uGOgs"
captive_portal:
在这里需要新增设置固定IP、WEB控制和添加传感器等配置代码,同时按实际情况设置WIFI网络和密码,如下:
esphome:
name: esp8266-zl2
esp8266:
board: esp01_1m
# Enable logging
logger:
# Enable Home Assistant API
api:
encryption:
key: "vVWZozV8A08B3kNuFd0LYMb6tJkRdULCdghfgtDF="
ota:
password: "9fde296283bdad02a4d781c63k8ad154"
wifi: #wifi和密码
ssid: "MyWIFI"
password: "pass123456"
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Esp8266-Zl2 Fallback Hotspot"
password: "UtFLBHz8SAui"
manual_ip: #设置固定IP
static_ip: 192.168.0.251
gateway: 192.168.0.1
subnet: 255.255.255.0
captive_portal:
web_server: ## web控制,添加了这2行,就可以在网页端打开设备ip;
port: 80
sensor: #设置dht11传感器
- platform: dht
pin: GPIO1
temperature:
name: "Temperature"
humidity:
name: "Humidity"
update_interval: 60s # 数据每60s监测一次
model: "DHT11"
配置完成后选择右上角“SAVE”保存配置,然后即可点击“INSTALL”进行安装。这时需将之前连接好的ESP8266和DHT11传感器插入Home Assistant服务器USB接口。系统会再次进入刷固件方式界面,请选择方式三。
选择服务器USB接口后,系统就开始刷新固件。第一次需要下载安装编译链,因此所需时间较长,以后就比较快了。
当屏幕出现以下提示,表示固件已经刷新。
INFO Reading configuration /config/esphome/esp8266-zl2.yaml...
INFO Generating C++ source...
INFO Compiling app...
Processing esp8266-zl2 (board: esp01_1m; framework: arduino; platform: platformio/espressif8266 @ 3.2.0)
--------------------------------------------------------------------------------
HARDWARE: ESP8266 80MHz, 80KB RAM, 1MB Flash
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
Dependency Graph
|-- ESPAsyncTCP-esphome @ 1.2.3
|-- ESPAsyncWebServer-esphome @ 2.1.0
| |-- ESPAsyncTCP-esphome @ 1.2.3
| |-- Hash @ 1.0
| |-- ESP8266WiFi @ 1.0
|-- DNSServer @ 1.1.1
|-- ESP8266WiFi @ 1.0
|-- ESP8266mDNS @ 1.2
|-- noise-c @ 0.1.4
| |-- libsodium @ 1.10018.1
|-- ArduinoJson @ 6.18.5
RAM: [==== ] 42.3% (used 34636 bytes from 81920 bytes)
Flash: [===== ] 49.4% (used 506025 bytes from 1023984 bytes)
========================= [SUCCESS] Took 8.06 seconds =========================
INFO Successfully compiled program.
esptool.py v4.4
Serial port /dev/ttyUSB0
Connecting....
Chip is ESP8266EX
Features: WiFi
Crystal is 26MHz
MAC: 5c:cf:7f:8f:26:99
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 460800
Changed.
Configuring flash size...
Auto-detected Flash size: 1MB
Flash will be erased from 0x00000000 to 0x0007cfff...
Compressed 510176 bytes to 357416...
Wrote 510176 bytes (357416 compressed) at 0x00000000 in 8.3 seconds (effective 492.3 kbit/s)...
Hash of data verified.
Leaving...
Hard resetting via RTS pin...
INFO Successfully uploaded program.
INFO Starting log output from /dev/ttyUSB0 with baud rate 115200
选择“STOP”退出,完成固件刷新。
这以后,就可以选择方式一通过WIFI进行固件刷新了(注意:方式一刷机仅需给ESP8266加上3.3V电源,不用接RX、TX串口线,也不用IO0接地)。
INFO Reading configuration /config/esphome/esp8266-zl2.yaml...
INFO Generating C++ source...
INFO Compiling app...
Processing esp8266-zl2 (board: esp01_1m; framework: arduino; platform: platformio/espressif8266 @ 3.2.0)
--------------------------------------------------------------------------------
HARDWARE: ESP8266 80MHz, 80KB RAM, 1MB Flash
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
Dependency Graph
|-- ESPAsyncTCP-esphome @ 1.2.3
|-- ESPAsyncWebServer-esphome @ 2.1.0
| |-- ESPAsyncTCP-esphome @ 1.2.3
| |-- Hash @ 1.0
| |-- ESP8266WiFi @ 1.0
|-- DNSServer @ 1.1.1
|-- ESP8266WiFi @ 1.0
|-- ESP8266mDNS @ 1.2
|-- noise-c @ 0.1.4
| |-- libsodium @ 1.10018.1
|-- ArduinoJson @ 6.18.5
RAM: [==== ] 42.3% (used 34636 bytes from 81920 bytes)
Flash: [===== ] 49.4% (used 506025 bytes from 1023984 bytes)
========================= [SUCCESS] Took 8.07 seconds =========================
INFO Successfully compiled program.
INFO Connecting to 192.168.0.251
INFO Uploading /data/esp8266-zl2/.pioenvs/esp8266-zl2/firmware.bin (510176 bytes)
INFO Compressed to 357428 bytes
Uploading: [============================================================] 100% Done...
INFO Waiting for result...
INFO OTA successful
INFO Successfully uploaded program.
INFO Starting log output from 192.168.0.251 using esphome API
WARNING Can't connect to ESPHome API for 192.168.0.251: Timeout while connecting to ('192.168.0.251', 6053)
INFO Trying to reconnect to 192.168.0.251 in the background
INFO Successfully connected to 192.168.0.251
[22:03:58][I][app:102]: ESPHome version 2022.12.8 compiled on Feb 15 2023, 21:59:45
[22:03:58][C][wifi:504]: WiFi:
[22:03:58][C][wifi:362]: Local MAC: 5C:CF:7F:8F:26:99
[22:03:58][C][wifi:363]: SSID: 'YUYU'[redacted]
[22:03:58][C][wifi:364]: IP Address: 192.168.0.251
[22:03:58][C][wifi:365]: BSSID: B0:73:5D:6D:64:4C[redacted]
[22:03:58][C][wifi:367]: Hostname: 'esp8266-zl2'
[22:03:58][C][wifi:369]: Signal strength: -58 dB ▂▄▆█
[22:03:58][C][wifi:373]: Channel: 1
[22:03:58][C][wifi:374]: Subnet: 255.255.255.0
[22:03:58][C][wifi:375]: Gateway: 192.168.0.1
[22:03:58][C][wifi:376]: DNS1: 0.0.0.0
[22:03:58][C][wifi:377]: DNS2: 0.0.0.0
[22:03:58][C][logger:293]: Logger:
[22:03:58][C][logger:294]: Level: DEBUG
[22:03:58][C][logger:295]: Log Baud Rate: 115200
[22:03:58][C][logger:296]: Hardware UART: UART0
[22:03:58][C][dht:017]: DHT:
[22:03:58][C][dht:018]: Pin: GPIO1
[22:03:58][C][dht:022]: Model: DHT11
[22:03:58][C][dht:027]: Update Interval: 60.0s
[22:03:58][C][dht:029]: Temperature 'Temperature'
[22:03:58][C][dht:029]: Device Class: 'temperature'
[22:03:58][C][dht:029]: State Class: 'measurement'
[22:03:58][C][dht:029]: Unit of Measurement: '°C'
[22:03:58][C][dht:029]: Accuracy Decimals: 1
[22:03:58][C][dht:030]: Humidity 'Humidity'
[22:03:58][C][dht:030]: Device Class: 'humidity'
[22:03:58][C][dht:030]: State Class: 'measurement'
[22:03:58][C][dht:030]: Unit of Measurement: '%'
[22:03:58][C][dht:030]: Accuracy Decimals: 0
[22:03:58][C][captive_portal:088]: Captive Portal:
[22:03:58][C][web_server:125]: Web Server:
[22:03:58][C][web_server:126]: Address: 192.168.0.251:80
[22:03:58][C][mdns:103]: mDNS:
[22:03:58][C][mdns:104]: Hostname: esp8266-zl2
[22:03:58][C][ota:093]: Over-The-Air Updates:
[22:03:58][C][ota:094]: Address: 192.168.0.251:8266
[22:03:58][C][ota:097]: Using Password.
[22:03:58][C][api:138]: API Server:
[22:03:58][C][api:139]: Address: 192.168.0.251:6053
[22:03:58][C][api:141]: Using noise encryption: YES
[22:04:31][D][dht:048]: Got Temperature=15.0°C Humidity=60.0%
[22:04:31][D][sensor:126]: 'Temperature': Sending state 15.00000 °C with 1 decimals of accuracy
[22:04:31][D][sensor:126]: 'Humidity': Sending state 60.00000 % with 0 decimals of accuracy
完成固件刷新后,重新给ESP8266加上3.3V电源(不用接RX、TX串口线,也不用IO0接地),ESPHome仪表盘上该设备已显示为“ONLINE”。
六、将ESP设备集成到Home Assistant
重新启动Home Assistant,进入界面之后,会自动提示发现设备。
点击“配置”,选择“提交”。
按提示输入ESP8266设备的加密密钥(查看配置文件里面encryption—key值),提交后就成功在Home Assistant创建了ESP8266设备。
在“配置”——》“设备”中可看到该设备集成,包括“1个设备”和“2个实体”。
点击“1个设备”,可以查看设备数据。
选择“添加到仪表盘”可以将该设备实体添加到“概览”中。文章来源:https://www.toymoban.com/news/detail-450319.html
文章来源地址https://www.toymoban.com/news/detail-450319.html
到了这里,关于以DHT11、ESP8266为例介绍ESPHome在Home Assistant中的应用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!