LG TV via LAN
LG TV via LAN
Hi, it's easy to Control your TV (LG) with the app for Android so I think for someone that have knowledge it would not be to hard to write a plugin for EG that makes it possible to Control the TV via LAN or am I wrong?
Re: LG TV via LAN
Hi, thank you but that plugin controls the TV via serial contact and I wanted to control it via LAN, no direct contact to the TV.
Re: LG TV via LAN
My HDHomeRun Duo unit suddenly started to show up as one of the devices that can be connected to.
However the TV reboots when I select it as a source.
However the TV reboots when I select it as a source.
Re: LG TV via LAN
No, the dual cannot handle dlna. There will be a new tuner later this year that can do dlna and transcode to h.264 giving access to more devices as you can read in the connect section.
Re: LG TV via LAN
Hi,
I normally don't post requests, I prefer to surf and research until I find the answer however I only get about an hour a day of play time on my pc and have been looking for a way to control my lg tv over lan for about two weeks now! I am like a dog with a bone.
I, like the post author would love to be able to control my LGTV over lan and I know it is possible as i have had a little success with a python script I found. ( after first installing python and running the script it worked first time however an hour later for no obvious reason, running the script would not show the interface)
The code for this script is here.....
https://github.com/ubaransel/lgcommander
Also someone has adapted this script to run headless on a raspberry Pi here......
http://harizanov.com/2013/12/control-lg ... pberry-pi/
You can also install a Google chrome extension for lg tv control from here......
https://chrome.google.com/webstore/deta ... hpia?hl=en
I am afraid I have very little programming experience and I am limited to Google searches, trial and error so I am hoping someone here with a little more know how maybe able to work something out or maybe point me in the right direction as I dont know what to try next
Cheers
Mark
I normally don't post requests, I prefer to surf and research until I find the answer however I only get about an hour a day of play time on my pc and have been looking for a way to control my lg tv over lan for about two weeks now! I am like a dog with a bone.
I, like the post author would love to be able to control my LGTV over lan and I know it is possible as i have had a little success with a python script I found. ( after first installing python and running the script it worked first time however an hour later for no obvious reason, running the script would not show the interface)
The code for this script is here.....
https://github.com/ubaransel/lgcommander
Also someone has adapted this script to run headless on a raspberry Pi here......
http://harizanov.com/2013/12/control-lg ... pberry-pi/
You can also install a Google chrome extension for lg tv control from here......
https://chrome.google.com/webstore/deta ... hpia?hl=en
I am afraid I have very little programming experience and I am limited to Google searches, trial and error so I am hoping someone here with a little more know how maybe able to work something out or maybe point me in the right direction as I dont know what to try next
Cheers
Mark
Re: LG TV via LAN
Hmm, I just tried to rewrite that script for python 2, but I can't test it as my TV doesn't seem to support remote control.
Anyway, that's the code:
Just paste this in a phython script action and change the line 'command="26"' to what ever you like to do (like descried in that blog)
Anyway, that's the code:
Code: Select all
import httplib
import xml.etree.ElementTree as etree
import socket
import re
lgtv = {}
dialogMsg =""
headers = {"Content-Type": "application/atom+xml"}
lgtv["pairingKey"] = "939781"
#lgtv["ipaddress"] = "192.168.0.5"
lgtv["port"] = 8080
command="26"
def getip():
strngtoXmit = 'M-SEARCH * HTTP/1.1' + '\r\n' + \
'HOST: 239.255.255.250:1900' + '\r\n' + \
'MAN: "ssdp:discover"' + '\r\n' + \
'MX: 2' + '\r\n' + \
'ST: urn:schemas-upnp-org:device:MediaRenderer:1' + '\r\n' + '\r\n'
bytestoXmit = strngtoXmit.encode()
sock = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
sock.settimeout(3)
found = False
gotstr = 'notyet'
i = 0
ipaddress = None
sock.sendto( bytestoXmit, ('239.255.255.250', 1900 ) )
while not found and i <= 5 and gotstr == 'notyet':
try:
gotbytes, addressport = sock.recvfrom(512)
gotstr = gotbytes.decode()
except:
i += 1
sock.sendto( bytestoXmit, ( '239.255.255.250', 1900 ) )
if re.search('LG', gotstr):
ipaddress, _ = addressport
found = True
else:
gotstr = 'notyet'
i += 1
sock.close()
if not found :
print "Lg TV not found"
return ipaddress
def displayKey():
conn = httplib.HTTPConnection( lgtv["ipaddress"], port=lgtv["port"])
reqKey = "<!--?xml version=\"1.0\" encoding=\"utf-8\"?--><auth><type>AuthKeyReq</type></auth>"
conn.request("POST", "/roap/api/auth", reqKey, headers=headers)
httpResponse = conn.getresponse()
if httpResponse.reason != "OK" :
print "LG TV: Network error"
return httpResponse.reason
def getSessionid():
conn = httplib.HTTPConnection( lgtv["ipaddress"], port=lgtv["port"])
pairCmd = "<!--?xml version=\"1.0\" encoding=\"utf-8\"?--><auth><type>AuthReq</type><value>" + lgtv["pairingKey"] + "</value></auth>"
conn.request("POST", "/roap/api/auth", pairCmd, headers=headers)
httpResponse = conn.getresponse()
if httpResponse.reason != "OK" :
return httpResponse.reason
tree = etree.XML(httpResponse.read())
return tree.find('session').text
def getPairingKey():
displayKey()
def handleCommand(cmdcode):
conn = httplib.HTTPConnection( lgtv["ipaddress"], port=lgtv["port"])
cmdText = "<!--?xml version=\"1.0\" encoding=\"utf-8\"?--><command></command><name>HandleKeyInput</name><value>"+cmdcode+"</value>"
conn.request("POST", "/roap/api/command", cmdText, headers=headers)
httpResponse = conn.getresponse()
#main()
lgtv["ipaddress"] = getip()
print "LG TV IP: "+lgtv["ipaddress"]
theSessionid = getSessionid()
while theSessionid == "Unauthorized" :
getPairingKey()
theSessionid = getSessionid()
if len(theSessionid) < 8 :
print "LG TV: Could not get Session Id: " + theSessionid
lgtv["session"] = theSessionid
result = command
handleCommand(result)
Last edited by Sem;colon on Tue Feb 03, 2015 1:08 pm, edited 1 time in total.
Re: LG TV via LAN
Hi Thanks for taking a look at the script. I added it to a python script in eg and ran it which resulted in the following
Python Script
Lg TV not found
Traceback (most recent call last):
Python script "33", line 73, in <module>
print "LG TV IP: "+lgtv["ipaddress"]
TypeError: cannot concatenate 'str' and 'NoneType' objects
I also tried it after I changed the pairing key to the one for my tv but it results in the same issue and again I tried it after uncommenting line 10 and replacing the ip address with the one for the tv and again the same error
There is also some info here for loxone not sure if it will help or not....
http://forum.loxone.com/enen/software/4 ... ntrol.html
Mark
Python Script
Lg TV not found
Traceback (most recent call last):
Python script "33", line 73, in <module>
print "LG TV IP: "+lgtv["ipaddress"]
TypeError: cannot concatenate 'str' and 'NoneType' objects
I also tried it after I changed the pairing key to the one for my tv but it results in the same issue and again I tried it after uncommenting line 10 and replacing the ip address with the one for the tv and again the same error
There is also some info here for loxone not sure if it will help or not....
http://forum.loxone.com/enen/software/4 ... ntrol.html
Mark
Re: LG TV via LAN
looks like the script couldn't find your TV, but that doesn't matter.
Change the line:
#lgtv["ipaddress"] = "192.168.0.5"
to:
lgtv["ipaddress"] = "192.168.0.5"
and replace 192.168.0.5 with the IP address of your TV
and change:
lgtv["ipaddress"] = getip()
to:
#lgtv["ipaddress"] = getip()
Change the line:
#lgtv["ipaddress"] = "192.168.0.5"
to:
lgtv["ipaddress"] = "192.168.0.5"
and replace 192.168.0.5 with the IP address of your TV

and change:
lgtv["ipaddress"] = getip()
to:
#lgtv["ipaddress"] = getip()
Re: LG TV via LAN
Cheers again. I will give that a try when I get home from work.
Mark
Mark
Re: LG TV via LAN
Hi
Ok so after making those changes the script runs without any errors in the log window or any other info other than Python Script
LG TV IP: 192.168.0.107
Nothing happens on the TV However if I change the pairing key on line 9 to something other than the correct one and run the script the TV displays the pairing key. So we know the script is connecting to the TV. After getting the pairing key displayed on the tv If I change line 9 back to the correct key and run the script again eventghost crashes and the tv just stays the same, displaying the key. I assume this is because the tv is waiting for just the pairing command but when I rerun the script I am sending the whole thing again.
Is there away to Print the result of getsessionid? so we can see if an authorized session is being made?
Mark
Ok so after making those changes the script runs without any errors in the log window or any other info other than Python Script
LG TV IP: 192.168.0.107
Nothing happens on the TV However if I change the pairing key on line 9 to something other than the correct one and run the script the TV displays the pairing key. So we know the script is connecting to the TV. After getting the pairing key displayed on the tv If I change line 9 back to the correct key and run the script again eventghost crashes and the tv just stays the same, displaying the key. I assume this is because the tv is waiting for just the pairing command but when I rerun the script I am sending the whole thing again.
Is there away to Print the result of getsessionid? so we can see if an authorized session is being made?
Mark
Re: LG TV via LAN
Quick update... After looking at your line print LG TV IP I added after line 80
print "LG TV Session ID: "+ theSessionid
and now when i test the script I get the following log.....
Python Script
LG TV IP: 192.168.0.107
LG TV Session ID: 923650698
so eg is connecting and getting a valid session ID
Mark
print "LG TV Session ID: "+ theSessionid
and now when i test the script I get the following log.....
Python Script
LG TV IP: 192.168.0.107
LG TV Session ID: 923650698
so eg is connecting and getting a valid session ID
Mark
Re: LG TV via LAN
Yes, of corse, if the session id is not valid it shows the paring key on the TV.
If "nothing" happens, it's fine.
You just need to change the "command" to what you whant to do / what your TV supports
If "nothing" happens, it's fine.
You just need to change the "command" to what you whant to do / what your TV supports
Re: LG TV via LAN
Yes thats the problem, I have changed the command from 26 (mute) to 400 (3D) 24 and 25 etc but nothing is happening on the tv. I know these commands work because I have tried them in the LGCommander python3 script.You just need to change the "command" to what you whant to do / what your TV supports
Mark
Re: LG TV via LAN
Sry, the script was incomplete at this point - looks like something wnet wrong while I copied it.
I corrected it in my earlier post.
I corrected it in my earlier post.