DIY Smart Home : Google Home + Raspberry Pi

DIY Smart Home : Google Home + Raspberry Pi

- 7 mins

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

  1. Google Home (Mini/Regular/Max) (Buy in India)
  2. Raspberry Pi (Any Model) (Buy in India)
  3. Female-to-Female Jumper Wires (Buy in India)
  4. 5v Relay Module (Buy in India)
  5. (Wireless) LAN with Internet Access

Optional Items

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

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

# To turn on pin

# To turn off pin

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

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__': '',debug=True,port=8000)

Exposing your Pi to the Internet

  1. Run Flask server in one terminal (python
  2. Initiate port forwarding in the another terminal
    python 80
    ./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.

  1. Click on New Applet and then “this
  2. Choose Google Assistant as the trigger service.
  3. 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)
  4. Enter the sample unterance with $ as a placeholder for the query.
  5. Click on Create Trigger when you’re done.


Setting the Action

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



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

🚀 Server Code :
☹️ Can’t get it to work ? Drop a mail
🙋‍♂ Follow me on Github or Connect on LinkedIn
➕ Check out my other posts

Header Icon made by Freepik from is licensed by CC 3.0 BY
comments powered by Disqus
rss facebook twitter github gitlab youtube mail spotify lastfm instagram linkedin google google-plus pinterest medium vimeo stackoverflow reddit quora quora