ESPHome UART Capture

I have a 433Mhz gateway for a tracking system I work with (similar to Lora, but closed). It has a UART onboard to echo its packets locally. I have created an ESP32 D1 Mini as an experiment to see if I can capture the packets and display the various attributes as sensors in HA. For now, I just want to see if I can see what’s on the serial port as raw data.

I have the following YAML, but I am not seeing anything in the logs. Do I need to do something more simple to just see any echoed packets in the logs? I’ve tried switching D1(TX) and D2(RX) and the baud rates, but I’d expect to see something, even if the baud rate is wrong.

esphome:
  name: orion
  friendly_name: Orion
  includes:
    - uart_read_line_sensor.h

esp8266:
  board: d1_mini

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

# Enable Home Assistant API
api:
  encryption:
    key: "redacted="

ota:
  password: "redacted"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Fallback Hotspot"
    password: "redacted"

captive_portal:

uart:
  id: uart_bus
  tx_pin: D1
  rx_pin: D2
  baud_rate: 115200
  stop_bits: 1



text_sensor:
- platform: custom
  lambda: |-
    auto orion_uart = new UartReadLineSensor(id(uart_bus));
    App.register_component(orion_uart);
    return {orion_uart};
  text_sensors:
    id: "uart_readline"

Logs:

[12:04:56][C][logger:416]: Logger:
[12:04:56][C][logger:417]:   Level: VERBOSE
[12:04:56][C][logger:418]:   Log Baud Rate: 0
[12:04:56][C][logger:420]:   Hardware UART: UART0
[12:04:56][C][uart.arduino_esp8266:102]: UART Bus:
[12:04:56][C][uart.arduino_esp8266:103]:   TX Pin: GPIO5
[12:04:56][C][uart.arduino_esp8266:104]:   RX Pin: GPIO4
[12:04:56][C][uart.arduino_esp8266:106]:   RX Buffer Size: 256
[12:04:56][C][uart.arduino_esp8266:108]:   Baud Rate: 115200 baud
[12:04:56][C][uart.arduino_esp8266:109]:   Data Bits: 8
[12:04:56][C][uart.arduino_esp8266:110]:   Parity: NONE
[12:04:56][C][uart.arduino_esp8266:111]:   Stop bits: 1
[12:04:56][C][uart.arduino_esp8266:115]:   Using software serial
[12:04:56][C][captive_portal:088]: Captive Portal:
[12:04:56][C][mdns:115]: mDNS:
[12:04:56][C][mdns:116]:   Hostname: orion
[12:04:56][V][mdns:117]:   Services:
[12:04:56][V][mdns:119]:   - _esphomelib, _tcp, 6053
[12:04:56][V][mdns:121]:     TXT: friendly_name = Orion
[12:04:56][V][mdns:121]:     TXT: version = 2023.10.6
[12:04:56][V][mdns:121]:     TXT: mac = e098068e69fc
[12:04:56][V][mdns:121]:     TXT: platform = ESP8266
[12:04:56][V][mdns:121]:     TXT: board = d1_mini
[12:04:56][V][mdns:121]:     TXT: network = wifi
[12:04:56][V][mdns:121]:     TXT: api_encryption = Noise_NNpsk0_25519_ChaChaPoly_SHA256
[12:04:56][C][ota:097]: Over-The-Air Updates:
[12:04:56][C][ota:098]:   Address: orion.local:8266
[12:04:56][C][ota:101]:   Using Password.
[12:04:56][C][api:139]: API Server:
[12:04:56][C][api:140]:   Address: orion.local:6053
[12:04:56][C][api:142]:   Using noise encryption: YES

Here’s my include code from ESPHome

#include "esphome.h"

class UartReadLineSensor : public Component, public UARTDevice, public TextSensor {
 public:
  UartReadLineSensor(UARTComponent *parent) : UARTDevice(parent) {}

  void setup() override {
    // nothing to do here
  }

  int readline(int readch, char *buffer, int len)
  {
    static int pos = 0;
    int rpos;

    if (readch > 0) {
      switch (readch) {
        case '\n': // Ignore new-lines
          break;
        case '\r': // Return on CR
          rpos = pos;
          pos = 0;  // Reset position index ready for next time
          return rpos;
        default:
          if (pos < len-1) {
            buffer[pos++] = readch;
            buffer[pos] = 0;
          }
      }
    }
    // No end of line has been found, so return -1.
    return -1;
  }

  void loop() override {
    const int max_line_length = 80;
    static char buffer[max_line_length];
    while (available()) {
      if(readline(read(), buffer, max_line_length) > 0) {
        publish_state(buffer);
      }
    }
  }
};

Try adding the debugging option to the uart block. If the data stream doesn’t have CR characters in it the data stream then the readline sensor will not publish anything to see in the logs.

This example might be helpful as well