Samsung Smart TV Support


(Im Brian) #1

There are discussions about integrating some kind of home AV equipment to ST. There’s a lot of discussion about IR blasters to control some devices (which, if I could find a good Zigbee/z-wave IR blaster that plays well with my setup, I’ll order it in a heartbeat).

As a (free) alternative for those with smart TVs would be to see if they have an API to send commands to. I have Samsung SmartTVs and I know they do have some level of support, as they allow apps on the same network to control them. I thought I’d share (what little) I know to see if anyone would have any suggestions or be interested in moving this along.

During today’s Office Hours, it was announced that they’ll soon enable limited support for the ST hub to control devices within the network, thereby removing most security concerns - opening a port to send commands through. So, with this change, I think this idea could inch closer to being doable.

Most (all?) Samsung SmartTVs run some kind of server on them. You should be able to see this if you can find the IP your router assigns it and access: http://192.168.0.100:55000/

It shouldn’t display anything, but it’ll let you know something on the other end is listening (connection refused or some such instead of the standard “not available” error).

Here, someone has reverse engineered the control setup in Python: https://gist.github.com/danielfaust/998441.

There’s some more discoveries being made here: http://forum.samygo.tv/viewtopic.php?f=12&t=1792&sid=d1c9c0a6aca3c5da07c594fdabb9a4f2&start=10

I’m not very familiar with Python, but from the first link, it seems like there’s quite a bit of non-standard stuff going on. From an example in PHP (which I am familiar with), they open a socket to the TV and base64 encode chunks of data and write to the socket in “chunks”.

Not sure if there’s any interest in pursuing this.


Samsung TV integration
Turning on tv and or av equipment without using Harmony?
Samsung TV
SmartTV Integration - LG?
(Im Brian) #2

Here is the unaltered PHP code (which I have not tested) for the interested or brave:

<?
	//error_reporting(E_ALL);

    
    $tvip = "192.168.1.96"; //IP Address of TV
    $myip = "192.168.1.200"; //Doesn't seem to be really used
    $mymac = "00-0c-29-3e-b1-4f"; //Used for the access control/validation, but not after that AFAIK
    $appstring = "iphone..iapp.samsung"; //What the iPhone app reports
    $tvappstring = "iphone.LE46C650.iapp.samsung"; //Might need changing to match your TV type
    $remotename = "Perl Samsung Remote"; //What gets reported when it asks for permission/also shows in General->Wireless Remote Control menu

    echo "Content-type: text/html\n\n";

    $sock = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
	$result = socket_connect($sock, $tvip, '55000');
    if( $result === false)
	   die ("Could not create socket: \n");

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

	$ipencoded = base64_encode($myip);
	$macencoded = base64_encode($mymac);
    $messagepart1 = chr(0x64) . chr(0x00) . chr(strlen($ipencoded)) . chr(0x00) . $ipencoded . chr(strlen($macencoded)) . chr(0x00) . $macencoded .
                     	chr(strlen(base64_encode($remotename))) . chr(0x00) . base64_encode($remotename);
						
    $part1 = chr(0x00) . chr(strlen($appstring)) . chr(0x00) . $appstring . chr(strlen($messagepart1)) . chr(0x00) . $messagepart1;

    socket_write($sock, $part1, strlen($part1));
    echo $part1;
    echo "\n";

    $messagepart2 = chr(0xc8) . chr(0x00);
    $part2 = chr(0x00) . chr(strlen($appstring)) . chr(0x00) . $appstring . chr(strlen($messagepart2)) . chr(0x00) . $messagepart2;
    socket_write($sock, $part2, strlen($part2));
    echo $part2;
    echo "\n";

    //Preceding sections all first time only

    if (isset($_REQUEST["key"])) {
       //Send remote key
       $key = "KEY_" . $_REQUEST["key"];
       $messagepart3 = chr(0x00) . chr(0x00) . chr(0x00) . chr(strlen(base64_encode($key))) . chr(0x00) . base64_encode($key);
       $part3 = chr(0x00) . chr(strlen($tvappstring)) . chr(0x00) . $tvappstring . chr(strlen($messagepart3)) . chr(0x00) . $messagepart3;
       socket_write($sock,$part3,strlen($part3));
       echo $part3;
       echo "\n";
    } else if (isset($_REQUEST["text"])) {
       //Send text, e.g. in YouTube app's search, N.B. NOT BBC iPlayer app.
       $text = $_REQUEST["text"];
       $messagepart3 = chr(0x01) . chr(0x00) . chr(strlen(base64_encode($text, ""))) . chr(0x00) . base64_encode($text, "");
       $part3 = chr(0x01) . chr(strlen($appstring)) . chr(0x00) . $appstring . chr(strlen($messagepart3)) . chr(0x00) . $messagepart3;
       socket_write($sock,$part3,strlen($part3));
       echo $part3;
       echo "\n";   
    }

    socket_close($sock);

    echo "\n\n";
?>

(Im Brian) #3

…And there’s decent documentation for Roku for less than smart-tvs: http://sdkdocs.roku.com/display/sdkdoc/Web+Service+API


(Im Brian) #4

Still doing research here - looks like 2011 models (possibly older?) use XHR requests. 2012+ appear to use web sockets.


(Im Brian) #5

For my newer model, I have a working (rough) prototype ported to node.js. I’ll be adding some polish and features to host it off my Raspberry Pi to have a universal remote for all connected devices within the network.

I’ll also likely expose a REST api so I can have ST trigger events via the Raspberry Pi.

That being said, I’ll probably start experimenting with having ST have at least some limited support as a stand-alone interface. I’ll need the local IP access in the forthcoming firmware update and have to experiment with Web Sockets in Groovy. But, porting from JS -> Groovy has to be easier than the per/php -> JS I just did.


(Anderson Costa) #6

Hi,

I’m researching how to control my Samsung Smart LED TV F5500 by computer and I found this topic.

I created a Virtual Assistant for Windows called AFC Assistente, and it execute several functions including Home Automation. I am now searching to add integration of the Assistant with Smart TVs from Samsung, but I have questions about the differences of Smart TV models, the Python code is of the C-Series models and I need to know more about the F-Series models to I can get to do the tests.

In this part for example, what should I put in my code:

app = ‘python’ # iphone…iapp.samsung
tv = ‘LE32C650’ # iphone.LE32C650.iapp.samsung


(Im Brian) #7

@neo3 - I’ve created a node.js app that controls Samsung TVs, Rokus, PS3 and Panasonic TVs (soon to support LG TVs and possibly Onkyo stereos). You can grab the source here, but it’s being actively developed:

If you just want to use the PHP lib I cited above, leave all values as default except the IPs(obviously), your MAC address (of the computer controlling the TV, not the TV itself) and the “Remote Name” if you want (but you can leave it).


(Anderson Costa) #8

@imbrian - Thank you very much! With the help of the your code and of the PHP code I can create a function in Visual Basic.


(Anderson Costa) #9

This function also works on the Samsung models A-Series and B-Series?


(Im Brian) #10

@neo3 - unsure of specific models, but if you have a “SmartHub”, it should work.


(Anderson Costa) #11

It is already possible be sure that the code works on C-Series and superior models, but in the SamyGo says nothing of the A-Series and B-Series models.


(Anderson Costa) #12

All controls that you created have been tested?


(Im Brian) #13

Within the Samsung controller, there’s a “keymap” which is a whitelist of all known Samsung commands. There’s a ton of them. I have not tested them all, as many are likely dependent on state of the TV (hitting “REC” while in a menu probably isn’t going to do anything). Within the template file, I have the interface I use for my daily remote. It’s simply written in HTML and issues my desired commands. The goal is to say “here’s a bunch of commands - you can either use my template - or generate your own to make a remote that’s exactly as you’d like it”.


(Anderson Costa) #14

I asked in relation to other devices, all controls were tested?

Because I would like to add other TV brands in my Virtual Assistant, but I can’t do the tests.


(Darryl) #15

@ImBrian : I have an LG SmartTV, I would be more than happy to be a guinea pig… :slight_smile:


(Im Brian) #16

@darrylb - that’s the next device on my list (err, maybe Nest, I don’t know). It’ll probably be a ways out as I’m working on some less shiny parts of the app to make it more flexible. You can monitor that github repo and wait for changes to the lgController.js


(Brad B) #17

Hey all,

I recently purchased a Samsung TV with support for commands over the network. I played around with some of the controllers out there written in various languages, and found it neat. However, I really wanted to control it via SmartThings, so I wrote this:

Right now it’s just a simple app to execute one command only based on a mode change or a press of the app itself. I tried to get the LAN commands working from a Device Type, but was unable to get them to execute. It’s a decent start, though. :slight_smile:


(Todd Wackford) #18

@bradbutner,

That is awesome! I can help you get it to a child device with service manager type app. Do you mind if I cut and paste directly from your code? I have a couple of questions for you too:

  1. Do you know how to display a marquee on the screen? Would be cool to get ST alerts like, "Cookie Jar Motion Detected!".
  2. I've heard that you can't turn the TV on, that it doesn't process IP commands when off. Your code looks like it will (I'll test)
  3. I have 2 Samsung and a Sharp AQUOS TV, do you know how to do this with the Sharp?

Let me know what you think,
Twack


(Brad B) #19

@twack,

Yeah, feel free to take the code and do whatever you like with it. That’s why I posted it out there. :slight_smile:

  1. I’m currently unaware of an ability to display a marquee on the TV. I know you can send text commands (not implemented in my code), but it’s mainly for areas where you have a text input field.
  2. While there is a command for POWER ON, sadly the TV stops all network communication when you shut it off. You can run the command, but nothing will happen. I put it in there as I wanted to verify.
  3. Unsure about Sharp TVs. So far I’ve only tested this on one Samsung TV model.

(Brad B) #20

Just an update that I’m working on a proper “Connect” app. I’ve removed the manual entry of IP and MAC Addresses, replacing it with auto-discovery using UPNP:

It finds, authenticates, and tests against the TV at this point. Next step is to have it go and create an actual device in your Things list. You can still get the original code at the link I previously posted, though.