Google Home + Raspberry Pi Power Strip

80K5898

Intro: Google Home + Raspberry Pi Power Strip

UPDATE #2: IFTTT changes:

My existing IFTTT commands through the maker channel are still working, but they've changed the naming structure since I made this guide. New commands still use the Google Assistant trigger, but the web requests are now handled by the "Webhooks" action.

UPDATE #1: Starter Application Now Available!

Hello World!

Now that the Google Home is out, there are lots of people wondering how to use it to control their existing Arduino or Raspberry Pi smart devices. Now that I've got my setup working, I thought I would share my fairly simple setup with you.

While I am currently using a Raspberry Pi Zero to control a five outlet power strip, This guide is more general. It will walk you through how to use any Raspberry Pi device to control an electronic relay, using Node.js and the IFTTT web services.

Check out a video of the device in action, or scroll to the final step, where I have it embedded!

https://www.youtube.com/watch?v=mCTjZTCaTzM

STEP 1: What You'll Need

At the very least, you will need:

And the rest is software. If you are totally new to Raspberry Pi, be aware that you may need some additional hardware like usb cables or wifi chips in order to get up and running.

STEP 2: On-Board Software Setup

So, to make this guide as user-friendly as possible, I'm going to include some links that you power-users might find excessive.

TLDR in advance: set up your raspberry pi on WiFi or Ethernet (preferably WiFi) and configure your router so that you have a server available externally. You'll use raspberry-gpio-python to control the relay.

For newer hobbyists, you will start out by setting up your raspberry pi.

You will want to get your raspberry pi set up on your local WiFi.

I'll be working in Node.js, so you will want to upgrade to the latest version of Node.

Configure the router so that port 80 redirects to your raspberry Pi's MAC address. (Sorry, this will depend on what router you're using, and there isn't really a universal guide)

I prefer using SSH to connect to my raspberry pi.

Plenty of things can go wrong in this process while you're starting out. Stay patient, and google things. The community is very supportive, and the odds are someone else has had your problem before!

STEP 3: Make a Circuit

So, there are lots of guides on getting started with relays on the Rasberry Pi. I mostly used Youtube tutorials like this one to get started.

Basically, you will need to provide power from your Raspberry Pi's 5v out pin, and choose which control pins you want to use to send the on/off signal to trigger the relay.

Using the above image, I recommend using the yellow pins for whichever model you use.

STEP 4: Create Your Server

Starter application now available!

Vist https://github.com/krpeacock/google_home_starter to download a starter application for this project, and follow the README to get it configured and running on your own device.

You can also check out my more-fleshed-out React project at https://github.com/krpeacock/power_strip/tree/strip if you are interested in seeing a slightly more complex version of the project

The main step is to build an Node + Express server that is able to handle POST requests.

In my code, it looks like this:

app.post('/api/switches/:id', function(req, res){  

  var foundSwitch = getSwitch(req.params.id);  

  foundSwitch.toggle();  

  saveState();  

  console.log("postSwitch "+JSON.stringify(foundSwitch));  

  res.json(foundSwitch);

})

I make a post request to /api/switches/:id, where id is written as sw1, sw2, and so on. After identifying the switch, I call a toggle() method to run my Python script and change the state of my relay.

I wrote individual python scripts for off and on functions, specifying which GPIO pin was tied to each switch. for example, sw1_on.py looks like:

import RPi.GPIO as GPIO<br>GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.OUT)

Then, by requiring the Python-shell node module, I can execute the script, using:

<p>const PythonShell = require('python-shell');</p><p>PythonShell.run('./public/python/scripts/sw1_on.py')</p>

STEP 5: Connecting to the Google Home

If you've managed to get this far, this information is probably the only reason that you're here. That's fine! this is the cool bit.

You have your server running, and it can control a relay. It is structured so that a POST request can change the state of the relay. Now all you need is to get your Google Home to deliver a POST request to your device. Eventually, you will want to add some authorization so that strangers can't control your devices, but now we just want the request to work.

  1. Go to https://ifttt.com and connect it to your Google account.
  2. Go to https://ifttt.com/create, and click on the +this link.
  3. Search for Google Assistant
  4. Select Google Assistant
  5. Choose "Say a simple phrase" as your trigger
  6. Tell Google what should trigger the action.
    • I prefer to name use the device I want to control, so I said "turn my lamp on"
  7. Designate a response
    • "Turning your lamp on"
  8. Click "Create Trigger" and proceed
  9. Click the +that link
  10. Search for "Webhooks"
  11. Select "Make a web request"

Now, here is the important bit. Identify your IP address (or domain, if you set up that level of abstraction), and enter it into the URL portion. If you followed my the structure in my starter project, it will look like

http://ipaddressgoeshere/API/switches/sw1?password=yourpasswordhere

Set Method to POST

Content Type should be text/plain

Body can be left blank

Create your action and choose Finish.

STEP 6: Congrats!


You've done it! Your Google Home now knows how to communicate over HTTP with your smart device.

Since this does a toggle, you can technically keep saying "Turn the lamp on" to turn it on and off. I preferred to add duplicate on and off commands for each of my switches to make everything feel more comfortable.

If you would like to contribute to this guide, or to work with me on building out a starter application, you can also feel free to get in touch! I want to make this process as easy as possible for new hackers.

79 Comments

Thanks for your guide, it worked excellently for me.
Of course, some quirks here and there, like the fact that my raspbian node.js version didn't come with npm and I had to download the latest one, or the fact that you didn't use GPIO.output on you python script. Other than that, it was excellent.
Now, I may try to add ssl support for the node.js server so the password isn't flying all over the place in cleartext ;)
I made a small video, it's in spanish, and I still have to attach the 8 relay module, but the LEDs represent the signal that will be sent to the module, and it works :D
Congratulations! It's been a few years since I set this up originally, so it may be about time that I update the starter with newer packages (like replacing the Python scripts with the node GPIO library that exists now).

Your project board is looking great, and I hope your 8-relay project ends up going smoothly as you work on it more
Thanks a lot! I used your google home and raspberry pi communication to write a program that controls the drone via the google home.
Hi. I Have been struggling with setting the sever up. I have the internal and external IP but i can't seem to get the server running. Thats the first problem. The second problem im having is Im a begginer coder. I dont fully understand the code the you are using but i do have some understanding of how it works. And the last problem. This code is for a 1 channel relay. i have a 16 channel relay how would I get all 16 to work with the google home. Any help??
You've got quite a journey ahead of you to get this project working, but don't get discouraged! There's a lot you'll know on the other side of this project that will be useful in future projects you want to take on.

First things first - I'd recommend trying to download the server first and run it on your computer. It can be harder to experiment and iterate directly on the pi, and this seems to be your first experience with a server. If you encounter any specific error messages, those will be helpful in directing you through the steps you may have gotten hung up on.

Definitely go through the process to figure out one relay first. Once you've got that problem solved, you'll have a new set of problems to face getting all 16 relays wired up. The pi should have enough pins to handle it, but you'll run into some power supply issues getting the required voltage to all of the relays in series.

For a code sample that would indicate how to extend the project, I built a project with 5 relays here: https://github.com/krpeacock/power_strip
If i use a ngrok server how would i get it to run the the app.js file and let it know that it has to turn on so and so GPIO?
The ngrok server "forwards" requests to another local server you'd be running. You will still need to install node.js, which will allow you to run JavaScript files on your device. Node.js will be responsible for running app.js and handling the GPIO, and ngrok will help you access your Pi from anywhere, not just on your local WiFi
Thanks for the quick reply. I got the server to say that its listening to port xxxx but when I try to use it in the browser it says connection refused. I really dnt know why.
And you're typing "http://localhost:[port # here]" in the browser url?
Yes i am. I tried using the internal and external ip but both say connection refused. I have a custorm port that was set up a year back. I've been using that port which was also made for port forwarding so i dont know if it could be that or do i have to use a specific port.
Thank you for your help! I have a totally different setup, but that webhook guide....helped me a lot!!!
Don't you think we need port forwarding? because google home cannot send requests to local machines (IP Addresses) right?
+1. Even after setting up port forwarding I'm having issues (IFTTT constantly errors with ETIMEDOUT). Any thoughts?
yes i had the same issue.
I used an API tester to give me a debug log of what was wrong, turns out port 8080 worked the best for this and setting my internal IP as a higher number while static to avoid it being taken when the Pi restarts. It is now up and running with no interactions other than with my Google Assistant
so Ive tested the .py scrips in the public folder and the npm start runs to listening on port 80
I have the port forwarded correctly to my pi
on the IFTTT Aplet however im getting the following
"Unable to make web request: Error: ETIMEDOUT"

this is my webhook URL "http:// myPublicIP/ API/ switches/ sw1?password=test&state=on:80" minus the spaces (Obviously)
my .env
DEV=FALSE
PORT=80
PASS=test
is there anyone here who has any ideas as to why I am getting this?
Nevermind, I used an API tester to give me a debug log of what was wrong, turns out port 8080 worked the best for this and setting my internal IP as a higher number while static to avoid it being taken when the Pi restarts. thank you for this code it is now up and running with no interactions other than with my Google Assistant
Hi Kyle, your tutorial worked out great for me, but I'm still questioning one thing. Would it be possible to integrate a button click rather than a switch seamlessly? Or would the best way be to simply reset a switch to off after an update is detected? I'm fairly new to all of this with limited python knowledge. Thanks in advance
There are plenty of ways you could make that work! I wanted mine to use a website or use voice control, but you could definitely find some way to get a button to trigger IFTTT. I suspect that most buttons that support Alexa would be good candidates, or you could find a way to do it directly through the Pi.

Congrats on your build, and I encourage you to push yourself to build your own vision!
Thank you for the reply, the reason I need it to function as a button is to replace the buttons on a fan remote, but I've started to doubt that it's the best way to automate it. Do you have any other ideas for me? The fan is a 3 speed with a light that dims when you hold the button, but I should be able to bypass that. Or should I try making my own control unit?
Hey all, really a newbie with node.js, btw installed node and npm, then downloaded your very very interesting project, and then since couple days, knocking my head on the wall trying to understand why it won't work with my setup. The python files works, the light turns on and off, but calling http://ipaddressgoeshere/API/switches/sw1?password... (with obvious values inside) nothing happens except the value: {"id":"sw1","state":"off","name":"Kyle's Lamp"}.
I can't even understand how to pass on and off parameter to the post...
Would anybody please save me from autodestruction ? ;)
Thank you.
More Comments