树莓派如何控制电视机,在Raspberry Pi上安装HDMI

您所在的位置:网站首页 如何控制家中的电视机网络开关 树莓派如何控制电视机,在Raspberry Pi上安装HDMI

树莓派如何控制电视机,在Raspberry Pi上安装HDMI

#树莓派如何控制电视机,在Raspberry Pi上安装HDMI| 来源: 网络整理| 查看: 265

在本教程中,我将展示如何使用HDMI-CEC和树莓派来控制电视。

Raspberry-Pi-HDMI-CEC-Thumbnail.jpg

HDMI-CEC是一种特殊的协议,用于电视与另一个设备通过HDMI电缆进行通信。该协议允许电视控制另一个设备,同时也允许该设备控制电视。

大多数现代电视实现了对这一协议的支持,但通常需要在电视的设置中启用。搜索电视型号应该可以找到这个设置的名称。

使用HDMI-CEC协议,可以使用树莓Pi以各种姿势控制电视,如关闭和打开或改变音量。

设备清单

以下是所有的设备清单,点击链接可以直达特别优惠购买。

建议 ? 树莓派 ? 高速SD卡 ? 树莓派4B电源 ? 以太网线或WiFi (Pi 3内置WiFi) ? HDMI 可选 ? 树莓派外壳 ? USB鼠标 ? USB键盘

在这个使用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 ' | 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 ' | 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 ' | 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协议的设备。

如果在树莓Pi上使用HDMI-CEC有任何问题,欢迎在下方留言。

平均: 5 / 5. votes: 1

到目前为止还没有投票!成为第一位评论此文章。

欢迎转载,请留下出处链接:Labno3 » 树莓派如何控制电视机,在Raspberry Pi上安装HDMI-CEC



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3