LG TV via LAN

Got a good idea? You can suggest new features here.

LG TV via LAN

Postby PontusBC » Wed Apr 03, 2013 1:39 pm

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?
PontusBC
 
Posts: 3
Joined: Mon Mar 25, 2013 5:46 pm

Re: LG TV via LAN

Postby jonib » Wed Apr 03, 2013 3:42 pm

Here seems to be a plugin for LG TV. Don't know how useful it is.

jonib
XBMC2 plugin to control XBMC. If you want to flatter me Image
jonib
Plugin Developer
 
Posts: 1195
Joined: Thu Mar 26, 2009 9:33 pm
Location: Sweden

Re: LG TV via LAN

Postby PontusBC » Wed Apr 03, 2013 4:36 pm

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.
PontusBC
 
Posts: 3
Joined: Mon Mar 25, 2013 5:46 pm

Re: LG TV via LAN

Postby shekhul » Tue May 21, 2013 7:57 am

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.
shekhul
 
Posts: 1
Joined: Tue May 21, 2013 7:25 am

Re: LG TV via LAN

Postby cristalzs » Thu May 30, 2013 10:44 am

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.
cristalzs
 
Posts: 1
Joined: Thu May 30, 2013 10:34 am

Re: LG TV via LAN

Postby mrdavisma » Sat Jan 31, 2015 9:34 pm

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-smart-tv-over-the-internet-using-a-raspberry-pi/

You can also install a Google chrome extension for lg tv control from here......

https://chrome.google.com/webstore/detail/lg-tv-remote-controller/icokofncdmhjjncknidajbngmbfphpia?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
mrdavisma
 
Posts: 11
Joined: Sat Jan 31, 2015 9:10 pm

Re: LG TV via LAN

Postby Sem;colon » Sun Feb 01, 2015 10:40 am

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:
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)

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)
Last edited by Sem;colon on Tue Feb 03, 2015 1:08 pm, edited 1 time in total.
Sem;colon
Experienced User
 
Posts: 559
Joined: Sat Feb 18, 2012 10:51 am
Location: Germany

Re: LG TV via LAN

Postby mrdavisma » Sun Feb 01, 2015 2:36 pm

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/4876-lg-tv-http-control.html

Mark
mrdavisma
 
Posts: 11
Joined: Sat Jan 31, 2015 9:10 pm

Re: LG TV via LAN

Postby Sem;colon » Sun Feb 01, 2015 5:51 pm

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()
Sem;colon
Experienced User
 
Posts: 559
Joined: Sat Feb 18, 2012 10:51 am
Location: Germany

Re: LG TV via LAN

Postby mrdavisma » Sun Feb 01, 2015 11:51 pm

Cheers again. I will give that a try when I get home from work.
Mark
mrdavisma
 
Posts: 11
Joined: Sat Jan 31, 2015 9:10 pm

Re: LG TV via LAN

Postby mrdavisma » Mon Feb 02, 2015 2:48 pm

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
mrdavisma
 
Posts: 11
Joined: Sat Jan 31, 2015 9:10 pm

Re: LG TV via LAN

Postby mrdavisma » Mon Feb 02, 2015 3:47 pm

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
mrdavisma
 
Posts: 11
Joined: Sat Jan 31, 2015 9:10 pm

Re: LG TV via LAN

Postby Sem;colon » Mon Feb 02, 2015 5:21 pm

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
Sem;colon
Experienced User
 
Posts: 559
Joined: Sat Feb 18, 2012 10:51 am
Location: Germany

Re: LG TV via LAN

Postby mrdavisma » Mon Feb 02, 2015 6:28 pm

You just need to change the "command" to what you whant to do / what your TV supports


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.

Mark
mrdavisma
 
Posts: 11
Joined: Sat Jan 31, 2015 9:10 pm

Re: LG TV via LAN

Postby Sem;colon » Tue Feb 03, 2015 1:10 pm

Sry, the script was incomplete at this point - looks like something wnet wrong while I copied it.
I corrected it in my earlier post.
Sem;colon
Experienced User
 
Posts: 559
Joined: Sat Feb 18, 2012 10:51 am
Location: Germany

Next

Return to Feature Requests

Who is online

Users browsing this forum: No registered users and 3 guests