Summer Nights

A blog on embedded and real-time systems as well as on general computing issues

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

 

21 Responses to “Samsung TV Remote Control over LAN/WLAN using Python”

  • Marco says:

    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

    • admin says:

      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?

  • Bangcook says:

    To Activate 3D?

    what’s the key value?

  • [...] http://deneb.homedns.org/things/?p=232  Posted by xfp at 2330  Tagged with: samsung, samsung tv, [...]

  • Zach says:

    How to open web browser using key code?

  • Dan says:

    Working perfectly!

    Thanks a lot!

  • Ozgur says:

    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…

  • Özgür says:

    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?

    • admin says:

      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.

  • ptrout says:

    @Ozgur I am trying to write this in c# as well. Any chance you can post your code?

    Thanks

  • Jismo says:

    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!

  • Sab says:

    Greate post. Really found it usefull. What I wanted to know is why do we add som any null charaters (chr(0×00)) in the script?

  • Mike Tsinberg says:

    This is Great! Does anybody has IP codes for LG Smart TV?

  • Stefan says:

    Very useful post. I used your code as reference to build a CLI remote control in Perl . Thank you!

  • Chopper_Rob says:

    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.

  • steve says:

    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?

    • admin says:

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

  • Sam says:

    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??

    • admin says:

      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.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="">