Uart Text Sensor (Hexadecimal to decimal)

Hi everybody, I’m pretty new on HA and EspHome. I have a sensor that returns 5 characters through uart in hexadecimal format, for example the ascii characters 00500 → 30:30:35:30:30 in hexa. I need to compare these values and change the state of a GPIO, but for that I need them to be in decimal format and be readable by HA. Is there any form to change these values from Hexa to Decimal? I am using UART Text Sensor.

Here is my yaml file:


esphome:

  name: serial-inficon

  friendly_name: serial_inficon

  includes:

    - datos/uart_read_line_sensor.h

esp8266:

  board: nodemcuv2

# Example configuration entry

web_server:

  port: 80

# 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: "xxxxxxxxxxxx"

ota:

  password: "xxxxxxx"

wifi:

  ssid: !secret wifi_ssid

  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails

  ap:

    ssid: "Serial-Inficon Fallback Hotspot"

    password: "xxxxxx"

captive_portal:

uart:

  id: uart_bus

  tx_pin: GPIO1

  rx_pin: GPIO3

  baud_rate: 9600

  debug:

    direction: BOTH

    dummy_receiver: false

interval:

  - interval: 10s

    then:

      - uart.write: '%'

     

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"    

   

switch:

  - platform: uart

    name: "Send_R"

    data: 'R'

   

binary_sensor:

  - platform: gpio

    pin: GPIO13

    name: serial1

    device_class: motion

  - platform: gpio

    pin: GPIO15

    name: serial2

    device_class: motion

infiresponse

In the image you can see the responses I get from the sensor (30:30:30:30:30). I need to change that data to Decimal, it should be 00000. The lambda publishes the state of the buffer. Any Ideas how to do that?

Take a look at this post for example conversion of the hexadecimal vector into a text string and also processing the response to update sensors.

Hello mulcmu, I tried your aproach but I get a lot of “trash data” like numbers and “???” in RawString template even when I am not requesting the info from the sensor. The closest I got was with the UART readline but I can’t parse the info I get from the custom sensor, I tried to get the position I need from the buffer and compare it with a value to trigger a binary sensor but I cant get it.

I think it is something of the sensor, because I tried your program with an Arduino sending data through serial and receiving with an ESP and it works.

Could you post the yaml that is giving the “trash data” and I’ll take a look at it?

1 Like

Hello again, thanks for replying. Each time I send “%” sensor will reply with the measure value. But now I keep getting all that data. Here is the Yaml.

esphome:
  name: serial-inficon
  friendly_name: serial_inficon
  includes:
   # - datos/uart_read_line_sensor.h
#    - datos/my_custom_component.h

esp8266:
  board: nodemcuv2

# Example configuration entry
web_server:
  port: 80

# Enable logging

# Enable Home Assistant API
api:
  encryption:
    key: "xxxxxxxxx"

ota:
  password: "xxxxxxxxxx"

wifi:
  ssid: !xxxxxxxxx  
  password: !xxxxx

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

captive_portal:

logger:
  baud_rate: 9600

uart:
  baud_rate: 9600
  tx_pin: GPIO1
  rx_pin: GPIO3
  debug:
    direction: RX
    dummy_receiver: true
    after:
      delimiter: "\n"
    sequence:
      - lambda: |-
          UARTDebug::log_string(direction, bytes);  //Still log the data
          
          int sensorID=0;
          float sensorTEMP=0; 
          
          //Example to convert uart text to string
          std::string str(bytes.begin(), bytes.end());

          //watch for potential problems with non printable or special characters in string
          id(rawString).publish_state(str.c_str());
          
        

          //Sample binary / hex data
          if (bytes.size()==9) {
            if(bytes[0]==0x40)
              id(binData).publish_state( bytes[4] );
          }

text_sensor:
  - platform: template
    name: "Raw String"
    id: "rawString"

sensor:
  - platform: template
    name: "Temp 1"
    id: "temp1"
  - platform: template
    name: "Temp 2"
    id: "temp2"
  - platform: template
    name: "Binary Data"
    id: binData


button:
  - platform: template
    name: "Test Button %"
    on_press:
      - uart.write: "%"
  - platform: template
    name: "Test Button R"
    on_press:
      - uart.write: "R"
       

#if you need to send a request periodically to get response from the uart device 
interval:
  - interval: 60s
    then:
      - uart.write:  "%"     

I am trying other options in the logger right now. I removed the part of the temperature sensors from your sketch.

I am 99,9% sure that my sensor is the problem. I need to make some changes then try again. Thank you for your time, I will update news if I have something.

Looks like the logger got enabled. Put the baud_rate: back to 0

1 Like

Its working now, THANK YOU so much mulcmu for your help :pray:, I get the 5 digit responses as needed.
stringmulcmu

Thank you again.

1 Like