Guest User

Samsung TV control Python

a guest
Jun 3rd, 2015
1,194
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!  /usr/bin/python
  2. #   Title: samsungremote.py
  3. #   Author: Asif Iqbal
  4. #   Date: 05APR2012
  5. #   Info: To send remote control commands to the Samsung tv over LAN
  6. #   TODO:
  7.  
  8. import socket
  9. import base64
  10. import time, datetime
  11. import sys
  12.  
  13. cmdargs = str(sys.argv)
  14.  
  15. #IP Address of TV
  16. tvip = "192.168.178.7"
  17. #IP Address of TV
  18. myip = "192.168.178.14"
  19. #Used for the access control/validation, but not after that AFAIK
  20. mymac = "b8:27:eb:b1:6c:7c"
  21. #What the iPhone app reports
  22. appstring = "iphone..iapp.samsung"
  23. #Might need changing to match your TV type
  24. tvappstring = "iphone.UE40D6500.iapp.samsung"
  25. #What gets reported when it asks for permission
  26. remotename = "Python Samsung Remote"
  27.  
  28. # Function to send keys
  29. def sendKey(skey, dataSock, appstring):
  30.  messagepart3 = chr(0x00) + chr(0x00) + chr(0x00) + chr(len(
  31. base64.b64encode(skey))) + chr(0x00) + base64.b64encode(skey);
  32.  part3 = chr(0x00) + chr(len(appstring)) + chr(0x00) \
  33. + appstring + chr(len(messagepart3)) + chr(0x00) + messagepart3
  34.  dataSock.send(part3);
  35.  
  36. # Open Socket
  37. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  38. sock.connect((tvip, 55000))
  39.  
  40. # Key Reference
  41. # Normal remote keys
  42.  #KEY_0
  43.  #KEY_1
  44.  #KEY_2
  45.  #KEY_3
  46.  #KEY_4
  47.  #KEY_5
  48.  #KEY_6
  49.  #KEY_7
  50.  #KEY_8
  51.  #KEY_9
  52.  #KEY_UP
  53.  #KEY_DOWN
  54.  #KEY_LEFT
  55.  #KEY_RIGHT
  56.  #KEY_MENU
  57.  #KEY_PRECH
  58.  #KEY_GUIDE
  59.  #KEY_INFO
  60.  #KEY_RETURN
  61.  #KEY_CH_LIST
  62.  #KEY_EXIT
  63.  #KEY_ENTER
  64.  #KEY_SOURCE
  65.  #KEY_AD #KEY_PLAY
  66.  #KEY_PAUSE
  67.  #KEY_MUTE
  68.  #KEY_PICTURE_SIZE
  69.  #KEY_VOLUP
  70.  #KEY_VOLDOWN
  71.  #KEY_TOOLS
  72.  #KEY_POWEROFF
  73.  #KEY_CHUP
  74.  #KEY_CHDOWN
  75.  #KEY_CONTENTS
  76.  #KEY_W_LINK #Media P
  77.  #KEY_RSS #Internet
  78.  #KEY_MTS #Dual
  79.  #KEY_CAPTION #Subt
  80.  #KEY_REWIND
  81.  #KEY_FF
  82.  #KEY_REC
  83.  #KEY_STOP
  84. # Bonus buttons not on the normal remote:
  85.  #KEY_TV
  86. #Don't work/wrong codes:
  87.  #KEY_CONTENT
  88.  #KEY_INTERNET
  89.  #KEY_PC
  90.  #KEY_HDMI1
  91.  #KEY_OFF
  92.  #KEY_POWER
  93.  #KEY_STANDBY
  94.  #KEY_DUAL
  95.  #KEY_SUBT
  96.  #KEY_CHANUP
  97.  #KEY_CHAN_UP
  98.  #KEY_PROGUP
  99.  #KEY_PROG_UP
  100.  
  101.  
  102. # First configure the connection
  103. ipencoded = base64.b64encode(myip)
  104. macencoded = base64.b64encode(mymac)
  105. messagepart1 = chr(0x64) + chr(0x00) + chr(len(ipencoded)) \
  106. + chr(0x00) + ipencoded + chr(len(macencoded)) + chr(0x00) \
  107. + macencoded + chr(len(base64.b64encode(remotename))) + chr(0x00) \
  108. + base64.b64encode(remotename)
  109.  
  110. part1 = chr(0x00) + chr(len(appstring)) + chr(0x00) + appstring \
  111. + chr(len(messagepart1)) + chr(0x00) + messagepart1
  112. sock.send(part1)
  113.  
  114. messagepart2 = chr(0xc8) + chr(0x00)
  115. part2 = chr(0x00) + chr(len(appstring)) + chr(0x00) + appstring \
  116. + chr(len(messagepart2)) + chr(0x00) + messagepart2
  117. sock.send(part2)
  118.  
  119.  
  120. # Now send the keys as you like, e.g.,
  121. #sendKey("KEY_CHDOWN",sock,tvappstring)
  122. sendKey(str(sys.argv[1]),sock,tvappstring)
  123. print str(sys.argv[1])
  124. # Close the socket when done
  125. sock.close()
RAW Paste Data