kdschlosser / samsungctl

Remote control Samsung televisions via a TCP/IP connection
MIT License
148 stars 34 forks source link

UPDATED POWER KEY HANDLING #96

Open kdschlosser opened 5 years ago

kdschlosser commented 5 years ago

OK folks so here is the skinny on this.

UPDATE TO POWER KEYS.. In order to remove confusion of what TV supports what power keys. I have decided to alter the Samsung handling of keys. The way Samsung had them set up seemed to cause to much confusion. I think that everyone will like this way better.

KEY_POWER = toggle. This is going to toggle the power. so if the TV is off it will turn it on. and if it is on it will turn it off.

KEY_POWERON = Discrete power on. this will always turn the TV on if it is off.

KEY_POWEROFF = You guessed it. Discrete power off. If the TV is on this will turn it off.

This is a much easier mechanism and a whole lot easier to grasp. so no matter what TV you have and what key it supports no longer matters. This is what these keys will do. plain and simple.

raydog153 commented 5 years ago

@kdschlosser This makes sense in many ways. The Samsung serial protocol is identical to what what you have above, so makes sense from that perspective as well. The serial protocol also supports fetching the power on state (Not sure what models or when this was added, but my newer tv supports it but not my older one)

I think the problem with the toggle for many is that you really need to know the current power state (Many ways to tackle or half support this). Looking from a UX perspective, when one uses a remote you kow if TV is on or off and so manually you choose accordingly. However for automation a toggle is next to useless unless you also know the state.

So the toggle is good if your trying to build some type of remote control, and the on/off is good for automation.

kdschlosser commented 5 years ago

Serial communications is always the best to use in any HA setup it is the defacto rock solid connection method used. that is why high end devices will always have a serial port on them. any Samsung device that has a serial port (ex-link jack) has always been able to supply the power state of the TV.

Getting the power state of a TV is a really simple thing to do. if you use an FTDI USB to TTL Serial adapter.. one that is +-5V tolerant you can simply wire the DTR pin (Terminal Reay) to the +5 and a USB A male cable and the -5V from the USB A to the GRND on the adapter.. you will want to use a 10Kohm resistor and jump across the +5V an the GRND. this will pull the +5V to ground when the TV is off this is what is called a "pull down resistor" because when the TV gets powered off the +5V gets disconnected.. it's like just cutting the wire.. so it "floats" it has no reference to ground or to +5v. so even something as simple as static discharge. or the electrical current that you body produces can cause a change in that wire. which is not wanted..

then all you need to do is check the serial port to see if there is a connection on the other end if there is then the TV is on. if there is not then the TV is off. every TV I have come across that has a USB jack on it will power off the USB when the TV goes into standby. and powers it back on when the TV is on. fool proof way to get the power state from the TV if there is no 2 way communications for the TV.

raydog153 commented 5 years ago

Interesting solution....I was just going to use ping as a hack for now. Quick and dirty. The older TV does have network and checking ping for 'on' status should be fast and simple. Off state would be accurate too unless TV was just turned on. I also energy monitoring devices and I could easily check the circuit my TV is on (TV has largest consumption on circuit, so easy to detect TV is on vs other devices based on current usage) to see how much power is currently being consumed. If more than x watts TV must be on.

You also mention that not all TVs support the KEY_POWER, could it be that it depends if TV has front power switch as well as well? Because I think there is also a KEY_PANNEL_POWER.

p3g4asus commented 5 years ago

Sorry for my ignorance, but if I have a WiFi connection to the TV, how will sending the poweron key be effective? Do I need an ethernet connection for wake on LAN? @raydog153 I agree, pinging to get the power state seems a easy but 99% of the time working solution.

kdschlosser commented 5 years ago

with the Samsung TV's it's easy to detect of the TV is on or not.

how I am detecting power state on the websocket based TV's is this. I am simply trying to connect to

http://IP:8001/api/v2

if it connects then guess what??? TV is on if not TV is off.

and for the legacy when someone wanted to know the power state i check the socket. if you try to a receive on a socket that has a broken connection it will kick out an error. So i set the socket to be non blocking, normally a socket receive will sit there and wait until data comes in when you call receive. we do not want to get "stuck" there. so we set non blocking. this will try to receive and then return if the connection is no good it will cause an error that i catch and then return False if there is a connection i then set the socket connection to block again and then return True

there is no need to use ping. or anything else for that matter, the stuff i stated above was to share knowledge for TV's that do not have serial or a network connection. and this means any brand and model.... so if you owned like a 4000 series TV that has no ability to do network anything an has no ex-link port. and you were using a blaster.. you could get the state of the TV using the method i provided above.

You can get really crafty with power states.. In my system all of the remotes in my house all share exactly the same IR codes. they are all the same brand.. I can carry them from room to room and when I enter that room the remote will operate the devices in the room without me needing to do a thing.

The remote control IR code gets sent to a central HA server.. and when a code comes into the server (the code is sent over the network) the server knows the source IP address. Because the IP address is assigned to a specific room so now I now where the IR code came from. I am then able to determine what device I should control and send back over the network the command that is needed to control that device... this is the cool part. specifically with my media room. if the power state on a device changes and no remote code was processed the system knows i pressed the button right on the specific device. I have mine set up so that if i press the power button on the TV. it will then power up the whole system. and vice versa.. this is what i call the "I lost the remote and am to lazy to go and find it" power control.

kdschlosser commented 5 years ago

the issue with piing is that it cannot be done from code without running the script as sudo or Administrator. So what gets done when you run ping is a separate process gets started to run ping and the output gets monitored.. one HUGE flaw with this. if for some reason the main program errors out.. the ping process never stops. it continues to run with no way of reattaching to the process. the user then has to locate the process an kill it manually.. NO THANK YOU i will pass on that headache. Plus using piing causes extra network traffic when it is really not needed.

kdschlosser commented 5 years ago

and since this process is orphaned with no means to regulate it, it will use 100% of one of the cores of your computer.

p3g4asus commented 5 years ago

You are right about ping. I did not think about implications. Your setup is really interesting. What does HA server stand for?

kdschlosser commented 5 years ago

HA = Home Automation

kdschlosser commented 5 years ago

also I am not sure if a WOL packets traverse WiFi or not.. it is just a UDP broadcast that has the mac address of the target machine encapsulated in the packet.

So I do not see a reason why it shouldn't work.

kdschlosser commented 5 years ago

HA Equipment List

I know i am forgetting some things.

Main server specs

raydog153 commented 5 years ago

Well, didn't literally mean using ping process (that would make no sense to spin up aa process just for that, among other things) but equivalent in code using simple socket connection.

But that I do not even need to do for serial as after more testing if TV is off/standby the any serial command responds as failed, so even thou the status commands do not work, I can use the status as returned to detect powered state. Much better than tcp since network may take some time to start. tcp would still be good to add to distinguish between starting up and fully powered on and ready to receive commands.

raydog153 commented 5 years ago

WOL does traverse Wifi, because my 2014 TV is only hooked up as wifi and worked for me. How the network packets are trasnmitt4ed really do not matter, how your network is setup matter more. Some routers separate the wifi and wired networks by default and do not let them talk to each other...configurable.

kdschlosser commented 5 years ago

You cannot create an ARP packet to ping with from code without the code being run as sudo or Administrator.

The way I am leveraging the mac address to be discovered is through a sort of hole. Unfortunately it cannot be used because if there is an entry in the ARP table that is returned. and if there is not then an APR packet gets sent from the OS.

kdschlosser commented 5 years ago

Broadcast packets are not routable. so if you have 2 logical networks that are separated by a router. samsungctl will work to control a TV on one network while the PC is on the other, except for powering the TV on.

That being stated.. if you have set up a WiFi Router and you have the WAN port plugged into your local network. you will be running 2 logical networks. Some people do like to make 2 different networks one for WiFi and the other is wired..