Consumer Electronics Control (CEC) is a feature of HDMI: https://en.wikipedia.org/wiki/Consumer_Electronics_Control
This feature has commands to control TV. It means you can control TV with your Raspberry Pi. So I create an Alexa Smart Home Skill to control my TV.
DiagramInstall ces-utils:
$ sudo apt-get install cec-utils
Turn on TV:
$ echo 'on 0' | cec-client -s
opening a connection to the CEC adapter...
DEBUG: [ 125] Broadcast (F): osd name set to 'Broadcast'
DEBUG: [ 126] Open - vc_cec initialised
DEBUG: [ 126] logical address changed to Free use (e)
NOTICE: [ 126] connection opened
DEBUG: [ 127] processor thread started
DEBUG: [ 127] << Broadcast (F) -> TV (0): POLL
DEBUG: [ 127] initiator 'Broadcast' is not supported by the CEC adapter. using 'Free use' instead
TRAFFIC: [ 127] << e0
DEBUG: [ 187] >> POLL sent
DEBUG: [ 187] TV (0): device status changed into 'present'
DEBUG: [ 187] << requesting vendor ID of 'TV' (0)
TRAFFIC: [ 187] << e0:8c
TRAFFIC: [ 382] >> 0f:87:08:00:46
DEBUG: [ 382] TV (0): vendor = Sony (080046)
DEBUG: [ 383] >> TV (0) -> Broadcast (F): device vendor id (87)
....
Turn off TV:
$ echo 'standby 0' | cec-client -s
opening a connection to the CEC adapter...
DEBUG: [ 145] Broadcast (F): osd name set to 'Broadcast'
DEBUG: [ 146] Open - vc_cec initialised
DEBUG: [ 146] logical address changed to Free use (e)
NOTICE: [ 146] connection opened
DEBUG: [ 147] processor thread started
DEBUG: [ 147] << Broadcast (F) -> TV (0): POLL
....
Now you can power on/off TV.
Step 2 - Set up AWS IoTThis sample only use AWS IoT device shadow. Just setup the thing and certificate. Follow the steps in AWS document to add the thing and certificate.
https://docs.aws.amazon.com/iot/latest/developerguide/iot-gs.html
Do not forget to download certificates. After creating the Thing, add device shadow state.
{
"desired": {
"command": "tv_off",
"counter": 0
}
}
Step 3 - Set up Raspberry PiConnect HDIM cable to TV. Clone sample source.
$ cd /home/pi
$ git clone https://github.com/sparkgene/alexa-tv-controller.git
$ cd alexa-tv-controller/raspberrypi
Copy download certs to Raspberry Pi and store it in /home/pi/alexa-tv-controller/raspberrypi/certs.
Rewrite shadow.js to your AWS IoT endpoint.
(host: "your-endpoint.iot.ap-northeast-1.amazonaws.com")
var shadowName = "tv-controller"
var thingShadows = awsIot.thingShadow({
keyPath: "/home/pi/alexa-tv-controller/raspberrypi/certs/private.pem.key",
certPath: "/home/pi/alexa-tv-controller/raspberrypi/certs/certificate.pem.crt",
caPath: "/home/pi/alexa-tv-controller/raspberrypi/certs/ca.pem",
clientId: "tv-controller",
region: "ap-northeast-1",
host: "your-endpoint.iot.ap-northeast-1.amazonaws.com"
});
Install client libraries:
$ cd /home/pi/alexa-tv-controller/raspberrypi
$ sh setup.sh
Run the client:
$ /usr/bin/node shadow_client.js
connected
registered
received accepted on tv-controller: {"state":{"desired":{"command":"tv_off","counter":1517043288},"reported":{"counter":1517043288,"command":"tv_off"}},"metadata":{"desired":{"command":{"timestamp":1517043289},"counter":{"timestamp":1517043289}},"reported":{"counter":{"timestamp":1517043293},"command":{"timestamp":1517043293}}},"version":26,"timestamp":1517046963}
counter:1517043288
no change do nothing
Test is working good. You can change the status at AWS IoT shadow console manually.
"command": "tv_on" turn on TV.
"command": "tv_off" turn off.
Step 4 - Create Smart Home SkillSmart home skill use v3 payload: https://developer.amazon.com/docs/smarthome/steps-to-build-a-smart-home-skill.html
Use the sample lambda function: https://github.com/sparkgene/alexa-tv-controller/blob/master/lambda_function/lambda_function.py
Change the friendlyName to "living TV".(line 63)
"friendlyName": "living TV",
Lambda needs permission to use AWS IoT. Create a role as follows:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*"
},
{
"Effect": "Allow",
"Action": [
"iot:GetThingShadow",
"iot:UpdateThingShadow"
],
"Resource": "*"
}
]
}
Step 5 - Use it from EchoNow you can use your smart home skill from the Echo. Enable the skill and say "Alexa, turn on living room TV".
Comments
Please log in or sign up to comment.