Reading RS232 data from weighing Scale using ESPHOME

Hi, I used custom components to read rs232 data from a weighing scale through esp8266 using max232 mini board. By esphome logs I can see the correct continuous serial data, but I cannot display the values as a sensor in homeassistant. my need is to continuously update the homeassistant sensor using the text sensor serial data. Any help is highly appreciated.

esphome:
  name: a12e
  platform: ESP8266  # Use ESP32 if applicable
  board: nodemcuv2    # Adjust this according to your ESP module
  includes:
    - uart_read_line_sensor.h

wifi:
  ssid: 
  password: 

logger:

api:
  encryption:
    key: 

ota:
  password: 

uart:
  id: uart_bus
  tx_pin: GPIO15
  rx_pin: GPIO13
  baud_rate: 9600

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

HI and welcome to HA. Can you please post your logs as well?

Thanks, yeah sure.

Hi Daryl, This is the ESPHome Log I’m getting

[23:28:33][C][uart.arduino_esp8266:102]: UART Bus:
[23:28:33][C][uart.arduino_esp8266:103]:   TX Pin: GPIO15
[23:28:33][C][uart.arduino_esp8266:104]:   RX Pin: GPIO13
[23:28:33][C][uart.arduino_esp8266:106]:   RX Buffer Size: 256
[23:28:33][C][uart.arduino_esp8266:108]:   Baud Rate: 9600 baud
[23:28:33][C][uart.arduino_esp8266:109]:   Data Bits: 8
[23:28:33][C][uart.arduino_esp8266:110]:   Parity: NONE
[23:28:33][C][uart.arduino_esp8266:111]:   Stop bits: 1
[23:28:33][C][uart.arduino_esp8266:115]:   Using software serial
[23:28:33][C][mdns:108]: mDNS:
[23:28:33][C][mdns:109]:   Hostname: a12e
[23:28:33][C][ota:093]: Over-The-Air Updates:
[23:28:33][C][ota:094]:   Address: a12e.local:8266
[23:28:33][C][ota:097]:   Using Password.
[23:28:33][C][api:138]: API Server:
[23:28:33][C][api:139]:   Address: a12e.local:6053
[23:28:33][C][api:141]:   Using noise encryption: YES
[23:29:19][D][text_sensor:064]: 'uart_readline': Sending state '\xffww-135.60kg'
[23:29:19][D][text_sensor:064]: 'uart_readline': Sending state 'ww-135.60kg'
[23:29:19][D][text_sensor:064]: 'uart_readline': Sending state 'ww-135.60kg'
[23:29:19][D][text_sensor:064]: 'uart_readline': Sending state 'ww-135.60kg'
[23:29:19][D][text_sensor:064]: 'uart_readline': Sending state 'ww-135.60kg'
[23:29:19][D][text_sensor:064]: 'uart_readline': Sending state 'ww-135.60kg'
[23:29:19][D][text_sensor:064]: 'uart_readline': Sending state 'ww-135.60kg'
[23:29:20][D][text_sensor:064]: 'uart_readline': Sending state 'ww-135.60kg'

ESP Uart reads the same weight as displayed in the scale.

By figuring the below configuration the values got exposed in homeassistant.

  • internal (Optional, boolean): Mark this component as internal. Internal components will not be exposed to the frontend (like Home Assistant). Only specifying an id without a name will implicitly set this to true.

I added the name for the text sensor in the code.

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

Now I’m trying to separate ‘ww’ text from the output.
image

Finally managed to remove the ‘ww’ preffix and converted the string to float, this is my final esphome code.

uart:
  id: uart_bus
  tx_pin: GPIO15
  rx_pin: GPIO13
  baud_rate: 9600

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

sensor:
  - platform: template
    update_interval: 1s
    name: "Weight"
    id: weight_sensor
    unit_of_measurement: "Kg"
    lambda: |-
      std::string x = id(uart_readline).state;
      return atof(x.c_str());
1 Like