Pages

23 January 2013

remote control commands to the Samsung tv over LAN

#!  /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" #get_settings('tvip')

#IP Address of TV

myip = "100.0.0.112" #get_settings('myip')

#Used for the access control/validation, but not after that AFAIK

mymac = "00-0c-29-3e-b1-4f" #get_settings('mymac')

#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.closet()

### Attempt to make a samsung control ###

def key_0():

    try:

        sendKey("KEY_0",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key 0, DEBUG')

def key_1():

    try:

        sendKey("KEY_1",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key 1, DEBUG')

def key_2():

    try:

        sendKey("KEY_2",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key 2, DEBUG')

def key_3():

    try:

        sendKey("KEY_3",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key 3, DEBUG')

def key_4():

    try:

        sendKey("KEY_4",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key 4, DEBUG')

def key_5():

    try:

        sendKey("KEY_5",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key 5, DEBUG')

def key_6():

    try:

        sendKey("KEY_6",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key 6, DEBUG')

       

def key_7():

    try:

        sendKey("KEY_7",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key 7, DEBUG')

def key_8():

    try:

        sendKey("KEY_8",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key 8, DEBUG')

def key_9():

    try:

        sendKey("KEY_9",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key 9, DEBUG')

def key_up():

    try:

        sendKey("KEY_UP",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key up, DEBUG')

def key_down():

    try:

        sendKey("KEY_DOWN",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key 9, DEBUG')

def key_left():

    try:

        sendKey("KEY_LEFT",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key left, DEBUG')

def key_right():

    try:

        sendKey("KEY_RIGHT",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key right, DEBUG')

def key_menu():

    try:

        sendKey("KEY_MENU",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key menu, DEBUG')

def key_prech():

    try:

        sendKey("KEY_PRECH",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key menu, DEBUG')

def key_guide():

    try:

        sendKey("KEY_GUIDE",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key menu, DEBUG')

def key_info():

    try:

        sendKey("KEY_INFO",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key menu, DEBUG')

def key_return():

    try:

        sendKey("KEY_RETURN",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key menu, DEBUG')

def key_ch_list():

    try:

        sendKey("KEY_CH_LIST",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key menu, DEBUG')

def key_exit():

    try:

        sendKey("KEY_EXIT",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key exit, DEBUG')

def key_enter():

    try:

        sendKey("KEY_ENTER",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key enter, DEBUG')

def key_source():

    try:

        sendKey("KEY_SOURCE",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key source, DEBUG')

def key_ad():

    try:

        sendKey("KEY_AD",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key AD, DEBUG')

def key_play():

    try:

        sendKey("KEY_PLAY",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key PLAY, DEBUG')

def key_pause():

    try:

        sendKey("KEY_PAUSE",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key PAUSE, DEBUG')

def key_mute():

    try:

        sendKey("KEY_MUTE",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key PLAY, DEBUG')

def key_picture_size():

    try:

        sendKey("KEY_PICTURE_SIZE",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key PICTURE SIZE, DEBUG')

def key_volup():

    try:

        sendKey("KEY_VOLUP",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key VOLUME UP, DEBUG')

def key_voldown():

    try:

        sendKey("KEY_VOLDOWN",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key PICTURE SIZE, DEBUG')

def key_tools():

    try:

        sendKey("KEY_TOOLS",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key TOOLS, DEBUG')

def key_poweroff():

    try:

        sendKey("KEY_POWEROFF",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key POWER OFF, DEBUG')

def key_poweroff():

    try:

        sendKey("KEY_POWEROFF",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key POWER OFF, DEBUG')

       

def key_chup():

    try:

        sendKey("KEY_CHUP",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key CH UP, DEBUG')

def key_chdown():

    try:

        sendKey("KEY_CHDOWN",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key CH DOWN, DEBUG')

def key_contents():

    try:

        sendKey("KEY_CONTENTS",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key CONTENTS, DEBUG')

def key_chdown():

    try:

        sendKey("KEY_CHDOWN",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key CH DOWN, DEBUG')

def key_w_link():

    try:

        sendKey("KEY_W_LINK",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key W LINK, DEBUG')

def key_media_p():

    try:

        sendKey("Media P",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key MEDIA P, DEBUG')

def key_rss():

    try:

        sendKey("KEY_RSS",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key RSS, DEBUG')

def key_internet():

    try:

        sendKey("KEY_CHDOWN",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key INTERNET, DEBUG')

def key_mts():

    try:

        sendKey("KEY_MTS",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key CH MTS, DEBUG')

def key_dual():

    try:

        sendKey("Dual",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key DUAL, DEBUG')

def key_caption():

    try:

        sendKey("KEY_CAPTION",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key CAPTION, DEBUG')

def key_subt():

    try:

        sendKey("Subt",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key SUBT, DEBUG')

def key_rewind():

    try:

        sendKey("KEY_REWIND",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key REWIND, DEBUG')

def key_ff():

    try:

        sendKey("KEY_FF",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key CH DOWN, DEBUG')

def key_rec():

    try:

        sendKey("KEY_REC",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key REC, DEBUG')

def key_stop():

    try:

        sendKey("KEY_STOP",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key STOP, DEBUG')

def key_stop():

    try:

        sendKey("KEY_TV",sock,tvappstring)

        sock.close()

    except:

        #logger.log('Samsung remote failed pressing key STOP, DEBUG')

# To test this remote you need to call each function eg if you want to the stop key you write: key_stop()

# After this comments

0 comments: