Wednesday, November 11, 2015

Voice Control Home Automation via Amazon Echo and Siri HomeKit - DIY

Voice Control Home Automation via Amazon Echo and Siri HomeKit - DIY


This is a quick start tutorial on setting up the Raspberry Pi 2 as a Home Automation System with Voice Control from Amazon Echo (Alexa) or Apple HomeKit (Siri). This is not a comprehensive tutorial, but should get you started.


Last Updated: 4/24/2016

REQUIREMENT:

Raspberry Pi 2 or Pi 3
8GB or larger microSD Card
Aeon Labs Z-Wave Z-Stick - Series 2 OR Gen5
Aeon Labs - Z-Wave Smart Energy Switch

OPTIONAL:
Logitech Harmony Home Hub
Amazon Echo or Amazon Dot

LET'S START:

Download Raspbian Jessie
Write the Raspbian Jessie Image to MicroSD Card
Insert MicroSD Card in Raspberry Pi 2 and Boot.
Connect to Pi using SSH (pi/raspberry)
Expand Filesystem - Ensures that all of the SD card storage is available to the OS
#Expand Filesystem and Reboot
sudo raspi-config

#Reboot Required and Important

#Initial Update and required packages
#All one line - cut/paste recommend
#Go get some coffee
sudo apt-get update && sudo apt-get -y upgrade && sudo apt-get -y install python2.7 libavahi-compat-libdnssd-dev git unzip curl subversion build-essential usbutils psmisc htop samba-common-bin samba php5 php5-cli php5-curl wicd-curses libudev-dev

###################################
#SAFE CUT/PASTE FROM THIS POINT 
#TO FIRST REBOOT
###################################

#apache get installed somewhere, so stop and disable it
sudo /etc/init.d/apache2 stop
sudo update-rc.d -f apache2 remove

#Setup SAMBA
#No password, Everyone has read/write access to \\<IPAddress>\root
cd /etc/samba
sudo mv smb.conf smb.conf.org
sudo wget --no-check-certificate http://dl.dropbox.com/u/42238/debian/openhab/smb.conf
sudo /etc/init.d/samba restart

#OPENHAB INSTALL
sudo mkdir -p /opt/openhab/addons_all
cd /opt/openhab

#Download current release
sudo wget https://bintray.com/artifact/download/openhab/bin/distribution-1.8.2-runtime.zip
sudo wget https://bintray.com/artifact/download/openhab/bin/distribution-1.8.2-addons.zip
sudo wget https://bintray.com/artifact/download/openhab/bin/distribution-1.8.2-demo.zip

#unzip
sudo unzip distribution-1.*-runtime.zip
sudo unzip distribution-1.*-addons.zip -d /opt/openhab/addons_all
sudo unzip -n distribution-1.*-demo.zip

#delete
sudo rm distribution-1.*-runtime.zip
sudo rm distribution-1.*-addons.zip
sudo rm distribution-1.*-demo.zip

#Addons (aka bindings) is what make OpenHAB so powerful
#To install Addons, copy them into the addons folder
sudo cp addons_all/org.openhab.action.harmonyhub*.jar addons
sudo cp addons_all/org.openhab.binding.harmonyhub*.jar addons
sudo cp addons_all/org.openhab.binding.exec*.jar addons
sudo cp addons_all/org.openhab.binding.http*.jar addons
sudo cp addons_all/org.openhab.binding.networkhealth*.jar addons
sudo cp addons_all/org.openhab.binding.ntp*.jar addons
sudo cp addons_all/org.openhab.binding.wol*.jar addons
sudo cp addons_all/org.openhab.binding.zwave*.jar addons
sudo cp addons_all/org.openhab.binding.systeminfo*.jar addons
sudo cp addons_all/org.openhab.io.harmonyhub*.jar addons
sudo cp addons_all/org.openhab.persistence.exec*.jar addons

#SIGAR for OpenHAB Systeminfo Binding
#https://github.com/openhab/openhab/wiki/Systeminfo-Binding
sudo mkdir -p /opt/openhab/lib
sudo wget https://groups.google.com/group/openhab/attach/ab7030271be23f05/sigar-raspbian.zip?part=0.1 -O /tmp/sigar-raspbian.zip
sudo unzip /tmp/sigar-raspbian.zip -d /tmp
sudo cp /tmp/sigar-raspbian/lib/* /opt/openhab/lib
sudo rm /tmp/sigar-raspbian.zip

#AMAZON ECHO BRIDGE INSTALL
#Go here for latest - https://github.com/armzilla/amazon-echo-ha-bridge/releases

sudo mkdir /opt/amazon-echo-bridge
cd /opt/amazon-echo-bridge
sudo wget --no-check-certificate https://github.com/armzilla/amazon-echo-ha-bridge/releases/download/v0.2.1/amazon-echo-bridge-0.2.1.jar


#HABMIN1 INSTALLATION
cd /opt/openhab
sudo wget https://github.com/cdjackson/HABmin/releases/download/0.1.3-snapshot/habmin.zip
sudo unzip habmin.zip
sudo rm habmin.zip
#delete old zwave binding
sudo rm addons/org.openhab.binding.zwave-1.5.0-SNAPSHOT.jar

#HABMIN2 INSTALLATION - requires HABMIN1
#HABMIN2 no longer works with OpenHAB 1 (only OpenHAB 2)
cd /opt/openhab/addons
sudo wget https://github.com/cdjackson/HABmin/blob/master/addons/org.openhab.io.habmin-1.7.0-SNAPSHOT.jar
sudo wget https://github.com/cdjackson/HABmin2/releases/download/0.0.15/org.openhab.ui.habmin_2.0.0.SNAPSHOT-0.0.15.jar
sudo chmod 755 *

sudo mkdir -p /opt/openhab/webapps/habmin2
cd /opt/openhab/webapps/habmin2
sudo wget https://github.com/cdjackson/HABmin2/releases/download/0.0.15/HABmin2-0.0.15-release.zip
sudo unzip HABmin2-0.0.15-release.zip
sudo rm HABmin2-0.0.15-release.zip

#OPENHAB CONFIG
#mail, security, zwave, harmonyhub
cd /opt/openhab/configurations
sudo cp openhab_default.cfg openhab.cfg

#SAMPLE OPENHAB ITEMS
cd /opt/openhab/configurations/items
sudo wget --no-check-certificate http://dl.dropbox.com/u/42238/debian/openhab/default.items

#SAMPLE OPENHAB SITEMAPS
cd /opt/openhab/configurations/sitemaps
sudo wget --no-check-certificate http://dl.dropbox.com/u/42238/debian/openhab/default.sitemap
sudo wget --no-check-certificate http://dl.dropbox.com/u/42238/debian/openhab/homekit.sitemap

#Z-WAVE Z-STICK OPENHAB CONFIGURATION
#Plug in Z-Stick
#Assuming Z-Wave Stick is attached to ttyUSB0
sudo sed -i 's^#zwave:port=^zwave:port=/dev/ttyUSB0^' /opt/openhab/configurations/openhab.cfg

#CUSTOM SCRIPTS
sudo mkdir /opt/openhab/custom_scripts

#Shutdown Windows PC Script
cd /opt/openhab/custom_scripts
sudo wget  --no-check-certificate http://dl.dropbox.com/u/42238/debian/openhab/custom_scripts/turnoffWindowsComputer.sh
sudo chmod +x turnoffWindowsComputer.sh

#PHP-FINDMYIPHONE
cd /opt/openhab/custom_scripts
sudo git clone git://github.com/albeebe/PHP-FindMyiPhone
sudo cp PHP-FindMyiPhone/class.findmyiphone.php .

sudo wget --no-check-certificate http://dl.dropbox.com/u/42238/debian/openhab/custom_scripts/listiclouddevices.php
sudo wget --no-check-certificate http://dl.dropbox.com/u/42238/debian/openhab/custom_scripts/turnoniphone.php
sudo wget --no-check-certificate http://dl.dropbox.com/u/42238/debian/openhab/custom_scripts/turnoffiphone.php

#NODEJS for SIRI HomeKit Bridge

#Remove conflicting nodejs
sudo apt-get -y remove nodejs

#Install nodejs
cd /tmp
sudo wget http://node-arm.herokuapp.com/node_latest_armhf.deb
sudo dpkg -i node_latest_armhf.deb
sudo rm node_latest_armhf.deb

#OpenHAB HomeKit Bridge
cd /opt
sudo git clone git://github.com/htreu/OpenHAB-HomeKit-Bridge
cd OpenHAB-HomeKit-Bridge

#This will take a while
cd /opt/OpenHAB-HomeKit-Bridge
sudo npm install -g npm@3.3.12

#Start OpenHAB on boot
cd /etc/init.d/
sudo wget --no-check-certificate http://dl.dropbox.com/u/42238/debian/openhab/etc/init.d/openhab
sudo chmod a+x openhab
sudo update-rc.d openhab defaults

#Home Kit Startup Script Setup
cd /opt/OpenHAB-HomeKit-Bridge
sudo cp start.sh.template start.sh
sudo chmod +x start.sh

#Amazon-Echo-Bridge Script Setup
cd /opt/amazon-echo-bridge/
sudo wget --no-check-certificate http://dl.dropbox.com/u/42238/debian/openhab/echobridgestart.sh
sudo chmod +x echobridgestart.sh

#Start bridges on boot

#delete 'exit 0' (backup rc.local also)
sudo sed -i.bak '/exit 0/d' /etc/rc.local

echo 'sleep 10' | sudo tee --append /etc/rc.local
echo 'sudo /opt/amazon-echo-bridge/echobridgestart.sh &' | sudo tee --append /etc/rc.local
echo 'sleep 60 #Give OpenHAB Time to Start' | sudo tee --append /etc/rc.local
echo 'sudo /opt/OpenHAB-HomeKit-Bridge/start.sh &' | sudo tee --append /etc/rc.local
echo 'exit 0' | sudo tee --append /etc/rc.local

###################################
#END CUT/PASTE 
###################################

#REBOOT
sudo reboot

#Configure Amazon Echo Bridge at
#http://IPADDRESS:8081/configurator.html
#Add sample items
cd /tmp
wget --no-check-certificate http://dl.dropbox.com/u/42238/debian/openhab/custom_scripts/iphone.json
wget --no-check-certificate http://dl.dropbox.com/u/42238/debian/openhab/custom_scripts/smartswitch.json
wget --no-check-certificate http://dl.dropbox.com/u/42238/debian/openhab/custom_scripts/windowspc.json
curl -H "Content-Type: application/json" --data @iphone.json http://localhost:8081/api/devices
curl -H "Content-Type: application/json" --data @smartswitch.json http://localhost:8081/api/devices
curl -H "Content-Type: application/json" --data @windowspc.json http://localhost:8081/api/devices

#FIND MY IPHONE SETUP
#CONFIGURE SAMPLE SCRIPTS
#Edit listiclouddevices.php with your iCloud username (email) and password

cd /opt/openhab/custom_scripts
sudo nano listiclouddevices.php


#Run it to find your deviceID
#DeviceID will be something like Eu4EGWK1Yt5+2JtKM7YNA++hxNtDw4ZzZuwT9BLxYcL2d7FaULCJMuHYVNSUzmXV<
php ./listiclouddevices.php

#Edit turnoniphone.php, turnoffiphone.php
#with username, password and deviceID
sudo nano turnoniphone.php
sudo nano turnoffiphone.php

#Run scripts to test
php ./turnoniphone.php
php ./turnoffiphone.php


#TURN OFF COMPUTER SCRIPT SETUP
#edit script with IPAddress, Username and Password
sudo nano turnoffWindowsComputer.sh

#Turn on your computer and test script
./turnoffWindowsComputer.sh
#Computer Power On will be handled via WOL Binding
#https://github.com/openhab/openhab/wiki/Wake-on-LAN-Binding-(WoL)

#Test via OpenHAB WebApp - http://IPADDRESS:8080/openhab.app

Download the OpenHAB iOS/Android app.

Ask Alexa to discover your devices - "Alexa, discover devices". Add additional devices via http://IPADDRESS:8081/configurator.html.

For HomeKit Siri setup, download Elgato Eve on iOS device
https://itunes.apple.com/us/app/elgato-eve/id917695792?mt=8

Launch Elgato Eve->Add Accessory
Under "SELECT ACCESSORY" you should see "OpenHAB HomeKit Bridge". Select it.
Select "Add to xxx" (xxx being whatever name you used)
Ignore warning about the accessory not being certified - Add Anyway
Select "Enter code manually"
Type in "031-45-154"


To reset HomeKit and start over, delete Elgato Eve app and then go into Settings->Privacy->HomeKit and select "Reset HomeKit Configuration"


Hardware:

Amazon Echo - http://www.amazon.com/dp/B00X4WHP5E/?tag=blogqnologyco-20
Voice Remote for Amazon Echo - http://www.amazon.com/dp/B00L9EPT8O/?tag=blogqnologyco-20
Logitech Harmony Home Hub - http://www.amazon.com/dp/B00N3RFC4G/?tag=blogqnologyco-20
Raspberry Pi 2 - http://www.amazon.com/dp/B00T2U7R7I/?tag=blogqnologyco-20
Aeon Labs Z-Wave Z-Stick Series 2 USB Dongle - http://www.amazon.com/dp/B003MWQ30E/?tag=blogqnologyco-20
(Newer version - Buy this version instead of the above) Aeon Labs Aeotec Z-Wave Z-Stick, Gen5 Z-Wave Plus - ZW090 - http://www.amazon.com/dp/B00X0AWA6E/?tag=blogqnologyco-20
Wink Connected Home Hub - http://www.amazon.com/dp/B00PV6GAI4/?tag=blogqnologyco-20
GE Link Smart Connected LED Light Bulb - http://www.amazon.com/dp/B00NOL16K0/?tag=blogqnologyco-20
GE Z-Wave On/Off/Dim Dimmer Switch (45612  - no neutral) - http://www.amazon.com/dp/B006LQFHN2/?tag=blogqnologyco-20
RGB LED Light Strip 5 meter with remote and power supply - http://www.amazon.com/dp/B00ASHQQKI/?tag=blogqnologyco-20

Harmony Hub Control for RGB LED discussion - http://forums.logitech.com/t5/Harmony-Remotes/RGB-Controller-44keys-exist-in-Harmony-DB/td-p/423325

Not shown in video, but I have everything running through this so I can monitor energy usage. I would also be able to determine if my TV is on by the amount of watts being drawn.
Aeon Labs - Z-Wave Smart Energy Switch - http://www.amazon.com/dp/B007UZH7B8/?tag=blogqnologyco-20

Software:

Raspbian Jessie -  https://www.raspberrypi.org/downloads/raspbian/
OpenHab - http://www.openhab.org/getting-started/index.html
Amazon Echo Bridge - https://github.com/armzilla/amazon-echo-ha-bridge
OpenHab HomeKit Bridge - https://github.com/htreu/OpenHAB-HomeKit-Bridge
PHP-FindMyiPhone - https://github.com/albeebe/PHP-FindMyiPhone

=======
Credit/References:

https://www.reddit.com/r/homeautomation/comments/377onm/extending_amazon_echo_voice_control_to_existing/

Getting Started with OpenHAB Home Automation on Raspberry Pi
http://www.makeuseof.com/tag/getting-started-openhab-home-automation-raspberry-pi/

[HOW TO] CONTROL YOUR OPENHAB ITEMS USING THE AMAZON ECHO - http://tinsley.io/2015/06/control-your-items-using-the-amazon-echo-openhab/

OpenHAB Community - https://community.openhab.org/