Raspberry Pi: Turn on / off TV connected via HDMI-CEC

In cec-client ( libcec), Your Raspberry Pi can control CEC-enabled devices, such as TVs, connected via HDMI. You can turn the TV on or off, switch the active source, and more.
This can be used with any Raspberry Pi version or model, including the original Raspberry Pi and the latest Raspberry Pi4. A possible use case for this situation is to connect to the Raspberry Pi via SSH and send a command to turn on or turn off the TV connected via HDMI-CEC. Alternatively, you can use the command to power on the TV and set the CEC adapter as the active source in the script so that when you open some applications on the Raspberry Pi, a TV connected to it via HDMI-CEC will power on and Switch to Raspberry Pi HDMI source. I believe you will also think of various other use cases.CECOr “Consumer Electronics Control” is a feature of HDMI that allows the use of a remote control to control devices connected via HDMI. For example, CEC is used to get the play / pause button on the remote control to control playback on a device connected via HDMI. Or when playing a video on a Chromecast with the TV off, the TV will automatically power on and switch to the Chromecast source.
Most modern TVs and AV receivers should support HDMI-CEC. However, it is worth noting that you may need to enable CEC in the TV settings of some models. Depending on the device brand, the CEC name may be different. For example, for Samsung TVs, it’s called Anynet +; for Philips, it’s called EasyLink or Fun-Link; for LG, it’s called SimpLink.

In order to be able to turn on (or off) the TV connected to the Raspberry Pi via HDMI, the first step is to install cec-client. Install on Raspbian or other Raspberry Pi-based Debian or Ubuntu-based Linux distributions cec-utils packagecec-client Is part of this package):

sudo apt install cec-utils

On other Linux distributions you have to search cec-client Either cec-utils In the repository, or build libcec From source.
right now cec-utils Installed, let’s scan for available devices on the CEC bus:

echo 'scan' | cec-client -s -d 1

In this command echo 'scan' Send the scan command to cec-client, -s For cec-client to execute a command and exist, and -d 1 Set the log level to 1 (error only) so it doesn’t pollute your terminal with useless information.
Remember the device number and address of the TV (or other device connected to the Raspberry Pi via HDMI-CEC), we will use it later.
Here is an example of running this command on a Raspberry Pi connected to a Samsung TV via HDMI (with CEC support):

$ echo 'scan' | cec-client -s -d 1
opening a connection to the CEC adapter...
requesting CEC bus information ...
CEC bus information
===================
device #0: TV
address:       0.0.0.0
active source: no
vendor:        Samsung
osd string:    TV
CEC version:   1.4
power status:  on
language:      eng

device #1: Recorder 1
address:       1.0.0.0
active source: no
vendor:        Pulse Eight
osd string:    CECTester
CEC version:   1.4
power status:  on
language:      eng

currently active source: unknown (-1)

In this example, the device number 0 versus 0.0.0.0 The address is my Samsung TV and device number 1 versus 1.0.0.0 The address is my Raspberry Pi device.
Now that we know the device number and address, you can use the following command to power on the TV connected to the Raspberry Pi via HDMI-CEC:

echo 'on ' | cec-client -s -d 1

Either:

echo 'on ' | cec-client -s -d 1

Both device numbers (0 Is the Samsung TV in the example above) and the device address (0.0.0.0 Is the address of the Samsung TV device in my example).-d 1 Limit the log level to errors only, and you can use the command without the command, but you will see very long logs that may be useless. E.g:

echo 'on 0' | cec-client -s -d 1

Either:

echo 'on 0.0.0.0' | cec-client -s -d 1

You also need to run as Command, it sets the CEC adapter as the active source (so the TV will switch to the Raspberry Pi HDMI source after the TV is turned on):

echo 'as' | cec-client -s -d 1

Want to turn off the TV (go to standby)? use:

echo 'standby ' | cec-client -s -d 1

Depending on how you use it, you may also need to check the current TV status (is it on or in standby?). This can be done using:

echo 'pow ' | cec-client -s -d 1

To see all the commands that cec-client can send to an HDMI-CEC connected device, use echo h | cec-client -s -d 1:

Available commands:

[tx] {bytes}              transfer bytes over the CEC line.
[txn] {bytes}             transfer bytes but don't wait for transmission ACK.
[on] {address}            power on the device with the given logical address.
[standby] {address}       put the device with the given address in standby mode.
[la] {logical address}    change the logical address of the CEC adapter.
[p] {device} {port}       change the HDMI port number of the CEC adapter.
[pa] {physical address}   change the physical address of the CEC adapter.
[as]                      make the CEC adapter the active source.
[is]                      mark the CEC adapter as inactive source.
[osd] {addr} {string}     set OSD message on the specified device.
[ver] {addr}              get the CEC version of the specified device.
[ven] {addr}              get the vendor ID of the specified device.
[lang] {addr}             get the menu language of the specified device.
[pow] {addr}              get the power status of the specified device.
[name] {addr}             get the OSD name of the specified device.
[poll] {addr}             poll the specified device.
[lad]                     lists active devices on the bus
[ad] {addr}               checks whether the specified device is active.
[at] {type}               checks whether the specified device type is active.
[sp] {addr}               makes the specified physical address active.
[spl] {addr}              makes the specified logical address active.
[volup]                   send a volume up command to the amp if present
[voldown]                 send a volume down command to the amp if present
[mute]                    send a mute/unmute command to the amp if present
[self]                    show the list of addresses controlled by libCEC
[scan]                    scan the CEC bus and display device info
[mon] {1|0}               enable or disable CEC bus monitoring.
[log] {1 - 31}            change the log level. see cectypes.h for values.
[ping]                    send a ping command to the CEC adapter.
[bl]                      to let the adapter enter the bootloader, to upgrade
the flash rom.
[r]                       reconnect to the CEC adapter.
[h] or [help]             show this help.
[q] or [quit]             to quit the CEC test client and switch off all
connected CEC devices.

Source

Sidebar