April 6, 2012
Samsung TV Remote Control over LAN/WLAN using Python
For quite some time, I have been looking for a method to control my Samsung UE55C8000 TV using Linux scripts. Finally, I found this forum post on SamyGo that provides an example using Perl. As I like to do scripting in Python, so I have translated the script to Python with some minor changes. As you can see in the following Python code, the script contains Key names for all the remote control keys and even some extra. You should be able to easily modify the script to your needs. The script can also be downloaded on my download page.
#! /usr/bin/python # Title: samsungremote.py # Author: Asif Iqbal # Date: 05APR2012 # Info: To send remote control commands to the Samsung tv over LAN # TODO: import socket import base64 import time, datetime #IP Address of TV tvip = "100.0.0.123" #IP Address of TV myip = "100.0.0.112" #Used for the access control/validation, but not after that AFAIK mymac = "00-0c-29-3e-b1-4f" #What the iPhone app reports appstring = "iphone..iapp.samsung" #Might need changing to match your TV type tvappstring = "iphone.UE55C8000.iapp.samsung" #What gets reported when it asks for permission remotename = "Python Samsung Remote" # Function to send keys def sendKey(skey, dataSock, appstring): messagepart3 = chr(0x00) + chr(0x00) + chr(0x00) + chr(len( base64.b64encode(skey))) + chr(0x00) + base64.b64encode(skey); part3 = chr(0x00) + chr(len(appstring)) + chr(0x00) \ + appstring + chr(len(messagepart3)) + chr(0x00) + messagepart3 dataSock.send(part3); # Open Socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((tvip, 55000)) # Key Reference # Normal remote keys #KEY_0 #KEY_1 #KEY_2 #KEY_3 #KEY_4 #KEY_5 #KEY_6 #KEY_7 #KEY_8 #KEY_9 #KEY_UP #KEY_DOWN #KEY_LEFT #KEY_RIGHT #KEY_MENU #KEY_PRECH #KEY_GUIDE #KEY_INFO #KEY_RETURN #KEY_CH_LIST #KEY_EXIT #KEY_ENTER #KEY_SOURCE #KEY_AD #KEY_PLAY #KEY_PAUSE #KEY_MUTE #KEY_PICTURE_SIZE #KEY_VOLUP #KEY_VOLDOWN #KEY_TOOLS #KEY_POWEROFF #KEY_CHUP #KEY_CHDOWN #KEY_CONTENTS #KEY_W_LINK #Media P #KEY_RSS #Internet #KEY_MTS #Dual #KEY_CAPTION #Subt #KEY_REWIND #KEY_FF #KEY_REC #KEY_STOP # Bonus buttons not on the normal remote: #KEY_TV #Don't work/wrong codes: #KEY_CONTENT #KEY_INTERNET #KEY_PC #KEY_HDMI1 #KEY_OFF #KEY_POWER #KEY_STANDBY #KEY_DUAL #KEY_SUBT #KEY_CHANUP #KEY_CHAN_UP #KEY_PROGUP #KEY_PROG_UP # First configure the connection ipencoded = base64.b64encode(myip) macencoded = base64.b64encode(mymac) messagepart1 = chr(0x64) + chr(0x00) + chr(len(ipencoded)) \ + chr(0x00) + ipencoded + chr(len(macencoded)) + chr(0x00) \ + macencoded + chr(len(base64.b64encode(remotename))) + chr(0x00) \ + base64.b64encode(remotename) part1 = chr(0x00) + chr(len(appstring)) + chr(0x00) + appstring \ + chr(len(messagepart1)) + chr(0x00) + messagepart1 sock.send(part1) messagepart2 = chr(0xc8) + chr(0x00) part2 = chr(0x00) + chr(len(appstring)) + chr(0x00) + appstring \ + chr(len(messagepart2)) + chr(0x00) + messagepart2 sock.send(part2) # Now send the keys as you like, e.g., sendKey("KEY_VOLUP",sock,tvappstring) time.sleep(1) sendKey("KEY_TOOLS",sock,tvappstring) time.sleep(1) sendKey("KEY_RIGHT",sock,tvappstring) time.sleep(1) sendKey("KEY_DOWN",sock,tvappstring) time.sleep(1) sendKey("KEY_RIGHT",sock,tvappstring) # Close the socket when done sock.close()
Hi
Well Done on creating the above
I am also trying to do the same but having a few problems
Basically I have created a LUA script based on your python to control the Samsung TV’s over IP
I can Send The KEY CODES to the set with no problem!
My problem is that i want to go a bit further and do the following
-send a command to the TV that opens the Web Browser, or the Photos APP, or even BBC IPLAYER.
I have sniffed ( wireshark) the set whilst opening the Photos app and managed to find in the TCP Stream the following
L2FjdGlvbj9zZWxlY3Q9MzFfcGhvdG9z
Running this against a Base 64 decoder the result is:
/action?select=31_photos
I have also noticed that IT is a HTTP GET command.
This TCP Stream was on port 55001
Now I am completely stuck not knowing what to do next!
Interested in Helping?
If So could you create a simple python script that opens say the web browser or the photos app in the TV?
you can come back to me on marcodias@me.com
Thanks
Hello Marco
Thanks for your comment. If I understand your correctly, then you have already found the solution to what you want to do. Why not simply send “http://TVIPADDRESS:55001/action?select=31_photos” to the TV base64 encoded?
To Activate 3D?
what’s the key value?
for 3d use: KEY_PANNEL_CHDOWN
How to open web browser using key code? Whats KEY_RSS?
Pingback: Samsung TV Remote Control » xfp's docs
How to open web browser using key code?
Working perfectly!
Thanks a lot!
Hi,
Thank you for your work. I was also trying to write such sw with c#. I first sniffed GEMOTE app with wireshark. And sent the the same data to my tv with my code but it did not work. I also tried your code with python 3.2.3. I receive below error code;
Traceback (most recent call last):
File “C:\Documents and Settings\Özgür Karadeniz\Desktop\samsungremote.py”, line 100, in
ipencoded = base64.b64encode(myip)
File “C:\Python32\lib\base64.py”, line 56, in b64encode
raise TypeError(“expected bytes, not %s” % s.__class__.__name__)
TypeError: expected bytes, not str
Could you please help. I am stuck. Thank you…
I am having issue due to version python 3.5 Were you able to resolve it?
Hi,
Thank for your application. I was just trying to write a sw like yours in order to manage channel recording from a usefull interface. I captured GreenFlowRemote with wireshark and sent exactly the same data with my code written in c#. But I am unable to control my tv. I can see the same datas as GreenFlow on wireshark but does not control.
After that I found your usefull application. But I run your application with Python2. I tried to send only below command;
sendKey(“KEY_VOLUP”,sock,tvappstring)
time.sleep(1)
it gives no error but does not control tv. Tv also did not as for permission for your application. Could you help?
Hello Özgür
I am not aware of how GreenFlowRemote works. Perhaps it sends certain “extra” information that you have not been able to capture.
On the other hand, my Python script should work actually. Perhaps you have not copied the script correctly. I would suggest downloading it from the link given in my post and then trying.
@Ozgur I am trying to write this in c# as well. Any chance you can post your code?
Thanks
I tried to tranform this code to python 3 and i noticed that the authentification doesn’t work even if i send the exact same informations. Later i tried to remove the second message and suddenly it works.
Maybe this could help someone who tries to transform this into another language. Just send the first message and after that you can pass any message corresponding to a key. Even if the application authorized is not authorized you can authentificate with the first message only.
If someone found out how to send more commands than keys or even recieve informations from the tv, i would the very interested even if it needs samygo!
Greate post. Really found it usefull. What I wanted to know is why do we add som any null charaters (chr(0x00)) in the script?
This is Great! Does anybody has IP codes for LG Smart TV?
Very useful post. I used your code as reference to build a CLI remote control in Perl . Thank you!
Works perfect, i see some nice domotica possiblities 🙂
And i found an extra key KEY_HDMI, this allows you to toggle between active HDMI sources. Very useful if you only have one active HDMI port.
I am not a programmer but I was looking for a way to try and put caller id name and number from Asterisk (PBX) on my Samsung TV. Are you familiar with Asterisk or have any interest in this?
Hi Steve
I do not have information about your question. I am also an Asterisk user and would be interested in knowing how it can be done. I think you can send such information from Asterisk to dreambox which should then appear on the TV (provided, of course, that you are using dreambox).
I have a Samsung HT-E6750W Blu ray home theatre system. Most of the commands work such as “KEY_VOLUP”, KEY_CHUP, ect however I can’t find a command that works for power. I have tried the following codes with no luck and have searched through numerous forums.
KEY_OFF
KEY_POWER
KEY_POWEROFF
KEY_POWER_OFF
KEY_PWR
KEY_D_OFF
KEY_PANEL_POWER
KEY_STANDBY
KEY_FRONT_POWER
KEY_FRONT_OFF
KEY_FRONT_POWEROFF
Any Suggestions??
I am not aware of any power command. I would also be interesting in knowing if there is such a command.
I suspect that, other than by IR remote, the power is handled at the HDMI-CEC level only.
I have a Samsung 46F6400 and I’m trying to control my TV through an application (mobile app, or WebApp, or PC app) in order to integrate it in a automation installation. Your program works well except for powering on the TV.
I couldn’t ping the TV when it’s powered off, so there is no way to power on the TV. But, in this forum there are talking about power on/off the TV through the HDMI-CEC, as you suggest, with a Raspberry Pi.
I have a Samsung 46F6400 and I’m trying to control my TV through an application (mobile app, or WebApp, or PC app) in order to integrate it in a automation installation. Your program works well except for powering on the TV.
I couldn’t ping the TV when it’s powered off, so there is no way to power on the TV. But, in there are talking about power on/off the TV through the HDMI-CEC, as you suggest, with a Raspberry Pi.
My theater system is HT-D6750. It works with BD_KEY_POWER for power off.
I’m getting a error in the scrip that says something like thist:
“No connection can be made because the receiving pc actively rejects it”
I got the IPs and macs putted correcly…is it something with the port 55000?
I have a 2014 H series
you find any solution? I have the same problem.
What is the command for hdmi3 please?
Where did you get the codes for the controls/remote keys? Are there any more? The power on and off functionality would be great. Has there been any further development/new info on the power issue?
Thank you for creating such a useful utility.
I suspect that this may very well be impossible, but here goes …
Is there any way to get back the information as to which TV station is currently being viewed, or to grab a screen capture?
I used Python 2.x and 3.x but return an error aftern run (f5):
Traceback (most recent call last):
File “C:\Users\VANDA\Desktop\ss.py”, line 35, in
sock.connect((tvip, 55000))
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
my tv model is F6400
Traceback (most recent call last):
File “remote.py”, line 122, in
sendKey(“KEY_MUTE”,sock,tvappstring)
File “remote.py”, line 31, in sendKey
dataSock.send(part3);
socket.error: [Errno 32] Broken pipe
on UE40ES5500, why?