UART IR Receiver/transmitter for TV

I have a YS-IRTM ir receiver/transmitter board. I want to use it as a IR blaster with esp32 and control a tv. I have tried a lot of different things but for some reason, I just cannot read the data from the serial interface.

I want to read the IR code from the remote then I was thinking of creating an UART switch to send commands.

esphome:
  name: test_esp32s_nodemcu
  platform: ESP32
  board: nodemcu-32s
  includes:
    - my_custom_component.h

wifi:
  ssid: !secret esp_wifi_ssid
  password: !secret esp_wifi_pass

  ap:
    ssid: "Test Esp32S Nodemcu"
    password: !secret esp_ap_password

captive_portal:

logger:

api:
  password: !secret esp_ota_password

ota:
  password: !secret esp_ota_password

time:
  - platform: homeassistant
    id: homeassistant_time

uart:
  id: ir
  tx_pin: GPIO26
  rx_pin: GPIO16
  baud_rate: 9600

custom_component:
- lambda: |-
    auto my_custom = new MyCustomComponent(id(ir));
    return {my_custom};

my_custom_component.h

#include "esphome.h"

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

  void setup() override {
    // nothing to do here
  }
  void loop() override {
    // Use Arduino API to read data, for example
    while (available() >= 1) {
      String line = Serial.readString();
    };
  };
};

I don’t have any knowledge regarding C++ so this looks like an uphill battle for me.
Can anyone help out?

I know you are asking for a serial interface, but if you are not interested in going down the programming rabbit hole, I would just add MQTT support to the esp device and interact with it that way. And I am a big fan of one less cable in my life :wink:

I know you already have some hardware, but I just flashed one of these very interesting devices with Tasmota, esphome support as well. It has 5 IR LED around its perimeter. And while I had to solder 5 wires to it to get the Tasmota image on it. It only took 30 minutes and the newer unit I got does not even have screws, just a press fit case that opened easily. I was sending IR commands to my home entertainment devices in under an hour with MQTT and Home Assistant. Good hunting!

There are numerous sellers of this devices with nothing other different name or perhaps different case:

My plan is really simple. My TV has a USB interface. Connecting a USB interface with a pulldown resistor to binary sensor of the esp will give me the on/off status of the TV. The esp device with an enclosure is mounted to a wall next to TV with a LED screen and dht22 on it. I was using this as a room temperature device. I had an idea to make the dumb TV smart. Using IR and USB interface seemed doable to me but oh boy. IR is giving me lots of trouble.
I have already all the hardware I need but I just need the get the programming right.

I found some examples of doing exactly that but not with esphome. If I can get UART working with ESP, I think I can do it.

I too when down this rabbit hole of trying to control my media devices. The IR is a real pain, I agree! I first went the raspberry pi route and after I gave up on LIRC I was able to get something working, but then I got enamored with the whole esp thing and build my own esp32 ir device, that is working pretty well. But of course as I was going down that route, I found these off the shelf esp8266 devices, I bought it just for kicks, but other than the esp programming learning I got, I would have saved a lot of time by using the off the shelf esp8266 IR device. Like you, I have semi smart TV or semi dumb TV, not sure which. When it is off the ONLY way to turn it on is via IR, but when it is on it has all kinds of USB serial interface and network controls…

I hope you able to get a software solution figured with too much frustration. It is pretty amazing what you can do with this little 5 USD chips! Good luck!!

I looked briefly at the UART section of esphome, it seems pretty straight forward. I would start by putting a serial to USB between your UART port on your esp8266 and your computer. Get a simple serial terminal running on your PC (bootterm is a nice command line tool) and try to write some code to send characters back and forth between your esp and your computer. I’m not quite clear on what your goal is with the serial interface, it sounds like you are just trying to use it as a binary input to tell if you TV is on or off.

I have YS-IRTM IR receiver and transmitter module that communicates with UART.

So esp32 needs to communicate with this module using UART to send and receive IR data.

I brought out my arduino and successfully sent and received data but I just cannot do it with Esphome for some reason.
The arduino code to receive data and send data is below:
These work
Receive data:

int incomingByte = 0;   // for incoming serial data

void setup() {
        Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
}

void loop() {

        // send data only when you receive data:
        if (Serial.available() > 0) {
                // read the incoming byte:
                incomingByte = Serial.read();

                // say what you got:
                Serial.print("I received: ");
                Serial.println(incomingByte, HEX);
        }
}

Send data:


uint8_t my_serial_bytes[5]={0xA1, 0xF1, 0x04, 0xFB, 0x8};

void setup() {
        Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
}

void loop() {
  
  
Serial.write(my_serial_bytes,sizeof(my_serial_bytes));


delay(5000);        // delay 5sec
                
        
}

With these codes I successfully received data from my remote and sent it to TV using an Arduino Uno with pins 1 and 2.

My mistake, sorry. I did not understand that this IR module interfaced to the ESP or Arduino via RS-232 serial. Looks like you are in pretty uncharted territory with this device. I found this example with ESP32 and MicroPython, but that is a whole different direction. But I did note in this post recommends level converters between the ESP and the IR unit, be careful to not overtax your ESP GPIO pins:

Again, hate to steer you in a different hardware direction, however most of the IR libraries for the ESP’s seem to be designed for a simple IR LED attached to one GPIO in and the receiver IR device attached to another. For 5 bucks you can get the components you need to do this circuit with ESP32 and these libraries, even though the first amazon listing below says 5 volts, it probably works at 3.3, but the second listing says it is 3.3 compatible:

https://tasmota.github.io/docs/IR-Remote/

Okay, another thing I did was to skip reading data with my esp32.
I connected this device to arduino and recorded my tv remote codes.
Then, I connected this to my esp32 again, created an UART switch using the codes I previously recorded with Arduino.

Like this:

uart:
  id: ir
  tx_pin: GPIO26
  rx_pin: GPIO16
  baud_rate: 9600

switch:
  - platform: uart
    name: "Power uart"
    data: [0xA1, 0xF1, 0x04, 0xFB, 0x08]
  - platform: uart
    name: "chn 1"
    data: [0xA1, 0xF1, 0x04, 0xFB, 0x11]
  - platform: uart
    name: "chn up"
    data: [0xA1, 0xF1, 0x04, 0xFB, 0x00]
  - platform: uart
    name: "chn down"
    data: [0xA1, 0xF1, 0x04, 0xFB, 0x01]
  - platform: uart
    name: "vol up"
    data: [0xA1, 0xF1, 0x04, 0xFB, 0x02]
  - platform: uart
    name: "vol down"
    data: [0xA1, 0xF1, 0x04, 0xFB, 0x03]

The switches showed up on Home Assistant but they do not work reliably. I have to spam the switches for it to work. Using Arduino, it works immediately. I am stuck here.

I do have this device
https://wiki.keyestudio.com/Ks0027_keyestudio_Digital_IR_Transmitter_Module

But I couldn’t get it to work. ):

That seems to be the type of device that espHome, Tasmota and general Arduino/ESP libraries are designed to work with. When you said it does not work, what do you mean? As I said, earlier in this thread IR is a pain in the butt. Seems like I spent most of my time with making sure the line of sight was good.

Exactly,
the line of sight etc does prevent this device from working realiably.
The other uart ir transmitter works very reliably. That is why I am trying to make this work.

Sound like there is not as much power getting to the IR LED on the ESP as with the Arduino, which makes senses as the ESP is only 3.3v and if your Arduino is 5 v, probably a brighter light. Hope you can find some code that works with that device, the ESP and esphome. However, as I said, sounds like not many have gone with this IR device you have. Good hunting!

Hmm, you might be right. I will try to connect the arduino to keyestudio transmitter and see if it will be better. Maybe the current draw from the esp pin is not enough.

Nope, the keyestudio ir transmitter is unreliable on both Arduino and ESP. It needs to be close to TV with direct line of sight. The YS-IRTM uart transmitter, works perfectly even from a distance of 10m but I just cannot figure out how to integrate this with a custom UART to esphome. Any help is appreciated at this point.
This is the log output:

[01:56:27][C][wifi:307]:   Hostname: 'test_esp32s_nodemcu'
[01:56:27][C][wifi:311]:   Signal strength: -61 dB ▂▄▆█
[01:56:27][C][wifi:315]:   Channel: 11
[01:56:27][C][wifi:316]:   Subnet: 255.255.255.0
[01:56:27][C][wifi:317]:   Gateway: 192.168.1.1
[01:56:27][C][wifi:318]:   DNS1: 192.168.1.45
[01:56:27][C][wifi:319]:   DNS2: 192.168.1.1
[01:56:28][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:56:29][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:56:29][C][uart_esp32:088]: UART Bus:
[01:56:29][C][uart_esp32:090]:   TX Pin: GPIO26
[01:56:29][C][uart_esp32:093]:   RX Pin: GPIO16
[01:56:29][C][uart_esp32:094]:   RX Buffer Size: 256
[01:56:29][C][uart_esp32:096]:   Baud Rate: 9600 baud
[01:56:29][C][uart_esp32:097]:   Data Bits: 8
[01:56:29][C][uart_esp32:098]:   Parity: NONE
[01:56:29][C][uart_esp32:099]:   Stop bits: 1
[01:56:30][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:56:31][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:56:31][C][logger:185]: Logger:
[01:56:31][C][logger:186]:   Level: DEBUG
[01:56:31][C][logger:187]:   Log Baud Rate: 115200
[01:56:31][C][logger:188]:   Hardware UART: UART0
[01:56:32][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:56:33][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:56:33][C][homeassistant.time:010]: Home Assistant Time:
[01:56:33][C][homeassistant.time:011]:   Timezone: 'TZ-3'
[01:56:34][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:56:35][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:56:35][D][api:067]: Disconnecting Home Assistant 2021.4.6 (192.168.1.45)
[01:56:36][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:56:37][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:56:37][D][api.connection:604]: Client 'Home Assistant 2021.4.6 (192.168.1.45)' connected successfully!
[01:56:38][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:56:39][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:56:39][D][time:040]: Synchronized time: Tue Apr 27 01:56:37 2021
[01:56:39][C][captive_portal:169]: Captive Portal:
[01:56:40][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:56:41][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:56:41][C][ota:029]: Over-The-Air Updates:
[01:56:41][C][ota:030]:   Address: test_esp32s_nodemcu.local:3232
[01:56:41][C][ota:032]:   Using Password.
[01:56:42][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:56:43][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:56:43][C][api:095]: API Server:
[01:56:43][C][api:096]:   Address: test_esp32s_nodemcu.local:6053
[01:56:44][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:56:45][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:56:46][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:56:47][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:56:47][D][api:067]: Disconnecting Home Assistant 2021.4.6 (192.168.1.45)
[01:56:48][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:56:49][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:56:49][D][api.connection:604]: Client 'Home Assistant 2021.4.6 (192.168.1.45)' connected successfully!
[01:56:50][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:56:51][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:56:51][D][time:040]: Synchronized time: Tue Apr 27 01:56:49 2021
[01:56:52][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:56:53][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:56:54][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:56:55][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:56:56][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:56:57][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:56:58][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:56:59][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:56:59][D][api:067]: Disconnecting Home Assistant 2021.4.6 (192.168.1.45)
[01:57:00][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:01][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:01][D][api.connection:604]: Client 'Home Assistant 2021.4.6 (192.168.1.45)' connected successfully!
[01:57:02][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:03][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:03][D][time:040]: Synchronized time: Tue Apr 27 01:57:01 2021
[01:57:04][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:05][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:06][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:07][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:08][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:09][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:10][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:11][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:11][D][api:067]: Disconnecting Home Assistant 2021.4.6 (192.168.1.45)
[01:57:12][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:13][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:13][D][api.connection:604]: Client 'Home Assistant 2021.4.6 (192.168.1.45)' connected successfully!
[01:57:14][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:15][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:15][D][time:040]: Synchronized time: Tue Apr 27 01:57:13 2021
[01:57:16][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:17][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:18][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:19][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:20][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:21][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:22][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:23][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:23][D][api:067]: Disconnecting Home Assistant 2021.4.6 (192.168.1.45)
[01:57:24][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:25][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:25][D][api.connection:604]: Client 'Home Assistant 2021.4.6 (192.168.1.45)' connected successfully!
[01:57:26][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:27][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:27][D][time:040]: Synchronized time: Tue Apr 27 01:57:25 2021
[01:57:28][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:30][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:31][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:32][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:33][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:34][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:35][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:36][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:36][D][api:067]: Disconnecting Home Assistant 2021.4.6 (192.168.1.45)
[01:57:37][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:38][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:38][D][api.connection:604]: Client 'Home Assistant 2021.4.6 (192.168.1.45)' connected successfully!
[01:57:39][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:40][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:40][D][time:040]: Synchronized time: Tue Apr 27 01:57:38 2021
[01:57:41][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:42][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:43][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:44][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:45][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:46][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:47][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:48][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:48][D][api:067]: Disconnecting Home Assistant 2021.4.6 (192.168.1.45)
[01:57:49][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:50][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:50][D][api.connection:604]: Client 'Home Assistant 2021.4.6 (192.168.1.45)' connected successfully!
[01:57:51][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:52][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:52][D][time:040]: Synchronized time: Tue Apr 27 01:57:50 2021
[01:57:53][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:54][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:55][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:56][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:57][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:58][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:57:59][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:58:00][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:58:00][D][api:067]: Disconnecting Home Assistant 2021.4.6 (192.168.1.45)
[01:58:01][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:58:02][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:58:02][D][api.connection:604]: Client 'Home Assistant 2021.4.6 (192.168.1.45)' connected successfully!
[01:58:03][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:58:04][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:58:04][D][time:040]: Synchronized time: Tue Apr 27 01:58:02 2021
[01:58:05][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:58:06][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:58:07][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:58:08][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:58:09][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:58:10][E][uart_esp32:147]: Reading from UART timed out at byte 0!
[01:58:11][E][uart_esp32:147]: Reading from UART timed out at byte 0!

Out of my wheelhouse as how to get this YS-IRTM talking to the ESP32. Hope you find a solution. I put a lot of effort into getting my little ESP32 GPIO circuit to turn on via IR my LG TV, as I said IR is pain. But, it is nice to be able to say to Google Home 'Turn on the LG TV screen."

Good hunting!

After struggling a lot, I decided to ditch the UART and stick the keyestudio transmitter to the TV. It looks a little bit ugly but it works. If anyone has an idea or suggestion, I can get back to this.