Amazon Echo can control smart home devices (Philip Hue lights, Belkin’s WeMo switch system). But those things are pretty expensive.
If you have Infrared remote control lights (or other), this project brings these device to smart home device.
How does it work?- Talk to Amazon Echo "Alexa, tell pi home lights on"
- Alexa Voice Service invoke Amazon Lambda function
- Lambda function update AWS IoT device shadow
- Raspberry Pi recive shadow update
- Send signal to Infrared remote control devices
Connect IR reciver LED signal to GPIO 18 and IR LED to GPIO 17.
Step2 - Install and configure lirc packagesudo apt-get install lirc
Edit /boot/config.txt
# Uncomment this to enable the lirc-rpi module
dtoverlay=lirc-rpi,gpio_in_pin=18,gpio_out_pin=17
Reboot
sudo shutdown -r now
Check device file is created
ls -l /dev/lirc*
crw-rw---- 1 root video 244, 0 Aug 22 23:17 /dev/lirc0
Test receiving IR remote
sudo /etc/init.d/lirc stop
mode2 -d /dev/lirc0
Point your remote controller and push any button. If you see output like following, it means setup is good.
space 1385566
pulse 770
space 876
pulse 1590
space 874
pulse 1589
space 1681
pulse 783
Step3 - Create remote control commandsClone sample source
cd /opt
git clone https://github.com/sparkgene/alexa_home_control.git
cd alexa_home_control/raspberrypi
Capture remote control signals.
sudo /etc/init.d/lirc stop
mode2 -d /dev/lirc0 | tee lights_on.txt
(enter Ctrl + C to stop capture)
Parse signal file:
python parse.py lights_on.txt
770 876 1590 874 1589 1681 783 1683 782 1680 784 1683 780 ....
Repeat this step for the button you want to use on Raspberry Pi.
Create /etc/lirc/lircd.conf and paste parsed signal. If the signal is longer than 80 characters, write it to the next line.
begin remote
name living_light
flags RAW_CODES
eps 30
aeps 100
gap 200000
toggle_bit_mask 0x0
begin raw_codes
name light_all
770 876 1590 874 1589 1681 783 1683 782 1680 784
1683 780 ....
name light_off
778 875 1592 866 1600 1683 779 1713 751 865 1599
865 1601 ....
end raw_codes
end remote
"Living_light" is the name of remote controller. If you changed this name, rewrite the `raspberrypi/iot_shadow.js ` .
https://github.com/sparkgene/alexa_home_control/blob/master/raspberrypi/iot_shadow.js#L28
sudo /etc/init.d/lirc start
# list remote controllers
$ irsend LIST '' ''
irsend: living_light
# list commands
$ irsend LIST living_light ''
irsend: 0000000000000001 light_all
irsend: 0000000000000002 light_off
# send signal
$ irsend SEND_ONCE living_light light_all
"light_all" and "light_off" is the name for the parsed signal. In the sample source, these name are linked with lambda function.
Test commands
If your config is correct, Raspberry Pi becomes a remote controller.
Step 4 - Setup 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.
http://docs.aws.amazon.com/ja_jp/iot/latest/developerguide/iot-gs.html
Do not forget to download certificates. After creating the Thing, add device shadow state.
{
"desired": {
"command": "none",
"counter": 0
}
}
Step 5 - Setup Amazon Lambda functionThis Lambda function update AWS IoT device shadow. So attach a role which have correct permission like following.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "*"
},
{
"Effect": "Allow",
"Action": [
"iot:Receive",
"iot:GetThingShadow",
"iot:UpdateThingShadow"
],
"Resource": "*"
}
]
}
Create lambda function.
https://github.com/sparkgene/alexa_home_control/blob/master/alexa_home_controller/lambda_function.py
Step 6 - Setup AWS IoT client on Raspberry PiSSH to Raspberry Pi again. Copy certificate files which download in Step 4. Certificate file to /opt/alexa_home_control/raspberrypi/certs/certificate.pem.crt
private key to /opt/alexa_home_control/raspberrypi/certs/private.pem.key
Run setup:
cd /opt/alexa_home_control/raspberrypi
sh ./setup.sh
You can see the log at /var/log/daemon.log
Step 7 - Create Alexa SkillAdd a new skill with following samples.
Skill Type:
Custom Interaction Model
Intent Schema:
https://github.com/sparkgene/alexa_home_control/blob/master/alexa_skill/intent_schema.json
Custom Slot Types
Type: LIST_OF_FAN_ACTION
Values: https://github.com/sparkgene/alexa_home_control/blob/master/alexa_skill/slot_LIST_OF_FAN_ACTION
Sample Utterances
https://github.com/sparkgene/alexa_home_control/blob/master/alexa_skill/utterances
Endpoint
Add your Lambda function ARN.
Account Linking
No
Now you can test your skill. Fix the Lambda function APP_ID variable with your skill application id before testing.
Comments