使用ESPHome烧录固件到ESP32-C3并接入HomeAssistant控制灯

一、安装ESPHome

uv init
uv add esphome
uv run esphome dashboard ./esphome

二、配置ESP32-C3控制灯

注意事项
1. id和name要用英文,不要用中文
2. wifi连接要用2.4GHz,不要用5GHz
3. i2c要注意frequency的设置

文件目录如下
在这里插入图片描述
ESP32-C3开发板
在这里插入图片描述

1.主配置文件esp32c3-luat.yaml

# https://wiki.luatos.com/chips/esp32c3/board.html
esphome:
  name: esp32c3-luat
  friendly_name: esp32c3-luat
#  name_add_mac_suffix: true
  platformio_options:
    board_build.flash_mode: dio
    board_build.mcu: esp32c3

esp32:
  # HARDWARE: ESP32C3 160MHz, 320KB RAM, 4MB Flash
  board: esp32-c3-devkitm-1
  variant: esp32c3
  framework:
    type: esp-idf

# Enable logging
logger:
  hardware_uart: USB_SERIAL_JTAG

packages:
  # MAC: 60:55:f9:73:59:68
  base: !include common/base.yaml
#  luat_light: !include common/light/luat_light.yaml
#  luat_switch: !include common/switch/luat_switch.yaml
  four_light: !include common/light/four_light.yaml
  rgb_light: !include common/light/rgb_light.yaml
  left_right_light: !include common/light/left_right_light.yaml
captive_portal:
#<<: !include common/mechanical/matrix_keypad.yaml

#WARNING GPIO2 is a strapping PIN and should only be used for I/O with care.
#WARNING GPIO8 is a strapping PIN and should only be used for I/O with care.
#WARNING GPIO9 is a strapping PIN and should only be used for I/O with care.
#Attaching external pullup/down resistors to strapping pins can cause unexpected failures.
#See https://esphome.io/guides/faq.html#why-am-i-getting-a-warning-about-strapping-pins
#12 13 This pin cannot be used on ESP32-C3s and is already used by the SPI/PSRAM interface (function: SPIHD).

2.基础通用配置base.yaml

# https://esphome.io/components/wifi
wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  fast_connect: true

# https://esphome.io/components/ota
ota:
  platform: esphome
  password: !secret ota_password

# https://esphome.io/components/api
api:
  encryption:
    key: !secret api_encryption_key
  # 没有连接HA,默认15min会重启一次,设置为0s,将不再重启
  reboot_timeout: 0s

# https://esphome.io/components/web_server
web_server:
  port: 80

# https://esphome.io/components/mdns
mdns:
  disabled: false


text_sensor:
  # https://esphome.io/components/text_sensor/version
  - platform: version
    name: "Version"
  # https://esphome.io/components/text_sensor/wifi_info
  - platform: wifi_info
    mac_address:
      name: "Mac"
    ip_address:
      name: "IP"
      update_interval: 1min
    dns_address:
      name: "DNS"
      update_interval: 1min
    ssid:
      name: "SSID"
      update_interval: 1min
    bssid:
      name: "BSSID"
      update_interval: 1min

3.密码文件secret.yaml

上面base.yaml中!secret wifi_ssid就是引用secret.yaml里面的wifi_ssid的值

# Your Wi-Fi SSID and password
wifi_ssid: "xxx"
wifi_password: "xxx"

api_encryption_key: "xxx"
ota_password: "xxx"
ap_password: "xxx"

4.围栏灯four_light.yaml

# https://esphome.io/components/light/binary
light:
  - platform: binary
    name: four_light
    output: four_light_output

output:
  - id: four_light_output
    platform: gpio
    pin: 0

5.彩灯rgb_light.yaml

# https://esphome.io/components/light/rgb
light:
  - platform: rgb
    id: colorful_light
    name: colorful_light
    red: output_component_red
    green: output_component_green
    blue: output_component_blue
# https://esphome.io/components/light/#pulse-effect
#    effects:
#        - pulse:
#        - pulse:
#            name: "Fast Pulse"
#            transition_length: 0.5s
#            update_interval: 0.5s
#            min_brightness: 0%
#            max_brightness: 100%
#        - pulse:
#            name: "Slow Pulse"
#            # transition_length: 1s      # defaults to 1s
#            update_interval: 2s
#        - pulse:
#            name: "Asymmetrical Pulse"
#            transition_length:
#              on_length: 1s
#              off_length: 500ms
#            update_interval: 1.5s
output:
  # https://esphome.io/components/output/ledc
#  - platform: ledc
#    id: output_component_red
#    pin:
#      number: 4
#      inverted: true
#  - platform: ledc
#    id: output_component_blue
#    pin:
#      number: 3
#      inverted: true
#  - platform: ledc
#    id: output_component_green
#    pin:
#      number: 2
#      inverted: true
  - platform: ledc
    id: output_component_red
    pin:
      number: 1
      inverted: true
  - platform: ledc
    id: output_component_blue
    pin:
      number: 18
      inverted: true
  - platform: ledc
    id: output_component_green
    pin:
      number: 19
      inverted: true

#output:
#  - platform: esp8266_pwm
#    id: output_component_red
#    pin: D1
#  - platform: esp8266_pwm
#    id: output_component_green
#    pin: D2
#  - platform: esp8266_pwm
#    id: output_component_blue
#    pin: D3

6.左右柱灯left_right_light.yaml

# https://esphome.io/components/light/binary
light:
  - platform: binary
    name: left_light
    output: left_light_output
  - platform: binary
    name: right_light
    output: right_light_output
output:
  - id: left_light_output
    platform: gpio
    pin: 6
  - id: right_light_output
    platform: gpio
    pin: 7

三、安装固件

在这里插入图片描述

四、HomeAssistant配置ESPHome

1.直接访问

在这里插入图片描述

2.配置ESPHome地址

如果配置了api_encryption_key,需要填写密钥
在这里插入图片描述

3.接入成功

在这里插入图片描述

### ESP32-C3 烧录电路图及连接方法 对于ESP32-C3模块,在进行固件烧录时,通常有两种主要的方式:通过USB-UART接口和通过JTAG接口。 #### USB-UART 接口烧录 当采用USB-UART方式进行烧录时,需要确保UART通信线路正确连接。具体来说: - TXD (GPIO1) 连接到电脑的RX端; - RXD (GPIO3) 连接到电脑的TX端; - GND接地; - VCC供电; 为了进入下载模式,还需要额外控制两个引脚: - GPIO0拉低至GND可以启动Flash Bootloader模式[^1]。 此外,如果使用的是带有内置USB转串口芯片的核心板,则只需将USB线缆接入开发板上的Micro USB插座即可完成上述连线工作。 #### JTAG 接口烧录 利用ESP32-C3内部集成的USB-JTAG控制器能够简化调试过程支持更复杂的操作。此方案适用于高级开发者或特定应用场景下的需求分析与故障排查。具体的接线如下所示: | JTAG Pin | Description | |----------|----------------------| | TDI | Test Data In | | TDO | Test Data Out | | TCK | Test Clock | | TMS | Test Mode Select | | NRST | Reset | | GND | Ground | 值得注意的是,某些评估套件如HX-DK已经集成了必要的硬件资源来方便用户直接通过USB电缆执行程序加载以及实时监控等功能[^2]。 在实际应用过程中,建议优先考虑官方推荐的方法和技术文档指导来进行设备配置和编程环境搭建,以减少可能出现的问题提高工作效率。 ```mermaid graph LR; A[PC] --> B[TX]; C[RX] --> D[ESP32-C3 UART]; E[VCC] -.-> F[GND]; H(GPIO0) --> I(GND); subgraph "For JTAG" J(TDI)--> K(ESP32-C3 JTAG) L(TDO)--> M(ESP32-C3 JTAG) N(TCK)--> O(ESP32-C3 JTAG) P(TMS)--> Q(ESP32-C3 JTAG) R(NRST)--> S(ESP32-C3 JTAG) T(GND)--> U(ESP32-C3 JTAG) end ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值