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!


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);  



  console.log("postSwitch "+JSON.stringify(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.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>

Looking back, this is a little bit tricky for a non-developer. I probably will need to throw in some starter code down the road.

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 "Maker"
  11. Choose the Gray icon (not WeMo Maker)
  12. 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


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 enjoyed this project, please share! There's a LinkedIn post here that can really help me out.

I am a web developer struggling to find work in the SF Bay Area, so I could use some visibility. If you are looking to hire a JavaScript developer, or know someone who is, you can can get in touch with me at kylpeacock@gmail.com.

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.

<p>Hey dude, awesome tutorial. I'm having a little trouble with starting the npm. I've added the port and psw to the hidden .env file but when I &quot;npm start&quot; I get this...</p><p>'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''</p><p>pi@raspberrypi:~/google_home_starter $ npm cache clear</p><p>pi@raspberrypi:~/google_home_starter $ npm start</p><p>&gt; google_home_controller@1.0.0 start /home/pi/google_home_starter</p><p>&gt; node app.js</p><p>module.js:340</p><p> throw err;</p><p> ^</p><p>Error: Cannot find module 'dotenv'</p><p> at Function.Module._resolveFilename (module.js:338:15)</p><p> at Function.Module._load (module.js:280:25)</p><p> at Module.require (module.js:364:17)</p><p> at require (module.js:380:17)</p><p> at Object.&lt;anonymous&gt; (/home/pi/google_home_starter/app.js:1:63)</p><p> at Module._compile (module.js:456:26)</p><p> at Object.Module._extensions..js (module.js:474:10)</p><p> at Module.load (module.js:356:32)</p><p> at Function.Module._load (module.js:312:12)</p><p> at Function.Module.runMain (module.js:497:10)</p><p>npm ERR! google_home_controller@1.0.0 start: `node app.js`</p><p>npm ERR! Exit status 8</p><p>npm ERR!</p><p>npm ERR! Failed at the google_home_controller@1.0.0 start script.</p><p>npm ERR! This is most likely a problem with the google_home_controller package,</p><p>npm ERR! not with npm itself.</p><p>npm ERR! Tell the author that this fails on your system:</p><p>npm ERR! node app.js</p><p>npm ERR! You can get their info via:</p><p>npm ERR! npm owner ls google_home_controller</p><p>npm ERR! There is likely additional logging output above.</p><p>npm ERR! System Linux 4.4.50+</p><p>npm ERR! command &quot;/usr/bin/nodejs&quot; &quot;/usr/bin/npm&quot; &quot;start&quot;</p><p>npm ERR! cwd /home/pi/google_home_starter</p><p>npm ERR! node -v v0.10.29</p><p>npm ERR! npm -v 1.4.21</p><p>npm ERR! code ELIFECYCLE</p><p>npm WARN This failure might be due to the use of legacy binary &quot;node&quot;</p><p>npm WARN For further explanations, please read</p><p>/usr/share/doc/nodejs/README.Debian</p><p>npm ERR!</p><p>npm ERR! Additional logging details can be found in:</p><p>npm ERR! /home/pi/google_home_starter/npm-debug.log</p><p>npm ERR! not ok code 0</p><p>pi@raspberrypi:~/google_home_starter $</p><p>'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''</p><p>two things in my mind...</p><p>-do I need to change the code you provided before running npm</p><p>-everytime I assign port 80 to the rpi the port closes.... I check with &quot;canyouseeme.org&quot; The port works with my cctv system but when I assign it to the rpi ip it closes... Let me know if you have any thoughts on that pls.</p><p>-----------------------</p><p>Also I have no idea what to do with the code after starting npm, if you could point to some resources or walk us through it I would be willing to make a video to put here.</p><p>Thanks so much in advance and good luck in your job hunt.</p><p>JF</p><p><a rel="nofollow">[delete]</a></p>
<p>Hi</p><p>jf89, did you fix this? Im getting the same error</p>
<p>no mate... but let me know if you do! </p><p>I think its going to have something to do with the script, like us hosting/altering the files but I have not experience in js or node....</p>
<p>i got it working on my laptop. <br></p><p>1. So you install node.js on your computer where you will test this starter project in node.js. Then inside the starter directory which has the package.json file, you run &quot;npm install&quot; which installs all the modules required by that package.json.</p><p>2. Then you create a file (in the same directory) called &quot;.env&quot; using touch or nano. Touch is nice because you just type touch .env and itll create the empty file for you.</p><p>3. Then you edit the file to look like this:</p><p>DEV=TRUE</p><p>PORT=8000</p><p>PASS=yourpass</p><p>4. Finally you run &quot;npm start&quot; and you get a prompt telling you the server is running and you can run the ipaddress:8000/api...etc... url in your browser with the password in the .env file you created.</p>
<p>Finally got it, thanks man. </p>
<p>I updated node and npm on my mac (im away from my pi), downloaded and unzipped the google_home_master file and Im reading thru the readme. How do I run touch .env?</p>
Hi. I wrote you a message via instruct able but I'm not sure it worked. I have a raspberry with a Bluetooth module in it's GPio pins. A script sends commands to that Bluetooth module which relays the command to a Bluetooth relay board which controls components. What I want to do now is to have a Google home send commands to that raspberry pie in order to execute a different script. Is that possible?
<p>hello Kyle,<br><br>i am a developer , and i worked with arduino , but not raspberry pi and node.i did everything step by step.<br>extracted the zip in Home/pi<br>not sure what to do next<br>when i call the <a href="" rel="nofollow"></a><br>this is what i get <br>___________________________________________<br></p>Not Found<p>The requested URL /API/switches/sw1 was not found on this server.</p><br>Apache/2.4.10 (Raspbian) Server at Port 80<br><br>________________________________________________<br><br>i tried to run the app.js and i got this error:<br><br>File &quot;app.js&quot;, line3const Pythonshell = reque....... <br>sytax error<br><br>
It sounds like Apache is hijacking port 80. You'll need to disable the Apache server with something like 'sudo service stop Apache' and then you'll be able to to run the Node.js server with 'sudo node app.js'.<br><br>Also, if you haven't yet, make sure to run 'npm install' in the console so that the package manager can pull in the additional resources that I aren't part of the bundle. You'll need those before Node will be able to execute my starter code. <br><br>Let me know if that helps!
<p>hello again , <br>after playing around for a while i sorted out the problem.<br>now when i call the link with the password from any device on my network it diplay this page &quot; {&quot;id&quot;:&quot;sw1&quot;,&quot;state&quot;:&quot;off&quot;,&quot;name&quot;:&quot;Kyle's Lamp&quot;}&quot;<br>but still the relay is no switching on or of, i modified the python on and off script and changed the pin number to the one i am using.<br>should i do anything else ?</p>
<p>So, the problem there is that you are making a GET request to the API, which is telling you the state of the sample switch. You'll need to make a POST request to the server in order to affect the state. </p><p>A great tool for this is https://www.getpostman.com/.</p>
<p>hello again.<br>and sorry , but i couldnt get my head around that.<br>i installed it on my pc, and when i request the link , i get the same result i get on a normal browser.<br>i got my google home to australia ( its not relased her yet) and want it to work so bad :P<br></p>
<p>1- i did stop apache .<br>2- when i do 'sudo node app.js' i get &quot;Listening on port undefined'<br>what dose this means ?<br>3- when i go to &quot;<a href="" rel="nofollow"></a> <br>i am asked again to enter username and a password.</p>
<p>It is trying to read your password and port from a hidden .env file that you will need to create. </p><p>Run `touch .env` in the root of the project. In that file, you'll need a port and password with syntax like this: <br><br>PORT=80</p><p>PASSWORD=password_here</p><p>I didn't want to share my info up on GitHub, and I figured you wouldn't either!</p>
<p>This is very interesting, I was looking for something similar!</p><p>No authentication? So anyone knowing your ip address can easily control your lamp even without Google Home.<br>My preference would be a local service, but I don't know when (or if) Google is going to allow that.</p>
<p>I've just released a starter application that requires you to use a password query string in the URL. It's not the most secure method, but it should keep your friends from messing with you.</p><p>https://github.com/krpeacock/google_home_starter</p>
This is an intermediary step. Since you have the ability to edit the body of the post request on IFTTT, you can readily attach some private key or JWT to your request. Hopefully, I'll get that next step running as part of my starter code, and then I'll share my authentication solution with everyone!<br><br>As for a local solution, you can check out this https://timleland.com/wireless-power-outlets/ implementation. There is a project that imitates a Phillips Hue Bridge, which can be accessed on local wifi. Unfortunately, those protocols limit you to On/Off and &quot;dimming&quot; features.<br><br>My approach has the benefit of being accessible through browser, phone widgets, and voice control, as long as you set up those features. Since the server's API can handle whatever requests you can dream up, it leaves room for scheduling events, affecting multiple devices, and so on.
Kyle,<br><br>I understand the the phase you did was just a start. Hopefully ifttt.com has a way to send url requests with authentication.<br><br>And thank you for the links! The habridge hack looks very promising for what I'm trying to do!<br><br>
<p>This is a great project, I'm going to set it up for myself. You mentioned examples and a starter project, which I would find very useful. I am a programmer, but with no experience with Node.JS, it would help!</p>
<p>I've just set up a starter application for you to use. Let me know if you have any issues setting it up! https://github.com/krpeacock/google_home_starter</p>
I'm going to build the new project over the weekend. I'll reply here with it once it's ready, and I'll update the Instructable to point to it. <br><br>Also, for my benefit, do you think you'll need more details on the electronics portion, or are you mainly concerned about the Node.js environment?
<p>The Node.js. I'll be adjusting the python scripts to work with my own IoT project.</p>
<p>Great! I'll let you know when I've got something for you to use.</p>
<p>That's a neat setup :)</p>
Thanks! I use the strip at my nightstand along with a couple Phillips Hue bulbs around the apartment for the full &quot;smart home&quot; experience

About This Instructable




Bio: I'm a maker, programmer, and designer. I love music, games, and simple design. I'm looking for work!
More by call_me_kyle:Google Home + Raspberry Pi Power Strip 
Add instructable to: