We’re here to help in the event of a school closure. See our resources.
NodeMCU ESP8266 google assistant

Home Automation using Google Assistant & ESP8266

We sometimes use affiliate links in our content. This won't cost you anything, but it helps us to offset the costs of paying our writing team. You can support us directly on BuyMeACoffee. Thank you!

Online Robotics Course by Learn Robotics
  • Facebook
  • Twitter
  • Pinterest
  • Buffer
  • reddit
  • LinkedIn
Previously, I posted a tutorial about home automation where I showed you how you can control relays over the internet using an Android app. In this article, I will show you how to configure your home automation system using Google Assistant and an ESP8266.

In this project, you’ll learn how to build a voice-controlled appliance. We’ll program phrases like “Turn on the lights” or “Switch on the fan.”

Sounds interesting right?!

You’re probably wondering if this is another tutorial for Android users. But that’s not the case! Google Assistant is now available for iOS on the App store. So, everyone regardless of smartphone type can take part in this home automation project using Google Assistant.

Before we get started, I recommend reading the following articles:

These posts will give you a general idea about the Web Apps we’ll be using.

If you are into IoT and Robotics, be sure to check out my eBook, “Mini WiFi Robot.” This eBook will take you through all the steps from designing, to building, to coding your very own mobile robot.

With that said, let’s get started.

Components for Home Automation with Google Assistant

Here is a list of all the hardware and software components we need:

Hardware Components

Software Components

Creating Variables and Devices

First, we have to create a few variables that will control our relays. To create variables, sign in to your RemoteMe account and go to “Variables” and click on “Add.” Fill the information as presented below:

relay control RemoteMe
  • Facebook
  • Twitter
  • Pinterest
  • Buffer
  • reddit
  • LinkedIn

Repeat this process for three more variables for Relay_2, Relay_3, and Relay_4.

create device with RemoteMe
  • Facebook
  • Twitter
  • Pinterest
  • Buffer
  • reddit
  • LinkedIn

After all the variables are created, we need a network device that will connect to our ESP8266 board. To create a device go to the “Devices” tab and click on “New Device.” From the drop-down menu select the last option “New Network Device.”

RemoteMe home automation
  • Facebook
  • Twitter
  • Pinterest
  • Buffer
  • reddit
  • LinkedIn

Here we need to name our device. I suggest using the same names as above so that you’re not confused while coding. Let’s name the device “Google_ESP.” The device ID is ‘1’. After these parameters are filled, hit “Submit” to create the device.

RemoteMe Google Assistant NodeMCU
  • Facebook
  • Twitter
  • Pinterest
  • Buffer
  • reddit
  • LinkedIn

Next, we have to generate the code for the device. To generate code click on the burger menu on the device and select “Code Generator Wizard”.

code generator RemoteMe
  • Facebook
  • Twitter
  • Pinterest
  • Buffer
  • reddit
  • LinkedIn

We will see a pop-up window where we have to select the variables we created earlier. Now click on “Next”

Variables for Home Automation using Google Assisant
  • Facebook
  • Twitter
  • Pinterest
  • Buffer
  • reddit
  • LinkedIn

On the next page enter your WiFi credentials so that the ESP can connect to your network.

home automation google assistant
  • Facebook
  • Twitter
  • Pinterest
  • Buffer
  • reddit
  • LinkedIn

Next, you can enable a direct connection if you want. This will minimize lag, but it doesn’t matter for this project.

configure RemoteMe
  • Facebook
  • Twitter
  • Pinterest
  • Buffer
  • reddit
  • LinkedIn

Then, turn on the “Debug” option and click on next.

  • Facebook
  • Twitter
  • Pinterest
  • Buffer
  • reddit
  • LinkedIn

Download the code. We will edit this code in the next step. Lastly, we need one more device. This will be a web device where we can switch on/off our devices.

To create the Web page, click on “New Device” and select the very first option “New Web Page”.
Name the device ” Web_ESP” and give device ID “2” and submit.

ESP8266 IFTTT RemoteMe
  • Facebook
  • Twitter
  • Pinterest
  • Buffer
  • reddit
  • LinkedIn

To control the relays we need buttons/switches on the web page. To add this page, first, click on the device “Web_ESP,” and click on “index.html.” From the drop-down list select “Edit with wizard.”

Web App ESP8266 home automation
  • Facebook
  • Twitter
  • Pinterest
  • Buffer
  • reddit
  • LinkedIn

Click on the “Insert Component”, next click on “Components,” and from the list select “Switcher.”

develop web app NodeMCU
  • Facebook
  • Twitter
  • Pinterest
  • Buffer
  • reddit
  • LinkedIn

In the name field notice the search icon (magnifying glass). Click on the icon and select “Relay_1.”

button for web app
  • Facebook
  • Twitter
  • Pinterest
  • Buffer
  • reddit
  • LinkedIn

Next, you can label the button anything. I labeled it “Relay 1.”

RemoteMe
  • Facebook
  • Twitter
  • Pinterest
  • Buffer
  • reddit
  • LinkedIn

Next click on “Insert” to add the switch.

Similarly, we have to create 3 more switches for Relay_2, Relay_3, and Relay_4.

switches for Web App
  • Facebook
  • Twitter
  • Pinterest
  • Buffer
  • reddit
  • LinkedIn

Set Up IFTTT for Google Assistant

In the previous step, we created variables and devices. We can use this for home automation, as is, but this is pretty boring.

To make it more interesting, we are going to connect our project to Google Assistant to take voice commands and turn on the relays.

First head over to the IFTTT website, and sign up with your Google account.

Note: Make sure you use the same google account you have on your smartphone or the setup won’t work.

Sign up for IFTTT
  • Facebook
  • Twitter
  • Pinterest
  • Buffer
  • reddit
  • LinkedIn

 

Once you log in, click on “My Applets” and then click on “New Applet”

Google Assistant IFTTT NodeMCU
  • Facebook
  • Twitter
  • Pinterest
  • Buffer
  • reddit
  • LinkedIn

 

On the page, you will see a heading, “If this then that.” Click on “+this”

IFTTT recipe home automation
  • Facebook
  • Twitter
  • Pinterest
  • Buffer
  • reddit
  • LinkedIn

 

In the search box type “Google” and select the “Google Assistant.”

home automation IFTTT Google Assistant
  • Facebook
  • Twitter
  • Pinterest
  • Buffer
  • reddit
  • LinkedIn

 

Now select the first trigger “Say a simple phrase.”

Google Assistant IFTTT
  • Facebook
  • Twitter
  • Pinterest
  • Buffer
  • reddit
  • LinkedIn

Here we will add some phrases we want the assistant to recognize and trigger an action. I used “Turn on relay one” as the trigger, below there are two more options where we can add the phrases in different forms.

Google Assistant IFTTT
  • Facebook
  • Twitter
  • Pinterest
  • Buffer
  • reddit
  • LinkedIn

Then, in the last field, we have to add a response phrase that the Google Assistant will use to give a reply. I used “Turning on relay one.” You can customize these phrases as you’d like.

Now click on “Create Trigger” to finalize the trigger.

Lastly, select “that.” Now, we will create what happens when the trigger is set.

Google Assistant IFTTT
  • Facebook
  • Twitter
  • Pinterest
  • Buffer
  • reddit
  • LinkedIn
In the search bar type “web” and select “Webhooks.”

IFTTT Webhooks ESP8266
  • Facebook
  • Twitter
  • Pinterest
  • Buffer
  • reddit
  • LinkedIn

Click on “Make a web request.”

web request IFTTT
  • Facebook
  • Twitter
  • Pinterest
  • Buffer
  • reddit
  • LinkedIn

Now go back to RemoteMe and go to the “Variables” tab.

Click on the burger menu on “Relay_1,” and click on “Generate Set Variable Link.”

link for IFTTT
  • Facebook
  • Twitter
  • Pinterest
  • Buffer
  • reddit
  • LinkedIn

Toggle on “use post method” and “Boolean”, Click on “GENERATE.”

generate link webhook IFTTT
  • Facebook
  • Twitter
  • Pinterest
  • Buffer
  • reddit
  • LinkedIn

Copy the link from RemoteMe, and head over to IFTTT. In the URL field paste the URL you just copied. Select “POST” under Method, and content-type “application/json.” Now from RemoteMe, copy the body section and paste it in the body field on IFTTT. Click on “Create action,” and “Finish” to make the Applet.

google assistant home automation
  • Facebook
  • Twitter
  • Pinterest
  • Buffer
  • reddit
  • LinkedIn

Now go to “My Applets,” and create a new applet the same as the previous step. This applet will be used to turn the relay off.  Adjust the phrases so that they make sense. Instead of a response “Turn on relay one,” change it to “Turn off relay one.”

In Webhooks paste the same URL link and select method as “POST” and content type as “application/json.” We’ll use the same body as before but replace “true” with “false” in the body section. Then hit “Create Action,” and “Finish” to create the Applet.

Google Assistant home automation
  • Facebook
  • Twitter
  • Pinterest
  • Buffer
  • reddit
  • LinkedIn

Similarly, make Applets for each relay with proper phrases. You should get 8 Applets in total for 4 relays.

IFTTT applets Google Assistant
  • Facebook
  • Twitter
  • Pinterest
  • Buffer
  • reddit
  • LinkedIn

With that complete, we are all set to edit and upload the code downloaded in the previous step.

Editing and Uploading Code

Open the code downloaded from RemoteMe. We have to edit the code a little to fit our requirements.

First, we have to define the pins we will use as output.

#define Relay_1 D1 
#define Relay_2 D2 
#define Relay_3 D3 
#define Relay_4 D4

Now scroll down, and you will find four functions something like “void onRelay_1Change“.  Here add the following line:

digitalWrite(Relay_1, b ? HIGH : LOW);

Add this line in all four functions, just replace “Relay_1” with 2, 3, 4, respectively.

void onRelay_1Change(boolean b){
  Serial.printf("onRelay_1Change: b: %d\n",b);
  digitalWrite(Relay_1,b ? HIGH : LOW);
}

void onRelay_2Change(boolean b){
  Serial.printf("onRelay_2Change: b: %d\n",b);
  digitalWrite(Relay_2,b ? HIGH : LOW);
}

void onRelay_3Change(boolean b){
  Serial.printf("onRelay_3Change: b: %d\n",b);
  digitalWrite(Relay_3,b ? HIGH : LOW);
}

void onRelay_4Change(boolean b){
  Serial.printf("onRelay_4Change: b: %d\n",b);
  digitalWrite(Relay_4,b ? HIGH : LOW);
}

Scroll down to find the setup function. Here we have to set the pin mode to output.

pinMode(Relay_1, OUTPUT);
pinMode(Relay_2, OUTPUT);
pinMode(Relay_3, OUTPUT);
pinMode(Relay_4, OUTPUT);

Also, we have to set the pins LOW, so the relays don’t turn on after powering the module.

digitalWrite(Relay_1, LOW);
digitalWrite(Relay_2, LOW);
digitalWrite(Relay_3, LOW);
digitalWrite(Relay_4, LOW);

With those changes complete, the code is ready to be uploaded to the ESP8266.

Download a copy of the sketch by entering your email below. Then you can add your WiFi name, password, and the token from RemoteMe.

Wiring Diagram for Home Automation Project

Connect components using the wiring diagram, below. You can download the Fritzing file and make modifications, as you’d like.

NodeMCU to Relay Fritzing
  • Facebook
  • Twitter
  • Pinterest
  • Buffer
  • reddit
  • LinkedIn

  • Connect D1, D2, D3, and D4 of ESP8266 to IN1, IN2, IN3, and IN4 of the relay module.
  • In the diagram, I have used transistors as a switch to control relay.
  • To power the Relays, connect a 9v battery.
  • If you want to power the ESP board, connect a 9v battery to Vin pin. I powered it over USB.
  • Make sure the GND pin of ESP is connected to the GND pin of the Relay module.

Connect the Relays (Use caution)

To turn on/off an appliance, we have to connect it to the other end of the relay.

Note: I don’t recommend working with an AC supply if you do not have prior experience. Alternating current is dangerous and accidental shock could be fatal. Learn Robotics and its contributors shall not be responsible if you harm yourself or anyone else.

Check out connections in this article for more details. To make the connections to the relay, follow the steps given below.

First, we have to find the live wire and cut it in the middle to attach it to the relay. To find the live wire you can use a wire tester.

attach wires to relay ESP8266
  • Facebook
  • Twitter
  • Pinterest
  • Buffer
  • reddit
  • LinkedIn

Connect one end of the wire to the middle terminal of the relay and another wire to normally open terminal.

connect relays home automation
  • Facebook
  • Twitter
  • Pinterest
  • Buffer
  • reddit
  • LinkedIn

Now you can plug it in, and start controlling it using your browser or google assistant.

Testing the Google Home Assistant

To test the build, first power up the ESP and the relay module. It will take a few seconds to connect to WiFi. Now almost everyone knows how to use Google Assistant, so I will not explain the process of it.

In short, we can ask Google Assistant to turn on relay one or two. In a couple of seconds, the light should turn on. You can also use the switches we created on RemoteMe.

Follow these steps:

  • Head over to remoteme.org and login with your account.
  • Go to the “Devices” tab and click on “Web_ESP”
  • Now click on “index.html” and select “Open in new tab”

Control Relays NodeMCU home automation
  • Facebook
  • Twitter
  • Pinterest
  • Buffer
  • reddit
  • LinkedIn
From the new webpage, it is possible to toggle on/off the relays.

Control Relays ESP8266
  • Facebook
  • Twitter
  • Pinterest
  • Buffer
  • reddit
  • LinkedIn

To share the page with a smartphone, select the “Get anonymous link” option from the same list. It will give you a link or click on the QR icon right next to it. It will display a QR code.

QR code RemoteMe
  • Facebook
  • Twitter
  • Pinterest
  • Buffer
  • reddit
  • LinkedIn

Scan this code using any mobile device and you will be redirected to the “Web_ESP.”

Video Tutorial

What’s next?

You can control more than four devices using an 8ch relay or even a 16ch relay board. The ESP8266 doesn’t have enough pins to control 16ch so you can use I2C modules instead. You can also program different commands into IFTTT to customize your Google Assistant commands. Lastly, this tutorial can be modified to control other electronic devices, robots, or prototypes.

I hope you enjoyed this tutorial. If you have any questions feel free to comment below.

And if you enjoy working with Arduino, you’re going to enjoy our 30-day Crash Course on Arduino. This online course is designed to bring you up to speed with building, wiring, and programming prototypes with Arduino. Join the course and gain access to over 20 hours of projects, quizzes, and a course certificate.

You May Also Like

Have a question? Need Help?

Thanks for dropping by! Comments are no longer moderated daily.
Before posting your comment, please consider buying me a coffee. If you need immediate support, you can schedule an appointment. Providing customized information for your individual scenario takes a lot of my time. I’m happy to help, but I can no longer provide individualized advice pro bono.
Thank you for understanding.

16 comments

  1. Mahikon

    help me

    D:\Descargas\arduino2\arduino2.ino: In function 'void onRelay_1Change(boolean)':
    arduino2:13: error: 'D1' was not declared in this scope
     #define Relay_1 D1
                     ^
    1. Liz Miller

      Mahikon, unfortunately, we do not provide complimentary debugging via our comments section. If you need help with your individual project, please book an appointment with our robotics coaches. Thanks for understanding ~Liz from Learn Robotics

  2. jeffrey g boudreau

    Is there a way to check the status of the relay/switch?
    I want to be able to ask google if something is on or off?

    1. Liz Miller

      Jeffrey, good question. You can do this a few ways. Either set a variable to store the current state and then read that variable using IFTTT. Another option is to parse the JSON file that is generated by RemoteMe by doing a GET request instead of a POST request on your RemoteMe URL. We have a similar tutorial using Siri that walks through this in more detail. Good luck! ~Liz from Learn Robotics

  3. Same fail: RemoteMeDirectWebSocketConnector.h No such file or directory
    I installed both libraries (remoteme and websockets) as instructions, but Arduino IDE can’t compile.

    1. Liz Miller

      There are 3 libs you need: 1) RemoteMe by RemoteMe.org, 2) RemoteMeUtils by Remoteme.org and 3) WebSockets by Markus Sattler

      1. Thanks, You are right, I needed RemoteMeUtils lib, Proyect works all ok. You are great.

        1. Liz Miller

          Awesome, Ronald. I’m glad you were able to get it working!

  4. Márcio Galvão

    Hi,
    error is appearing on this line
    RemoteMe& remoteMe = RemoteMe::getInstance(TOKEN, DEVICE_ID);

    1. Liz Miller

      Márcio, make sure you have the WebSockets by Markus Sattler and the RemoteMe by RemoteMe.org libraries installed in the Arduino IDE. Then try compiling again.

  5. bbester

    Hi and thank you very much for this tutorial.
    I’m getting errors when compiling the sketch – not able to past the error message here but it goes something like “…..librariesRemoteMesrcRemoteMeWebSocketConnector.cpp:69:36: error: ‘REMOTEME_HTTP_PORT was not declared in this scope

    ….webSocket->begin(REMOTEME_HOST, REMOTEME_HTTP_PORT, buf “as”);

    Will appreciate any help to solve this
    BB

    1. Liz Miller

      BB, make sure you have the WebSockets by Markus Sattler and the RemoteMe by RemoteMe.org libraries installed in the Arduino IDE. Let me know how it goes!

  6. Hans Jakobsson

    Hi,
    I get this error RemoteMeDirectWebSocketConnector.h No such file or directory
    What can i do about that

    1. Liz Miller

      Hans, I believe this error is because of library dependencies. Did you install WebSockets by Markus Sattler and the RemoteMe by RemoteMe.org libraries in the Arduino IDE? If not, try installing this library by going to Sketch > Include Library > Manage Libraries and search for “WebSockets”. You’re looking for the one by Markus Sattler. Do the same for the RemoteMe library. Click install and try compiling your code again. Here’s an article on this setup you can refer to: https://www.learnrobotics.org/blog/control-devices-using-a-web-browser-nodemcu/

  7. Hans Jakobsson

    Hi,
    I get this error RemoteMeDirectWebSocketConnector.h No such file or directory
    What can i do about that

    1. Liz Miller

      Hans, I believe this error is because of library dependencies. Did you install WebSockets by Markus Sattler and the RemoteMe by RemoteMe.org libraries in the Arduino IDE? If not, try installing this library by going to Sketch > Include Library > Manage Libraries and search for “WebSockets”. You’re looking for the one by Markus Sattler. Do the same for the RemoteMe library. Click install and try compiling your code again. Here’s an article on this setup you can refer to: https://www.learnrobotics.org/blog/control-devices-using-a-web-browser-nodemcu/

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Free Learn Robotics Swag

Patch/Sticker with every LR Kit

Work with an Engineer

Live Chat with an Instructor

365 Day Course Access

1 year access to every course

100% Secure Checkout

PayPal / MasterCard / Visa

Become a Learn Robotics Insider

Sign up and receive product announcements and discounts!

 

Success! Check your email for a surprise!

Pin It on Pinterest