Controlling an RS232 Device over UART / WiFi

I recently had the need to connect part of my AV setup to my Home Assistant instance, however to do so I had two options, using the LAN control option built into the device, or via an RS232 serial port.

Naturally I attempted to use the LAN control part first, which involves opening a TCP socket to port 10008 of the device.
But I ran into problems as the connection kept wanting a user to login, even though there was no user account, and I was unable to figure out how to pass the login prompt and send commands automatically.

So I went to JayCar and grabbed one of these:

https://www.aliexpress.com/item/32722395554.html

Basically, it converts a TTL level signal to RS232 level signals.
I hooked it up to a Wemos D1 Mini, on the ESP, you want to use one of the HARDWARE UART pins, so for me, I went with D4, which is GPIO2 / TXD1.

My equipment had a 3.5mm socket for RS232 control, the manual had a pinout for DB9 to 3.5 so that was a simple cable to make, but your equipment might have something else.

Code wise, this is what ive settled on using and has been working MOSTLY well:

esphome:
  name: sharptv
  platform: ESP8266
  board: d1_mini

wifi:
  ssid: '********'
  password: '********'

api:

# Enable logging
logger:

ota:

mqtt:
  broker: 172.16.0.60
  username: rs232
  password: ********

uart:
  baud_rate: 38400
  tx_pin: D4

switch:
  - platform: uart
    name: "Power On"
    data: [0x50, 0x4F, 0x57, 0x52, 0x20, 0x20, 0x20, 0x31, 0x0D, 0x0A]
    on_turn_on:
      then:
      - mqtt.publish:
          topic: esphome/rs232/sharp/state
          payload: "ON"
          retain: true

  - platform: uart
    name: "Power Off"
    data: [0x50, 0x4F, 0x57, 0x52, 0x20, 0x20, 0x20, 0x30, 0x0D, 0x0A]
    on_turn_on:
      then:
      - mqtt.publish:
          topic: esphome/rs232/sharp/state
          payload: "OFF"
          retain: true

  - platform: uart
    name: "HDMI 1"
    data: [0x49, 0x4E, 0x50, 0x53, 0x20, 0x20, 0x31, 0x30, 0x0D, 0x0A]
    on_turn_on:
      then:
      - mqtt.publish:
          topic: esphome/rs232/sharp/input
          payload: "HDMI1"
          retain: true

  - platform: uart
    name: "HDMI 2"
    data: [0x49, 0x4E, 0x50, 0x53, 0x20, 0x20, 0x31, 0x33, 0x0D, 0x0A]
    on_turn_on:
      then:
      - mqtt.publish:
          topic: esphome/rs232/sharp/input
          payload: "HDMI2"
          retain: true

  - platform: uart
    name: "HDMI 3"
    data: [0x49, 0x4E, 0x50, 0x53, 0x20, 0x20, 0x31, 0x38, 0x0D, 0x0A]
    on_turn_on:
      then:
      - mqtt.publish:
          topic: esphome/rs232/sharp/input
          payload: "HDMI3"
          retain: true

  - platform: uart
    name: "DISPLAYPORT"
    data: [0x49, 0x4E, 0x50, 0x53, 0x20, 0x20, 0x31, 0x34, 0x0D, 0x0A]
    on_turn_on:
      then:
      - mqtt.publish:
          topic: esphome/rs232/sharp/input
          payload: "DISPLAYPORT" 
          retain: true

I say mostly, because when the ESP reboots, it sticks some data out of the pin, which the equipment holds in its buffer. So if the ESP has just reboot, and I try and send a command, the unit wont respond, as it gets more data than it thought. 🙂

This can be fixed by including a line break and carriage return at the START of the command, to clear the buffer, or by sending the command twice. but i havent done that yet because i … havent got around to it… 

Ill also mention, the TX/RX might be wrong on the Chinese board because ive seen a few different photos, if it doesnt work on TX try RX :^)

Ive ordered a handful of these to test making it smaller (think a cable with a bulge in the middle)

https://www.aliexpress.com/item/32834977750.html