How to use UART to communicate with devices in esphome


This is a program I wrote myself, but it prompts an error <Platform not found: ‘sensor.uart’>. The picture below is the communication format. How should I modify the program?

Hi

Please post the whole code/sketch properly quoted so it’s readable not a screen capture :wink:
For protocol english version would help !

Vincèn

esphome:
  name: pm_sensor
  friendly_name: pm_sensor

esp8266:
  board: esp01_1m


# Enable Home Assistant API
api:

ota:
  - platform: esphome

wifi:
  id: id_wifi
  # fast_connect: true
  reboot_timeout: 600s
  networks:
    - ssid: "bgy"
      password: "msnmsn321"
  ap:
    ssid: "pm_sensor"

captive_portal:

logger:
  level: info # VERBOSE #makes uart stream available in esphome logstream
  baud_rate: 0 #disable logging over uart


web_server:
  port: 80

button:
  - platform: restart
    name: "pm_sensor_restart"

uart:
  rx_pin: GPIO3  # 默认 RX 引脚
  tx_pin: GPIO1  # 默认 TX 引脚
  baud_rate: 9600
  data_bits: 8
  parity: NONE
  stop_bits: 1

sensor:
  - platform: uart
    name: "PM1.0 Sensor"
    uart_id: uart_1
    id: pm10_sensor
    on_uart_data: |-
      id(pm10_sensor).publish_state((x[4] << 8 | x[5]));  # 读取 PM1.0 值
    unit_of_measurement: "µg/m³"

  - platform: uart
    name: "PM2.5 Sensor"
    uart_id: uart_1
    id: pm25_sensor
    on_uart_data: |-
      id(pm25_sensor).publish_state((x[6] << 8 | x[7]));  # 读取 PM2.5 值
    unit_of_measurement: "µg/m³"

  - platform: uart
    name: "PM10 Sensor"
    uart_id: uart_1
    id: pm10_sensor
    on_uart_data: |-
      id(pm10_sensor).publish_state((x[8] << 8 | x[9]));  # 读取 PM10 值
    unit_of_measurement: "µg/m³"
  













here is the link for communication protocol,in chinese

I don’t believe there’s a UART sensor platform. I think this requires building a component.

1 Like

You’re correct, although there’s a bit of a hacky workaround by abusing the debug facility of the UART component.

More info here: How to transfer data from uart - #3 by commanderalpha

Fantasy from Chatgpt?
If you want to read uart on Esphome without specific component, the only way afaik is uart debug with dummy receiver.

uart:
  baud_rate: 9600
  debug:
    direction: BOTH
    dummy_receiver: true
    after:
      delimiter: "\n"
    sequence:
      - lambda: UARTDebug::log_string(direction, bytes);
1 Like

Do you like to try using an external component?

You can try like this.

uart:
  baud_rate: 9600
  data_bits: 8
  parity: NONE
  stop_bits: 1
  rx_pin: GPIO3
  tx_pin: GPIO1

external_components:
  - source: github://eigger/espcomponents/releases/latest
    components: [ uartex ]
    refresh: always

uartex:
  rx_timeout: 10ms
  tx_delay: 50ms
  tx_timeout: 500ms
  tx_retry_cnt: 3

  rx_header: [0x42, 0x4D]
  # rx_footer: "\r\n"
  tx_header: [0x42, 0x4D]
  # tx_footer: "\r\n"

  # rx_checksum: add
  # tx_checksum: add
  rx_checksum2: !lambda |-
    uint8_t crc_1 = 0;
    uint8_t crc_2 = 0;
    for (int i = 0; i < len; i++)
    {
      crc_1 += data[i];
      crc_2 += data[i];
    }
    crc_2 += crc_1;
    return {crc_1, crc_2};


  version:
    disabled: False
  error:
    disabled: False
  log:
    disabled: False

sensor:
  - platform: uartex
    name: "PM1.0 Sensor"
    unit_of_measurement: "µg/m³"
    state: 
      data: [0x00, 0x00]
      mask: [0x00, 0x00]
    state_number:
      offset: 2
      length: 2
      precision: 2
  - platform: uartex
    name: "PM2.5 Sensor"
    unit_of_measurement: "µg/m³"
    state: 
      data: [0x00, 0x00]
      mask: [0x00, 0x00]
    state_number:
      offset: 4
      length: 2
      precision: 2
  - platform: uartex
    name: "PM10 Sensor"
    unit_of_measurement: "µg/m³"
    state: 
      data: [0x00, 0x00]
      mask: [0x00, 0x00]
    state_number:
      offset: 6
      length: 2
      precision: 2
2 Likes

fork and star!when my PCB is ready, I will verify it immediately

This also seems to work:

Use as:

  includes:
    - uart_read_line_sensor.h

(see also ESPHome-Duepi/pelletstove.yaml at cff01436965763a82e9ecec6fec7b0c6e0114c11 · mvroosmalen1970/ESPHome-Duepi · GitHub)

yes,chatgpt sucks

Please do jot post llm generated code to the forum and ask it ‘it is correct’

First it’s against the forum rules. …see below.

Second llms (all of them, yes even deepseek and grok3 and latest gpt whatever.) are currently glorious at generating code fast… But because the cutoff dates trail current HA by nearly a year. And yes it matters… It causes the llm to generate baloney that LOOKS right.

It then takes the volunteers hours to unwind the BS from real chasing thier tails on garbage.

For that reason please don’t post llm generated code and ask people to debug it… Thanks.

3 Likes

sorry sir, i will note that