16

I want to be able to turn my Samsung TV on (and off) the raspberry pi is hooked up to my TV via a HDMI cable.

My cousin already did this with other Tv and it worked, but with the Samsung seems not to work.

  • What is the easiest way to do this?

  • How is this possible?

Thanks for your help!

User98764431
  • 569
  • 1
  • 19
  • 33
Esteban
  • 169
  • 1
  • 1
  • 3

6 Answers6

12

As @lenik's answer says, your TV needs to support CEC. I installed a compiled version of libcec which included the command line utility cec-client from SourceForge using the following commands:

wget http://sourceforge.net/projects/selfprogramming/files/libCEC.deb/libcec_2.1.0-1_armhf.deb/download -O libcec_2.1.0-1_armhf.deb

sudo dpkg -i libcec_2.1.0-1_armhf.deb



UPDATE: 2017-02-27

The link above to SourceForge no longer exists. According to this article you can install cec-client using the following command:

    sudo apt-get install cec-utils

The rest of this answer should still be valid.


Here's a thread on raspberrypi.org that discusses using the cec-client to issue commands.


The thread says that the following command successfully turned on a Samsung TV:
echo "on 0" | cec-client -s



The following commands change the source from TV Tuner to Raspi HDMI:
echo "as" | cec-client -s

or

echo "tx 1f 82 30 00" | cec-client -s



The following commands were unsuccessful in turning the TV off:
echo "tx 10 36" | cec-client -s 

or

echo "standby 0" | cec-client -s

standby 0
DEBUG: [ 56474] << putting 'TV' (0) in standby mode
TRAFFIC: [ 56476]   << 10:36
DEBUG: [ 56477] sending data: len = 1, payload = 10:36
DEBUG: [ 56538] received data: header:00020001 p0:00003610 p1:00000000 p2:00000000 p3:00000000 reason:1
DEBUG: [ 56539] command transmitted
DEBUG: [ 56660] received data: header:00040002 p0:04360001 p1:00000000 p2:00000000 p3:00000000 reason:2
TRAFFIC: [ 56661]   >> 01:00:36:04
NOTICE: [ 56661]    >> TV (0) -> Recorder 1 (1): feature abort ( 0)
DEBUG: [ 56661] marking opcode 'standby' as unsupported feature for device 'TV'
HeatfanJohn
  • 3,125
  • 3
  • 24
  • 36
  • 1
    FWIW echo "standby 0" | cec-client -s works fine for my Panasonic TV, although the process stalls after, and I had to kill -9 it. – Sparhawk Jul 07 '14 at 00:16
  • Link is broken. – jjmontes Feb 25 '17 at 00:57
  • @jjmontes Thanks, I updated the answer with alternative information on how to install cec-client – HeatfanJohn Feb 27 '17 at 20:59
  • I was able to standby my Samsung TV. I've changed the following setting on the TV: Application/AnyNet+ (HDMI-CEC)/Setup/Auto Turn Off. After I set this to true, TV is going to standby when I send standby 0. – filhit Mar 09 '17 at 22:42
7

you need a TV set that supports CEC (Consumer Electronics Control) protocol over HDMI. it might be a good idea to consult your TV manual or company web site to find details regarding your particular case, also quick search for "TV_model + CEC" might give you a few pointers.

basically, if your TV set is made just a few years ago, there's a good chance you might find how to setup and enable CEC, but for older models chances are not so good.

Samsung CEC implementation is called Anynet+, you may try to search for that too.

lenik
  • 11,541
  • 1
  • 30
  • 37
4

After installing the cec-client per the above answer by HeatfanJohn, I am successfully able to power on/off my Vizio E390i-A1 LED HDTV. I wrote a script for the community:

Create new file via:

mkdir /opt/scripts; vi /opt/scripts/turntv.sh

paste in the following:

#!/bin/bash
#Written by CaptainRewind for the RaspberryPi community
#Modify as needed
#Updated 2014-01-22:
#Now includes full path to cec-client (for cron purposes)
#Also includes "input" command to switch TV Input to RaspberryPi

action=${1}

cecBin="/usr/local/bin/cec-client"

onCommand='echo "on 0" | ${cecBin} -s'
offCommand='echo "standby 0" | ${cecBin} -s'
inputCommand='echo "as" | ${cecBin} -s'

do_on()
{
eval ${onCommand} > /dev/null 2>&1
}

do_off()
{
eval ${offCommand} > /dev/null 2>&1
}

do_input()
{
eval ${inputCommand} > /dev/null 2>&1
}

case ${action} in

        on)
                do_on
                exit 0
                ;;

        off)
                do_off
                exit 0
                ;;

        input)
                do_input
                exit 0
                ;;

        *)
                echo $"Usage: $0 {on|off|input}"
                exit 1
                ;;

esac

Make it executable:

sudo chmod +x /opt/scripts/turntv.sh

Then, create aliases:

vi /home/pi/.bashrc

add the following:

alias tvon="/opt/scripts/turntv.sh on"
alias tvoff="/opt/scripts/turntv.sh off"
alias input="/opt/scripts/turntv.sh input"

Save, exit, logoff/logon (or just source your .bashrc). Now you can turn the TV on or off using "tvon" and "tvoff".

Now you can schedule a nightly job to turn off the TV in case you fall asleep:

crontab -e

Add the following:

00 03 * * * /opt/scripts/turntv.sh off
4

You could make something like a TV remote with an IR LED. There is a tutorial on Adafruit on what you would like to do. Here is another blog post about the same subject.

Note: You will have to use the GPIO pins. I'm not sure if you were looking for a solution that used them or not.

syb0rg
  • 8,188
  • 4
  • 37
  • 51
  • You'd need a IR led, not a sensor, to send out commands to the TV from the Pi. – Gerben Jul 31 '13 at 12:11
  • 1
    @Gerben Edited to fix. Could you remove the down vote now? – syb0rg Jul 31 '13 at 16:15
  • This would make a good hack, but there is no way to get any feedback whether your signals did what they'd suppose to do. Also the order of the options in the source-switching menu depend on its current source. – EDP Aug 27 '15 at 14:56
3

I also wrote a couple of scripts to help with this. I went a bit further than CaptainRewind, though he did a good job. This is just a shell script written to control my TV, so I didn't include anything that it doesn't support (such as volume and channel controls; I'm still looking for a way to do either of these without buying much extra hardware) and it includes setup-specific details like "tv input raspi" being synonymous with "tv input 3" but I left those in so that you can see how to make such customizations for yourself.

You can use it easily If you put it at /usr/local/bin/tv by issuing such commands as "tv on" "tv off" or "tv input 1" or you can just put it in your home directory as tv.sh and use it directly from there.

I hope this helps some people.

#!/bin/bash

must be called with a parameter

tv.sh [command]

will send the associated command to the TV over CEC.

if [ $# -lt 1 ] #Check to see if at least one parameter was supplied then echo "Must be called with the command to send to the television" echo "Commands include on, off, as, and input [1-4]." echo "example: $0 input 1" # $0 is the name of the program echo "For help, use: " $0 " -? " exit 1 fi

case $1 in "-?") cat <<EOF Supported commands include: on, off, status, as, input [source] Where [source] can be a digit from 1 to 4 or an alias (blueray, pc, or raspi) EOF ;;

begin list of commands.

most of these came from http://www.cec-o-matic.com/

more can be added, including proprietary commands.

"on") echo "on 0" | cec-client -s ;; "off") echo "standby 0" | cec-client -s ;; "status") echo "pow 0" | cec-client -s |grep "power status:" ;; "as") echo "as" | cec-client -s ;; "input") if [ $# -ge 2 ] # if there were 2 or more parameters then case $2 in # check the second one # NOTE: These must all be broadcast to work. (2nd nibble must be F) 1|bluray) echo "tx 1F 82 10 00" | cec-client -s ;; 2) echo "tx 1F 82 20 00" | cec-client -s ;; 3|pc|raspi) echo "tx 1F 82 30 00" | cec-client -s ;; 4) echo "tx 1F 82 40 00" | cec-client -s ;; *) echo "Unknown input: $2" ;; esac else echo "input needs a second parameter" echo "usage: " $0 " input [input name]" echo "input name is 1-4, bluray, pc, or raspi" fi ;; # end of the input case

*) echo $1 "is not a recognized parameter. " $0 " -? for a list." ;; esac exit 0

hackerb9
  • 103
  • 3
Tarcas
  • 31
  • 1
0

You might want to retry with a recent version of XBMC (now called Kodi') I've had better luck with my Samsung UE46F6500 tv and OpenELEC (official) Version: 5.95.3 with Kodi, although CEC control is lost after putting my tv to standby or turning my tv back on again:

Standby:

echo "standby 0" | cec-client -s

On:

echo "on 0" | cec-client -s

Change source:

echo "as" | cec-client -s

However, these functions take exclusive control of the CEC interface away from Kodi, so Kodi loses CEC control after any of these commands and the remote stops working. You'll need to restart Kodi to get it working again:

systemctl restart kodi

Kodi (>= 13) has built-in functions CECToggleState, CECActivateSource, CECStandby Some people are working to pass these cec-commands via python using these built-in funcitons, or looking for a possibility to open the cec-hardware in shared mode: http://forum.kodi.tv/showthread.php?tid=207524 and https://discourse.osmc.tv/t/control-tv-on-standby-toggle-using-cec-kodi-built-in-functions/3638/4