Skip to content

Instantly share code, notes, and snippets.

@danielfaust
Created May 30, 2011 04:12
Show Gist options
  • Save danielfaust/998441 to your computer and use it in GitHub Desktop.
Save danielfaust/998441 to your computer and use it in GitHub Desktop.
Samsung TV Remote Control Python Script
import time
import socket
import base64
src = '192.168.1.2' # ip of remote
mac = '00-AB-11-11-11-11' # mac of remote
remote = 'python remote' # remote name
dst = '192.168.1.3' # ip of tv
app = 'python' # iphone..iapp.samsung
tv = 'LE32C650' # iphone.LE32C650.iapp.samsung
def push(key):
new = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
new.connect((dst, 55000))
msg = chr(0x64) + chr(0x00) +\
chr(len(base64.b64encode(src))) + chr(0x00) + base64.b64encode(src) +\
chr(len(base64.b64encode(mac))) + chr(0x00) + base64.b64encode(mac) +\
chr(len(base64.b64encode(remote))) + chr(0x00) + base64.b64encode(remote)
pkt = chr(0x00) +\
chr(len(app)) + chr(0x00) + app +\
chr(len(msg)) + chr(0x00) + msg
new.send(pkt)
msg = chr(0x00) + chr(0x00) + chr(0x00) +\
chr(len(base64.b64encode(key))) + chr(0x00) + base64.b64encode(key)
pkt = chr(0x00) +\
chr(len(tv)) + chr(0x00) + tv +\
chr(len(msg)) + chr(0x00) + msg
new.send(pkt)
new.close()
time.sleep(0.1)
while True:
# switch to tv
push("KEY_TV")
# switch to channel one
push("KEY_1")
push("KEY_ENTER")
time.sleep(5)
# switch to channel 15
push("KEY_1")
push("KEY_5")
push("KEY_ENTER")
time.sleep(5)
# switch to HDMI
push("KEY_HDMI")
time.sleep(5)
@Will695
Copy link

Will695 commented Feb 29, 2020

I'm getting
Traceback (most recent call last):
File "c:\Users\Will.vscode\extensions\ms-python.python-2020.2.64397\pythonFiles\ptvsd_launcher.py", line 48, in
main(ptvsdArgs)
File "c:\Users\Will.vscode\extensions\ms-python.python-2020.2.64397\pythonFiles\lib\python\old_ptvsd\ptvsd_main_.py", line 432, in main
run()
File "c:\Users\Will.vscode\extensions\ms-python.python-2020.2.64397\pythonFiles\lib\python\old_ptvsd\ptvsd_main_.py", line 316, in run_file
runpy.run_path(target, run_name='main')
File "C:\Users\Will\AppData\Local\Programs\Python\Python36-32\lib\runpy.py", line 263, in run_path
pkg_name=pkg_name, script_name=fname)
File "C:\Users\Will\AppData\Local\Programs\Python\Python36-32\lib\runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "C:\Users\Will\AppData\Local\Programs\Python\Python36-32\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "c:\Users\Will\Google Drive\Python Scripts\Samsung\SmartThings\pysmartthingspy.py", line 57, in
push("KEY_TV")
File "c:\Users\Will\Google Drive\Python Scripts\Samsung\SmartThings\pysmartthingspy.py", line 41, in push
chr(len(base64.b64encode(remote))) + chr(0x00) + base64.b64encode(remote)
File "C:\Users\Will\AppData\Local\Programs\Python\Python36-32\lib\base64.py", line 58, in b64encode
encoded = binascii.b2a_base64(s, newline=False)
TypeError: a bytes-like object is required, not 'str'

I've got this as my config
src = 'IP of PC' # ip of remote
mac = 'Mac of PC' # mac of remote
remote = 'Hostname of PC' # remote name
dst = 'IP of TV' # ip of tv
app = 'python' # iphone..iapp.samsung
tv = '[TV] Samsung' # Name of my TV

and set the port to 8001 as i've got a 2015 tv

I don't actually have a SmartThings HUB, I've been trying to control my TV via my pc or Hassio

@nandishyb
Copy link

Do you know the way we can send the mouse pointer movement command to Samsung 2013 TV ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment