Read data from UART RX pin and send value to HA

Hi, I have a separate Arduino controller that measures spare power from my solar panels and pulses the immersion heater on & off.

This controller transmitts the spare power reading via the Arduino’s serial port. I would like to get that power back into home assistant. Can I use the UART function of the ESPHome.

I cant find many examples on reading data in, I can format the data leaving the Arduino. Therefore the ESP8266 can know what to expect.

Kind regards, Dave

https://esphome.io/custom/uart.html

Hi, could I please have some advise to how to “read” / “see” values in esphome/homeassistant?

I have the PZEM platform showing V, A, Hz etc… correctly.

Now I have connected a real MODBUS RTU to my esphome and I off course get strange values every second. I would like to see the RAW serial data that gets received to see if I can translate that to sensors in HA.

The MODBUS values come from a solar inverter.

I really have googled any topic about modbus / esphome but it is VERY SCARS…

My config:

uart:
  tx_pin: D1
  rx_pin: D2
  baud_rate: 9600
  stop_bits: 1

And this give me CORRECT values when I use a real PZEM (of course):

  - platform: pzemac
    update_interval: 1s
    current:
      name: "${esp_name} A"
    voltage:
      name: "${esp_name} V"
    power:
      name: "${esp_name} W"
      unit_of_measurement: W
      id: "${esp_name}_W"
      icon: mdi:flash-outline
    frequency:
      name: "${esp_name} Hz"
    power_factor:
      name: "${esp_name} Pf"
    energy:
      name: "${esp_name} Wh"

Main question: How can I see the RAW serial data on pin D1 / D2?

I might then be able to use:

EDIT: I also have a hard time understanding how to use above link …

Currently with the MODBUS connected to pzem config I get totally weird values :-):

[14:41:56][D][pzemac:049]: PZEM AC: V=0.1 V, I=8716.288 A, P=216.9 W, E=6815744.0 Wh, F=87.1 Hz, PF=0.00
[14:41:56][V][sensor:013]: 'pzem016modbus V': Received new state 0.100000
[14:41:56][D][sensor:092]: 'pzem016modbus V': Sending state 0.10000 V with 1 decimals of accuracy
[14:41:56][V][sensor:013]: 'pzem016modbus A': Received new state 8716.288086
[14:41:56][D][sensor:092]: 'pzem016modbus A': Sending state 8716.28809 A with 3 decimals of accuracy
[14:41:56][V][sensor:013]: 'pzem016modbus W': Received new state 216.899994
[14:41:56][D][sensor:092]: 'pzem016modbus W': Sending state 216.89999 W with 1 decimals of accuracy
[14:41:56][V][sensor:013]: 'pzem016modbus - Dagverbruik': Received new state 2048.702393
[14:41:56][D][sensor:092]: 'pzem016modbus - Dagverbruik': Sending state 2.04870 kWh with 3 decimals of accuracy
[14:41:56][V][sensor:013]: 'pzem016modbus Wh': Received new state 6815744.000000
[14:41:56][D][sensor:092]: 'pzem016modbus Wh': Sending state 6815744.00000 Wh with 0 decimals of accuracy
[14:41:56][V][sensor:013]: 'pzem016modbus Hz': Received new state 87.099998
[14:41:56][D][sensor:092]: 'pzem016modbus Hz': Sending state 87.10000 hz with 1 decimals of accuracy
[14:41:56][V][sensor:013]: 'pzem016modbus Pf': Received new state 0.000000
[14:41:56][D][sensor:092]: 'pzem016modbus Pf': Sending state 0.00000  with 2 decimals of accuracy
[14:41:57][D][pzemac:049]: PZEM AC: V=0.1 V, I=8650.752 A, P=216.8 W, E=6684672.0 Wh, F=90.0 Hz, PF=0.00
[14:41:57][V][sensor:013]: 'pzem016modbus V': Received new state 0.100000
[14:41:57][D][sensor:092]: 'pzem016modbus V': Sending state 0.10000 V with 1 decimals of accuracy
[14:41:57][V][sensor:013]: 'pzem016modbus A': Received new state 8650.751953
[14:41:57][D][sensor:092]: 'pzem016modbus A': Sending state 8650.75195 A with 3 decimals of accuracy
[14:41:57][V][sensor:013]: 'pzem016modbus W': Received new state 216.800003
[14:41:57][D][sensor:092]: 'pzem016modbus W': Sending state 216.80000 W with 1 decimals of accuracy
[14:41:57][V][sensor:013]: 'pzem016modbus - Dagverbruik': Received new state 2048.762695
[14:41:57][D][sensor:092]: 'pzem016modbus - Dagverbruik': Sending state 2.04876 kWh with 3 decimals of accuracy
[14:41:57][V][sensor:013]: 'pzem016modbus Wh': Received new state 6684672.000000
[14:41:57][D][sensor:092]: 'pzem016modbus Wh': Sending state 6684672.00000 Wh with 0 decimals of accuracy
[14:41:57][V][sensor:013]: 'pzem016modbus Hz': Received new state 90.000000
[14:41:57][D][sensor:092]: 'pzem016modbus Hz': Sending state 90.00000 hz with 1 decimals of accuracy
[14:41:57][V][sensor:013]: 'pzem016modbus Pf': Received new state 0.000000
[14:41:57][D][sensor:092]: 'pzem016modbus Pf': Sending state 0.00000  with 2 decimals of accuracy
[14:41:58][D][pzemac:049]: PZEM AC: V=0.1 V, I=8650.752 A, P=219.8 W, E=6684672.0 Wh, F=90.5 Hz, PF=0.00
[14:41:58][V][sensor:013]: 'pzem016modbus V': Received new state 0.100000
[14:41:58][D][sensor:092]: 'pzem016modbus V': Sending state 0.10000 V with 1 decimals of accuracy
[14:41:58][V][sensor:013]: 'pzem016modbus A': Received new state 8650.751953
[14:41:58][D][sensor:092]: 'pzem016modbus A': Sending state 8650.75195 A with 3 decimals of accuracy
[14:41:58][V][sensor:013]: 'pzem016modbus W': Received new state 219.800003
[14:41:58][D][sensor:092]: 'pzem016modbus W': Sending state 219.80000 W with 1 decimals of accuracy
[14:41:58][V][sensor:013]: 'pzem016modbus - Dagverbruik': Received new state 2048.823730
[14:41:58][D][sensor:092]: 'pzem016modbus - Dagverbruik': Sending state 2.04882 kWh with 3 decimals of accuracy
[14:41:58][V][sensor:013]: 'pzem016modbus Wh': Received new state 6684672.000000
[14:41:58][D][sensor:092]: 'pzem016modbus Wh': Sending state 6684672.00000 Wh with 0 decimals of accuracy
[14:41:58][V][sensor:013]: 'pzem016modbus Hz': Received new state 90.500000
[14:41:58][D][sensor:092]: 'pzem016modbus Hz': Sending state 90.50000 hz with 1 decimals of accuracy
[14:41:58][V][sensor:013]: 'pzem016modbus Pf': Received new state 0.000000
[14:41:58][D][sensor:092]: 'pzem016modbus Pf': Sending state 0.00000  with 2 decimals of accuracy
[14:41:59][D][pzemac:049]: PZEM AC: V=0.1 V, I=8781.824 A, P=220.5 W, E=6750208.0 Wh, F=93.6 Hz, PF=0.00
[14:41:59][V][sensor:013]: 'pzem016modbus V': Received new state 0.100000
[14:41:59][D][sensor:092]: 'pzem016modbus V': Sending state 0.10000 V with 1 decimals of accuracy
[14:41:59][V][sensor:013]: 'pzem016modbus A': Received new state 8781.824219
[14:41:59][D][sensor:092]: 'pzem016modbus A': Sending state 8781.82422 A with 3 decimals of accuracy
[14:41:59][V][sensor:013]: 'pzem016modbus W': Received new state 220.500000
[14:41:59][D][sensor:092]: 'pzem016modbus W': Sending state 220.50000 W with 1 decimals of accuracy

This is the right link to help you:

The text sensor that is created in these instructions will contain what your UART receives, line by line.
The readings are also monitored in the log, so you should see the incoming data immediately. Just adapt the baud rate and it should work. Here is an example output from my log. It is from an ESPHome MCU that runs basically only the above Custom UART code together with the standard initial ESPHome settings:

[08:13:39][D][text_sensor:015]: 'uart_readline': Sending state 'Hello World'

The serial source for that transmission was another ESPHome MCU that sends data every single second using:

uart.write: "Hello World\r\n"

I am stuck in the same situation now, I can send data but can’t read them.

@Jpsy, I used this UART text sensor, it works fine when I send data from another ESP8266/Putty, but it seems that in order to ESP8266 or text sensor get the data it is necessary to append the “\r”.

In my case I can’t see the values updated on the text sensor because my equipment does not send with it CR. Actually, even when I send the command with the CR LF to my equipment, it does not replies.
Then I have to send the raw string to work.

It may be that i said something wrong as I’m a beginner with esphome and uart.

Maybe someone have some experience with this kind of situation that could assist?

Well, that’s actually quite simple. Have a look at the file uart_read_line_sensor.h. In the readline function there is a case statement that looks for CRs and sends all cumulated output to the text sensor when a CR is found:

case '\r': // Return on CR
  rpos = pos;
  pos = 0;  // Reset position index ready for next time
  return rpos;

If your equipment sends some other delimiter, change the code to find that delimiter and then forward the cumulated characters to the text sensor.
You could also change the code to forward each and every single character to the text sensor. But this would probably create an enormous amount of state update events in HA and could stall your system.

2 Likes

Well,
That is true, it is working now :slight_smile:
Lack of knowledge from my part.

Now my first ESPHome project is almost complete.

Thank you very much for the help @Jpsy.

1 Like

Good morning, I recently bought a nextion screen. I have it connected via uart to ESPHOME, I have created for the buttons on the screen a binary sensor. It has sometimes given error and does not work very fine. They’re looking to fix it. But searching I found the “Custom UART Text Sensor” and I think what I need would do better. It is assumed that when I press some button on the screen it sends some code by uart. With that and Node-Red it would be much easier for me to integrate the screen as I want to use it. But I followed the instructions of the “Custom UART Text sensor”, but it doesn’t give me any information. :(, it doesn’t work. I don’t have where to look. Thank you very much, for your time.

There is a nextion component

I know, but I want to use the Custom UART Sensor. Thanks.

Are you saying (because you are not entirely clear) that the esphome nextion binary sensor did not work properly for you?

If so, perhaps you should post an issue on github rather than trying to create another solution?

I have bought a Nextion screen and I find that if you want to have a lot of screens and a lot of buttons, the ESP32 can’t handle them and it locks up. One solution would be to create a text sensor that has the HEX value that sends the screen in text format to Home Assistant. And from there, do the automations and actions with HA or Node-Red. The possibilities would be endless. I do not know if I explained well. The screen every time you press a button sends a HEX code and I would like to capture it with the sensor.

Thank you

Good morning, today the only thing I have managed is to see in the log how it receives the press code from the screen. It does it in several lines and if someone can help me configure the sensor so that in HA it comes out together in a line it would make me happy. :). Codes always end in FF FF FF. In the answer above you can see the program that manages the screen and how it sends the codes and how I would like to see them in the text sensor in Home Assistant.Captura de pantalla 2021-07-16 113755

Read post 6 above, it tells you exactly how.

Forget what I was asking for above. In the next ESPHome update the developer of the Nextion component has done a big update and includes what I needed. Thank you. :grinning: