I have been thinking about doing some DIY home automation projects for a long time. Now that I have a Google Home Mini (thanks to Pixel 2, I got it for free!!) and RaspberryPi 3 model B (won in a Hackathon!!), I decided to do something real.
I have adapted this project from Instructables to my needs. Thanks to call_me_kyle.
Setting up RaspberryPi (Pi)
It is pretty much straightforward to install Raspbian OS and connect Pi to WiFi (if I can do it easily, so can everyone :D). It is better to setup SSH login on Pi so that you don’t have to have a separate display for the Pi. There are lot of resource available to do these things. Google is your go-to guy!! Install the latest version of node.js on Pi. I followed this link. Skip straight to “Install node.js” part.
Bringing a web server up
Clone the repository in https://github.com/krpeacock/google_home_starter and follow “Getting Started” in “Google Home Starter Guide“. When I set PORT=80, server did not start. Must be due to restricted privileges. So, I set the PORT as 8080. I named my Pi as raspberrypi. My POST request would be,
curl -X POST "http://raspberrypi/API/switches/sw1?password=homeauto"
If you run the above curl command multiple times, you will see the state of the switch toggle between on and off
{"id":"sw1","state":"off","name":"Kyle's Lamp"}
# Running again
{"id":"sw1","state":"on","name":"Kyle's Lamp"}
If you see the above response, then your web server is functioning as expected.
Exposing the web server to outside world
I used ngrok for tunneling the web server running in my Pi locally to be accessible outside the LAN. I used a linux screen session in order to keep the ngrok tunnel running even if I log out from SSH.
After installing ngrok, run
screen ./ngrok http 8080
You should see a display like this,
ngrok by @inconshreveable (Ctrl+C to quit)
Session Status online
Account Prasanth Mathialagan (Plan: Free)
Version 2.2.8
Region United States (us)
Web Interface http://127.0.0.1:4040
Forwarding http://b2c1ac64.ngrok.io -> localhost:8080
Forwarding https://b2c1ac64.ngrok.io -> localhost:8080
Connections ttl opn rt1 rt5 p50 p90
51 0 0.00 0.00 0.04 0.22
HTTP Requests
-------------
POST /API/switches/sw1 200 OK
POST /API/switches/sw1 200 OK
POST /API/switches/sw1 200 OK
Don’t hit Ctrl+C as it will stop the tunnel. Just close the tab. You can open a new tab and check if the screen is still active by running
screen -list
You should see your screen listed,
There is a screen on:
2081.pts-1.raspberrypi (25/02/18 19:59:22) (Attached)
1 Socket in /run/screen/S-pi.
Now you can replace the hostname/IP address in the POST request with ngrok URL and try running curl. Voila!! Your local web server can now take requests from anywhere around the world!! Fascinating!!
curl -X POST "http://b2c1ac64.ngrok.io/API/switches/sw1?password=homeauto"
Watch the magic with IFTTT
IFTTTÂ is the brain of this project. In IFTTT, we can define applets that will run defined action for the configured trigger.
In our project,
trigger – Command from Google Assistant
action – POST request to URL
Create an account in IFTTT if you don’t have one already. Connect Google Assistant and Webhooks services. Create a new applet. For the “+this” part of the applet, choose Google Assistant service and select “Say a simple phrase” as trigger. In the trigger fields, fill the command you want to say to Google assistant and what your assistant should respond back.
For the “+that” part of the applet, choose Webhooks as action service and select “Make a web request” as action. Fill URL and other details.
Your own applet is ready!!
If you say the magic phrase “Turn on my bed lamp” to your Google Home, you should hear your assistant responding with “Turning on your bed lamp“. You should also see a POST request to the URL in ngrok console. We are almost there!! Hang on!!
Connecting the lights
We cannot directly connect Pi’s GPIO pins to 120V electrical lights. We need a relay. Relays are really cheap in amazon and there are lot of tutorials available online on how to connect relay, Pi and switches. Since I did not want to get my hands dirty with handling 120V wirings, I went for a bit expensive IoT relay. The upside is you just need to connect a jumper wire from Pi to the relay and it works out like a charm.
I was using GPIO pin 35 (Pin number as per the board) for connecting the Pi to Relay. I had to make some changes to the python files in the project. If you run these python files, you will see your light turn on/off if it is connected.
sw1_on.py
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
# Setting the mode to board based pin numbering
GPIO.setmode(GPIO.BOARD)
GPIO.setup(35, GPIO.OUT)
GPIO.output(35, GPIO.HIGH)
sw1_off.py
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
# Setting the mode to board based pin numbering
GPIO.setmode(GPIO.BOARD)
GPIO.setup(35, GPIO.OUT)
GPIO.output(35, GPIO.LOW)
GPIO.cleanup()
That is all you need to do!! Your DIY home automation project is ready!! Just say the magic phrase and see your lights go on/off. Since the command turns the light on and off on subsequent calls, you might want to set the command to something like “Toggle my bed lamp”
Resources
http://www.instructables.com/id/Google-Home-Raspberry-Pi-Power-Strip/
http://thisdavej.com/beginners-guide-to-installing-node-js-on-a-raspberry-pi/
https://github.com/krpeacock/google_home_starter
https://www.mattcutts.com/blog/a-quick-tutorial-on-screen/
https://ngrok.com/