LG TV serial control - AVS Forum | Home Theater Discussions And Reviews
Forum Jump: 
 1Likes
  • 1 Post By nevans1989
 
Thread Tools
post #1 of 7 Old 02-15-2018, 09:43 PM - Thread Starter
Member
 
Join Date: Feb 2018
Location: Seattle
Posts: 25
Mentioned: 0 Post(s)
Tagged: 0 Thread(s)
Quoted: 14 Post(s)
Liked: 8
LG TV serial control

I wanted to share a little project that I am just finishing up. Hopefully what I have learned can help others. There’s a TL; DR at the end if this is too much.

The LG UF8500 that I own has IP control and a pretty good app, which is great because remotes tend to grow legs of their own in my house. But the app doesn’t support one crucial function: turning the TV on. I originally didn’t think the TV had an RS-232 port, but one day I was reading the spec sheet and saw that it was listed as a “Phone jack”. I decided to rummage through my spare parts bin and see if I could use it. I found a USB to DB-9 adapter and I was half way there. I ordered a 3.5mm phono to DB-9 (and I also had to get a null modem adapter to properly connect them, otherwise the TX and RX pins were misconnected) and waited patiently for the next 2 days until it arrived. Once it showed up, I plugged the USB into my laptop and the 3.5mm end into the TV (the port labeled SVC ONLY).

https://i.imgur.com/s66hHxa.jpg

My tv is on a slim mount on the wall, so it was a little bit difficult to get the 3.5mm plug all the way in. My wife (who is much more slender than I am) provided some valuable assistance in this step. My laptop is running Windows 10 and luckily the USB-serial adapter is truly plug and play. I opened the device manager to find out which COM port was assigned to the adapter (it was COM3 – typical). I opened up putty and configured it based on a little googling. The common “9600 8N1” settings did the trick. Specifically, that means 9600 baud, 8 data bits, no parity bit, and 1 stop bit. In putty, I left “flow control” at the default XON/XOFF option. I hit Open and it worked!


I found the LG command codes here https://www.lg.com/us/commercial/doc...-ba-rs232c.pdf (PDF) (link is now dead as of Dec 2019, try this https://static.bhphotovideo.com/lit_files/332136.pdf). The way to type these codes is in 3 groups, each separated by a space, then followed by carriage return command (this means pressing the Enter key in an interactive terminal). The first group contains the COMMAND1 and COMMAND2 characters. The second is the set ID, and as far as I can tell this is only important if you have multiple sets daisy chained together. I just use 00 which is the “all sets” code. The third group is the value for the command, like on or off for the power command, or the volume level for the volume command. Here is an example for the power on command.
Code:
ka 00 01
The COMMAND1 and COMMAND2 codes for power are 'k' and 'a' respectively. '00' is for all sets, and '01' is the value for on. The command for powering off the set would be
Code:
ka 00 00
This was great, but plugging in a laptop, launching a terminal and typing a command is definitely not an improvement over finding the remote (or pressing the power button on the tv). I decided I should pull my old raspberry pi out of the closet and set it up as a server with a rudimentary TV control API. At this point I realized that I could have used the built in serial connections on the GPIO pins, but I already had the working USB connection, so I just kept using that.

The raspberry pi (3rd gen) that I used had ubuntu mate installed. I had to do a firmware update (sudo rpi-update from the command line) to get the USB-serial adapter to connect to the TV, but once that was done I could connect with minicom set to use /dev/ttyUSB0. I had to change the permissions on /dev/ttyUSB0 to read-write for all (that’s 0666 in linux permission codes) if I wanted any user other than root to be able to connect. Once I ran minicom to get the settings properly configured on the serial adapter, I could just redirect an echo to the device and send commands. So, from a command prompt I could type
Code:
 echo “ka 00 01” > /dev/ttyUSB0
and the command would be sent to the TV. The raspberry pi had apache and PHP installed already, so I decided to put together a quick and dirty PHP script to allow simple on/off and toggle mute control. The crux of the script goes along the lines of:
Code:
  <?
$action_list = (object) array('on'=>'ka 00 01\n', 'off'=>'ka 00 00\n','mute'=>'ke 00 00\n');
$action = $_GET["action"];           // this was sent in the query string as ‘on’, ‘off’, or ‘mute’
$action = $action_list->$action; //pull full command matching request from array, 
                                                                //now $action will be equal to something like “ka 00 01\n” or similar 
  shell_exec("echo \"$action\" > /dev/ttyUSB0");
?>
This was great. I could now create a web shortcut on my android phone to turn on my tv with 1 touch! In the end, this wasn’t enough, and I am now creating an Alexa skill to control power and mute with my voice. I doubt I’ll actually publish this skill though. It does what I need in developer mode for my account only and I don’t see enough people copying my setup as exactly as necessary to make it worth my continued support of the app and efforts in the publication process. As the skill works now, you would have to forward ports on your router and have a valid SSL certificate & (sub)domain on your local network. If this is done wrong, you could end up inviting the bad guys right into your network. But if you really want to know everything, direct message me.


TL; DR

What: I hooked up a Raspberry Pi to my tv to control some basic functions like power on from my phone. This is how you do it.
Why: LG’s TVs can’t be turned on from the network port (and therefore from the app).
How:

Hardware requirements:
1 – Raspberry Pi with network connection (I used the Raspberry Pi 3)
1 –TV with RS-232 connector (I used an LG UF8500 that I purchased in 2015)


Optional (if you don’t use the Raspberry Pi’s built in GPIO serial connectors):
1 – USB to DB-9 serial adapter like this one http://www.amazon.com/gp/product/B00ZHP2NN0
1 – null modem like this one
http://www.amazon.com/gp/product/7564005068
1 – DB-9 to 3.5mm headphone style connector like
http://www.amazon.com/gp/product/7564005068/gp/product/B01M3TIUEF

Instructions:
  • Connect the Raspberry Pi to the TV using the GPIO to serial connection or using the USB-DB9 to null modem to DB9-3.5mm adapters.
  • SSH into the Raspberry Pi and install minicom if it is not already installed (sudo apt-get install minicom for Debian based distros)
  • Determine which tty is assigned to the USB to serial adapter (or figure out which one is for the onboard GPIO adapter). For the USB adapter, run
    Code:
    dmesg | grep ttyUSB0
    and depending on the model of your USB-serial adapter it should say something like “HL-340 USB-Serial adapter is now attached to ttyUSB0”. Now you know which tty to use. If you don’t see anything, unplug the USB adapter and plug it back in, wait a few seconds, then run the command again.
  • Set the permissions on the tty: sudo chmod 666 /dev/ttyUSB0
  • Now run minicom in setup mode to configure the serial port. Type minicom -s and press enter, then use the arrow keys to select Serial Port Setup. In that screen, type A to edit the path to the Serial device. Type in /dev/ttyUSB0 or whatever you got in the previous step. Press enter to finish editing the path then press E to change the baud rate. Press C then Q to use 9600 8N1. Press Enter twice to return to the main configuration screen and choose Save setup as dfl to save it as default. You can now exit minicom.
  • If you want to test, you can send the command to turn your tv on by typing echo “ka 00 01\n” > /dev/ttyUSB0 (or whatever tty your serial device is assigned to). If it doesn’t work you need to do some troubleshooting.
  • Install apache and php if they aren’t already installed sudo apt-get install php libapache2-mod-php apache2
  • Check that you can get the default apache start page by going to your Raspberry Pi’s local IP address from a browser on a computer connected to the same network as the Raspberry Pi. If it doesn’t show up, the Raspberry Pi’s firewall might be blocking it, or the apache service might be stopped. Google it.
  • Your default web directory is probably /var/www/html/ on the Debian based Raspberry Pi. Save the PHP script in this folder. I named it tv.php but I invite you to be more creative in your filenames. Paste the following text into the file and save.
    Code:
    <?
    $action_list = (object) array('on'=>'ka 00 01\n', 'off'=>'ka 00 00\n','mute'=>'ke 00 00\n');
    $action = $_GET["action"];           // this was sent in the query string as ‘on’, ‘off’, or ‘mute’
    $action = $action_list->$action; //pull full command matching request from array,
                                                                    //now $action will be equal to something like “ka 00 01\n” or similar
    
    shell_exec("echo \"$action\" > /dev/ttyUSB0");
    ?>
  • Now access it from a browser on a local computer or phone to control your tv! Be sure to add the command to the query string, so if you Raspberry Pi’s IP address is 192.168.0.20, go to
    Code:
    http://192.168.0.20/tv.php?action=on
    to turn on your tv.


Attached Thumbnails
Click image for larger version

Name:	uf8500-inputs-back-large.jpg
Views:	98
Size:	176.9 KB
ID:	2362066  

Last edited by owenv2; 12-20-2019 at 10:29 AM.
owenv2 is offline  
Sponsored Links
Advertisement
 
post #2 of 7 Old 02-18-2018, 12:57 PM - Thread Starter
Member
 
Join Date: Feb 2018
Location: Seattle
Posts: 25
Mentioned: 0 Post(s)
Tagged: 0 Thread(s)
Quoted: 14 Post(s)
Liked: 8
Here is a more complete list of codes.
Note: the mute command is different in this list than in my first post. This mute command (mc 00 09) seems to be more reliable. The command from the previous post does not work in smart tv apps, like Netflix. It only works when the TV is set to one of the HDMI or legacy inputs.


Power On ka 00 01
Power Off ka 00 00
Volume Up mc 00 02
Volume Down mc 00 03
Toggle Mute mc 00 09
Info (Display) mc 00 aa
Menu mc 00 43
Up mc 00 40
Down mc 00 41
Right mc 00 06
Left mc 00 07
Enter (OK) mc 00 44
Return (Back) mc 00 28
Exit mc 00 5b
Picture Size: Just Scan kc 00 09
owenv2 is offline  
post #3 of 7 Old 02-20-2018, 11:07 PM
Newbie
 
ttt2k's Avatar
 
Join Date: Nov 2011
Posts: 4
Mentioned: 0 Post(s)
Tagged: 0 Thread(s)
Quoted: 0 Post(s)
Liked: 10
That's some awesome work there! Very much appreciated I'm sure for anyone looking to control their LG TV more reliably.
ttt2k is offline  
Sponsored Links
Advertisement
 
post #4 of 7 Old 12-19-2019, 09:04 PM
Newbie
 
Join Date: Dec 2019
Posts: 3
Mentioned: 0 Post(s)
Tagged: 0 Thread(s)
Quoted: 1 Post(s)
Liked: 1
I know your post is a little old at this point, but I'm trying to set up serial control on my new LG B9 TV and am having some trouble and found this post. Hopefully you can provide some insight. I think I've gotten the setup from my laptop to the TV up correctly, the TV accepts commands such as volume up/down and it accepts the power off command, but I can't get the TV to power on with the 'ka 01 01' command (01 is my device id).

I have a USB to rs232 cable from my laptop, a null modem adapter, and an rs232 db9 to 3.5mm mini jack cable/adapter. The commands only seem to work if I don't use the null modem in between the two adapters. So I am partially wondering if the cables I have aren't quite wired the way the TV is expecting, but the tv accepting some of the commands gives me some confidence.

Did you run into any issues with powering on your device over the serial connection? It's my understanding that the serial port should be listening even when the power is off (unlike if the setup was going through the USB port or if it were an IP connection), but I suppose it's possible LG had changed that.
nevans1989 is offline  
post #5 of 7 Old 12-20-2019, 10:40 AM - Thread Starter
Member
 
Join Date: Feb 2018
Location: Seattle
Posts: 25
Mentioned: 0 Post(s)
Tagged: 0 Thread(s)
Quoted: 14 Post(s)
Liked: 8
Quote:
Originally Posted by nevans1989 View Post
I know your post is a little old at this point, but I'm trying to set up serial control on my new LG B9 TV and am having some trouble and found this post. Hopefully you can provide some insight. I think I've gotten the setup from my laptop to the TV up correctly, the TV accepts commands such as volume up/down and it accepts the power off command, but I can't get the TV to power on with the 'ka 01 01' command (01 is my device id).

I have a USB to rs232 cable from my laptop, a null modem adapter, and an rs232 db9 to 3.5mm mini jack cable/adapter. The commands only seem to work if I don't use the null modem in between the two adapters. So I am partially wondering if the cables I have aren't quite wired the way the TV is expecting, but the tv accepting some of the commands gives me some confidence.

Did you run into any issues with powering on your device over the serial connection? It's my understanding that the serial port should be listening even when the power is off (unlike if the setup was going through the USB port or if it were an IP connection), but I suppose it's possible LG had changed that.

If your system is accepting volume and power off commands without the null modem then I'm sure you have it wired correctly. You must have just bought a different usb to rs232 or rs232 to 3.5mm cable than I did. Regarding the power on command, that is the only command I have ever had a problem with. Most commonly if my TV has been turned off for several hours the 1st power on command won't turn on the tv. Since I am using the web/php server to issue the commands, I have it set to send 2 power on commands 20 ms apart and it now always turns the tv on.

Living room: Denon AVR-X4400H, KG3.5 based Klipsch 5.1 speakers with one Rythmik LVX12. LG 65" UF8500, Xbox One. Using cheapo upfiring speakers for atmos now, in-ceiling speakers coming soon(-ish).
Office: Monoprice 25 watt hybrid amp with a pair of Walnut Wharfedale Diamond 220s and occasionally a Velodyne VX-10B.
owenv2 is offline  
post #6 of 7 Old 12-20-2019, 11:11 AM
Newbie
 
Join Date: Dec 2019
Posts: 3
Mentioned: 0 Post(s)
Tagged: 0 Thread(s)
Quoted: 1 Post(s)
Liked: 1
Thanks for the reply! That's what I was thinking too in terms of the cables, if it works for some commands it just be wired correctly. I was sending the command a few times in a row, but I'm doing it all manually through powershell right now, and I don't think I can send commands that quickly! Haha. I'll keep fiddling with it and maybe set up a little program to send the power on command automatically to test it out. I looked through the service menu too but nothing on there stood out.
nevans1989 is offline  
post #7 of 7 Old 12-21-2019, 10:14 AM
Newbie
 
Join Date: Dec 2019
Posts: 3
Mentioned: 0 Post(s)
Tagged: 0 Thread(s)
Quoted: 1 Post(s)
Liked: 1
So I got it working last night, whether to my stupidity or my blind trust of the manual... I was sending commands with the word "Transmission" in the command because that's how the manual has it written out. I had tried earlier without that term but was getting no response because I had the cable set up wrong at that point and never went back to try it again until last night. It is weird that some commands worked with that as a part of the packet, but hey it's working now, couldn't be more stoked to be past that.
owenv2 likes this.
nevans1989 is offline  
Sponsored Links
Advertisement
 
Reply Home Automation

Thread Tools
Show Printable Version Show Printable Version
Email this Page Email this Page


Forum Jump: 

Posting Rules  
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off