{"id":765,"date":"2019-07-22T10:29:34","date_gmt":"2019-07-21T23:59:34","guid":{"rendered":"http:\/\/parablog-wordpress.dockerbox.rei.moe\/?p=765"},"modified":"2019-07-22T10:29:34","modified_gmt":"2019-07-21T23:59:34","slug":"controlling-an-rs232-device-over-uart-wifi","status":"publish","type":"post","link":"https:\/\/blog.lewys.eu\/?p=765","title":{"rendered":"Controlling an RS232 Device over UART \/ WiFi"},"content":{"rendered":"\n<p>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.<\/p>\n\n\n\n<p>Naturally I attempted to use the LAN control part first, which involves opening a TCP socket to port 10008 of the device.<br>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.<\/p>\n\n\n\n<p>So I went to JayCar and grabbed one of these:<\/p>\n\n\n\n<p><a href=\"https:\/\/www.aliexpress.com\/item\/32722395554.html\">https:\/\/www.aliexpress.com\/item\/32722395554.html<\/a><\/p>\n\n\n\n<p>Basically, it converts a TTL level signal to RS232 level signals.<br>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.<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>Code wise, this is what ive settled on using and has been working MOSTLY well:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>esphome:\n  name: sharptv\n  platform: ESP8266\n  board: d1_mini\n\nwifi:\n  ssid: '********'\n  password: '********'\n\napi:\n\n# Enable logging\nlogger:\n\nota:\n\nmqtt:\n  broker: 172.16.0.60\n  username: rs232\n  password: ********\n\nuart:\n  baud_rate: 38400\n  tx_pin: D4\n\nswitch:\n  - platform: uart\n    name: \"Power On\"\n    data: [0x50, 0x4F, 0x57, 0x52, 0x20, 0x20, 0x20, 0x31, 0x0D, 0x0A]\n    on_turn_on:\n      then:\n      - mqtt.publish:\n          topic: esphome\/rs232\/sharp\/state\n          payload: \"ON\"\n          retain: true\n\n  - platform: uart\n    name: \"Power Off\"\n    data: [0x50, 0x4F, 0x57, 0x52, 0x20, 0x20, 0x20, 0x30, 0x0D, 0x0A]\n    on_turn_on:\n      then:\n      - mqtt.publish:\n          topic: esphome\/rs232\/sharp\/state\n          payload: \"OFF\"\n          retain: true\n\n  - platform: uart\n    name: \"HDMI 1\"\n    data: [0x49, 0x4E, 0x50, 0x53, 0x20, 0x20, 0x31, 0x30, 0x0D, 0x0A]\n    on_turn_on:\n      then:\n      - mqtt.publish:\n          topic: esphome\/rs232\/sharp\/input\n          payload: \"HDMI1\"\n          retain: true\n\n  - platform: uart\n    name: \"HDMI 2\"\n    data: [0x49, 0x4E, 0x50, 0x53, 0x20, 0x20, 0x31, 0x33, 0x0D, 0x0A]\n    on_turn_on:\n      then:\n      - mqtt.publish:\n          topic: esphome\/rs232\/sharp\/input\n          payload: \"HDMI2\"\n          retain: true\n\n  - platform: uart\n    name: \"HDMI 3\"\n    data: [0x49, 0x4E, 0x50, 0x53, 0x20, 0x20, 0x31, 0x38, 0x0D, 0x0A]\n    on_turn_on:\n      then:\n      - mqtt.publish:\n          topic: esphome\/rs232\/sharp\/input\n          payload: \"HDMI3\"\n          retain: true\n\n  - platform: uart\n    name: \"DISPLAYPORT\"\n    data: [0x49, 0x4E, 0x50, 0x53, 0x20, 0x20, 0x31, 0x34, 0x0D, 0x0A]\n    on_turn_on:\n      then:\n      - mqtt.publish:\n          topic: esphome\/rs232\/sharp\/input\n          payload: \"DISPLAYPORT\" \n          retain: true\n<\/code><\/pre>\n\n\n\n<p>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. \ud83d\ude42<\/p>\n\n\n\n<p>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 \u2026 havent got around to it\u2026&nbsp;<\/p>\n\n\n\n<p>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 :^)<\/p>\n\n\n\n<p>Ive ordered a handful of these to test making it smaller (think a cable with a bulge in the middle)<br> <br><a href=\"https:\/\/www.aliexpress.com\/item\/32834977750.html\">https:\/\/www.aliexpress.com\/item\/32834977750.html<\/a> <br><\/p>\n\n\n\n<p><br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"https:\/\/blog.lewys.eu\/?p=765\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Controlling an RS232 Device over UART \/ WiFi<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21,23,41,19,27,18,25],"tags":[],"class_list":["post-765","post","type-post","status-publish","format-standard","hentry","category-educational","category-esp8266","category-home-assistant","category-home-automation-2","category-programming","category-projects","category-wireless"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/blog.lewys.eu\/index.php?rest_route=\/wp\/v2\/posts\/765","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.lewys.eu\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.lewys.eu\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.lewys.eu\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.lewys.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=765"}],"version-history":[{"count":1,"href":"https:\/\/blog.lewys.eu\/index.php?rest_route=\/wp\/v2\/posts\/765\/revisions"}],"predecessor-version":[{"id":766,"href":"https:\/\/blog.lewys.eu\/index.php?rest_route=\/wp\/v2\/posts\/765\/revisions\/766"}],"wp:attachment":[{"href":"https:\/\/blog.lewys.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=765"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.lewys.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=765"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.lewys.eu\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=765"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}