在本教程中,我將展示如何使用HDMI-CEC和樹莓派來控制電視。
HDMI-CEC是一種特殊的協議,用於電視與另一個設備通過HDMI電纜進行通信。該協議允許電視控制另一個設備,同時也允許該設備控制電視。
大多數現代電視實現了對這一協議的支持,但通常需要在電視的設置中啟用。搜索電視型號應該可以找到這個設置的名稱。
使用HDMI-CEC協議,可以使用樹莓Pi以各種姿勢控制電視,如關閉和打開或改變音量。
設備清單
以下是所有的設備清單。
建議
樹莓Pi 1、2、3或4
SD卡
電源供
以太網線或WiFi(Pi 3和4內置了WiFi)
HDMI線
可選
樹莓派外殼
在這個使用HDMI-CEC的教程中,我在樹莓Pi 4上運行了最新版本的Raspbian Buster。
本教程應該也適合舊版Raspberry Pi和舊版Raspbian操作系統。
將 cec-client 安裝到Raspberry Pi 上
在本節中,我將展示在Raspberry Pi上安裝cec-client軟件的簡單步驟。
cec-client是在Raspberry Pi上使用的軟件包,通過HDMI-CEC協議來控制設備。
1. 第一個任務是更新樹莓派上的軟件包列表,以及升級當前安裝的軟件包。
可以運行以下兩個命令來實現。
sudo apt update sudo apt upgrade
第一條命令將更新軟件包列表。第二條命令將升級已安裝的軟件包。
2. 下一步是安裝cec-utils軟件包。這個軟件包包含了本教程中稍后要用到的cec-client軟件。
可以通過運行以下命令將這個軟件包安裝到Raspberry Pi上。
sudo apt install cec-utils
使用這個軟件包提供的工具,能夠使用CEC給連接到Raspberry Pi的HDMI電纜發送命令。
使用樹莓派通過HDMI-CEC與電視進行交互。
在本節中,我將展示使用 cec-client 軟件與其他設備交互的各種方法。
第一個是使用echo和管道(|)。使用這兩樣東西將命令直接傳遞給 cec-client 軟件,而不需要啟動它。
此外,在cec-client軟件中同時使用-s和-d選項。
選項告訴軟件,發出一條命令,軟件啟動,發出命令,然后立即退出。
選項 -d 1 設置了軟件的調試級別。將其設置為1,cec-client軟件將只顯示錯誤。
此設置可減少沒控制台的日志量。
樹莓派掃描HDMI-CEC設備。
1. 首先,需要做的是掃描現有的設備,這些設備要具有CEC功能。
通過在Raspberry Pi上運行以下命令來掃描支持HDMI-CEC的設備。
echo 'scan' | cec-client -s -d 1
2. 從這個命令中,應該看到Raspberry Pi現在可以訪問的設備列表。
需要識別想與之交互的設備。通常,"os string: "和 "vendor: "字段可以識別想與之交互的設備。
一旦確定了正確的設備,請記下 "address
:"或設備編號。
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: Sony osd string: TV CEC version: 1.4 power status: standby 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 device #4: Playback 1 address: 3.0.0.0 active source: no vendor: Sony osd string: PlayStation 4 CEC version: 1.3a power status: standby language: ???
請注意,本例中的 "device #1: Recorder 1
"是Raspberry Pi自己的CEC連接,所以可以忽略它。
3. 例如,如果想控制 "索尼電視",可以看到,設備號是 "0",設備的地址是 "0.0.0.0"。
有了設備號或設備地址,就可以向它發送命令。
通過HDMI-CEC發送 "開啟 "命令。
使用 cec-client 開啟設備是一個相對簡單的過程。
echo 'on <DEVICEADDRESS>' | cec-client -s d 1
從上面可以看出,需要做的就是發送on命令,后面跟着設備地址或設備號。
通過HDMI-CEC打開設備的例子
例如,如果想打開地址為 "0.0.0.0 "的索尼電視,使用下面的命令。
echo 'on 0.0.0.0' | cec-client -s -d 1
通過HDMI-CEC發送 "待機 "命令
如果想用Raspberry Pi關閉電視(使其進入待機狀態),也可以使用HDMI-CEC輕松完成。
echo 'standby <DEVICEADDRESS>' | cec-client -s d 1
要讓電視進入待機狀態,只需要發送 "待機 "命令,然后是設備的地址或號碼。
通過HDMI-CEC關閉設備示例
使用這個命令是比較簡單。
要讓索尼電視進入待機狀態,需要做的就是發送 "standby",后面跟着設備地址 "0.0.0.0"。
echo 'standby 0.0.0.0 | cec-client -s -d 1
通過HDMI-CEC獲取電源狀態
甚至可以利用樹莓派上的HDMI-CEC來檢索設備的電源狀態。
echo 'pow <DEVICEADDRESS>' | cec-client -s d 1
使用pow命令,可以了解所連接的設備目前是處於開機狀態還是待機狀態。
這個命令可以決定是否需要打開或關閉電視的電源,特別是在遠程管理設備時。
通過HDMI-CEC獲取電源狀態示例
和大多數HDMI-CEC命令一樣,利用這個命令比較容易。需要做的就是參考pow后面的設備地址。
echo 'pow 0.0.0.0' | cec-client -s -d 1
如果設備處於待機狀態,會看到在終端上出現類似下面的東西。
opening a connection to the CEC adapter... power status: standby
檢索其他的CEC-客戶端命令
如果想檢查可以使用HDMI-CEC從Raspberry Pi向電視發出的其他命令,可以使用以下命令。
echo 'h' | cec-client -s -d 1
這個命令的作用是檢索cec-client軟件知道如何處理的可用命令。
從這個命令中,你應該得到一個命令列表,如下圖所示。
================================================================================ 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. ================================================================================
到這里,你應該已經知道了如何在Raspberry Pi上使用cec-client來控制支持HDMI-CEC協議的設備。