DIY Smart Home : Google Home + Raspberry Pi

Imagine. You’re snug and warm in your bed, and are about to happily doze off when you realize- you’ve forgotten to turn your lights off. Don’t worry though- it’s possible for you to do so without climbing out of those soft sheets. Here’s how you can set up your room to be infinitely cooler.

What you’ll need

Optional Items

Raspberry Pi Case

Note : Make sure your case does not cover the GPIO ports.

Note : Make sure your case does not cover the GPIO ports. RPi accessories (only needed the first time) HDMI Cable (to display RPi screen) Micro USB Charger (to power RPi) USB Keyboard and Mouse (to control RPi)

If you’re tweaking an existing switchboard : Wire of appropriate thickness, to connect switch ends to relay Screwdriver, to open your switchboard and secure wires in the relay) Insulation tape Safety Gloves



How it works

Step 1 : Controlling switches with Python

The first thing that you need to do is set up the wiring so that you can programmatically flip the switch using our Raspberry Pi. While it’s easy to trigger GPIO ports, they can only emit 5V. In order to close/break a 220V circuit, you need a relay. A relay is an electromagnetic switch operated by a relatively small electric current that can turn on or off a much larger electric current. This is why you’ll hear a clicking sound when the relay is triggered.

1.0 Setting up your RPi

Follow this guide to get your Pi running and then enable SSH.

Alternatively, if you don’t have a keyboard, mouse and a display, check out this headless setup.

Make sure you enable SSH or VNC to access your RPi using another device on the same LAN.

1.1 Triggering GPIO ports with Python

Run the following in a Python Shell on your Pi.

import RPi.GPIO as GPIO pin = 33 # Setup GPIO . setmode ( GPIO . BCM ) GPIO . setwarnings ( False ) GPIO . setup ( pin , GPIO . OUT ) # To turn on pin GPIO . output ( pin , GPIO . HIGH ) # To turn off pin GPIO . output ( pin , GPIO . LOW )

Refer this detailed guide about Turning on an LED with your Raspberry Pi’s GPIO Pins

1.2 Triggering appliances with Python

If you’d like to control a switch in an existing wall socket, grab a screwdriver and a pair of safety gloves. Open the socket and disconnect the switch’s live wire and connect it to NC port of the 1st Relay. Take another wire and connect the NO port of the 1st Relay to the switch’s live input (that you just opened). This lets you break the circuit with the switch when the relay circuit is closed.

Recreate the following circuit using your Pi, Relay and Wires.

Step 2 : Controlling switches with HTTP requests

You need to run a server on your Pi so that we can interact with it remotely. (I’ll be using Flask since it’s fairly simple.)

from flask import Flask , request , jsonify app = Flask ( __name__ ) @app.route ( '/prime' , methods = [ 'POST' ]) def switch (): query = request . json . get ( 'obj' , "NA" ) # Process query and turn appliance on/off with RPi.GPIO if __name__ == '__main__' : app . run ( host = '0.0.0.0' , debug = True , port = 8000 )

You could also develop a Mobile app to send requests to this server with an intuitive UI on your phone or tablet.

to send requests to this server with an intuitive UI on your phone or tablet. However the Flask server can only be accessed locally. Though you can control your switch locally, you can’t do it outside your LAN.

The easiest way to expose your local servers to the public internet is using secure tunnels.

Some options and their free tier features are : ngrok : Provides a new subdomain every time client is run. (It is infeasible to keep updating IFTTT applet’s webhook time and again.) pagekite : Provides custom subdomain for 30 days on verifying email ID.



Exposing your Pi to the Internet

Run Flask server in one terminal ( python server.py ) Initiate port forwarding in the another terminal

python pagekite.py 80 homeboy.pagekite.me

or

./ngrok http 80

Any request sent to your ngrok or pagekite URL should hopefully get tunnelled/forwarded to your flask server.

Step 3 : Controlling switches with the Google Home/Assistant

Now that you have a server to control your smart home remotely, you need to call the endpoint using your Google Home. To do this, you will be using IFTTT ie, If This Then That, which is a free web-based service to create chains of simple conditional statements, called applets. An applet has 2 components : a trigger (which initiates the action) and the action itself. To bridge the Google Home with your Raspberry Pi, you need to create an IFTTT applet. Download the IFTTT app and connect it to the Google account associated wth your Google Home.

Setting the Trigger.

Click on New Applet and then “this” Choose Google Assistant as the trigger service. Select “Say a phrase with a text ingredient”. This text field will tell your server which appliance to trigger and what to do (On/Off) Enter the sample unterance with $ as a placeholder for the query. Click on Create Trigger when you’re done.

Setting the Action

Click on “that” to set the action Choose Webhooks as the action service. This lets you make a HTTP request to an endpoint when the trigger is activated. Paste your ngrok/pagekite URL in the URL field Set method as POST and content type as application/json Enter the sample JSON with TextField as a placeholder for the query replace $ in the Google Assistant trigger.

Troubleshooting

There are quite a few ways above steps could go wrong. Here’s a checklist to help you debug.

Step 1 GPIO ports work : Test with an LED before moving on to a Relay. Port numbers are correct : It’s possible that you’re triggering the wrong GPIO port. Relay inputs are correct : It’s possible that you’re triggering the wrong switch. Loose connections : 🤷‍♂

Step 2 Flask server is running and on the right port : Send requests locally with cURL, Postman or HTTPie. Use nohup to run the server in the background. Add logs to monitor parameters of incoming requests. Tunnel is running and forwarding requests to the right port : Send requests to the right endpoint on the right tunnel URL ( *.ngrok.com/method_path or *.pagekite.com/method_path ). It is also possible that your subdomain may expire or your tunnel may stop responding after some time. Both processes are running simaltaneously

Step 3 IFTTT applet is triggered. : Click on the ⚙️ icon to the top-right of your applet to configure it and enable “Receive notifications when the Applet is run”. It’s helps to add a response message to your Google Assistant trigger service. Request is received at server : Monitor tunnel and Flask logs to make sure request is received.



🚀 Server Code : github.com/py-ranoid/PyCasa

☹️ Can’t get it to work ? Drop a mail

🙋‍♂ Follow me on Github or Connect on LinkedIn

➕ Check out my other posts