Home Automation Using Google Assistant

Previously we have seen a post about home automation, if you haven’t seen it already then I recommend you check it out here. In this tutorial, we will see how to control devices/appliances over the internet using google assistant.

Before getting into it, do check out “Getting started with ESP8266” tutorial as it will help you set up the IDE for ESP programming. After you are familiar with ESP8266, we can move on.

Video Tutorial:

Requirements:

Creating Variables & Devices:

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

  • name = Relay_1
  • mode = Local
  • type = Boolean
  • toggle on “Persistent”

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

Now that variables are created we need a network device that will connect to our ESP board. 
To create a device goto “Devices” tab and click on “New Device”. From the drop-down menu select the last option “New Network Device”. 
Here we just need to give a name to our device. I suggest you use the same names as I have used so there will be no confusion during coding.
Let’s name the device “Google_ESP”, The device ID I have used is ‘1’. After these parameters are filled, hit “Submit” to create the device.

After the device is made, we need to generate the code. To generate the code, we will use the Code Generator Wizard. Click on the burger menu on the network device and select the wizard.

You will get a pop up window , just select the 4 variables we created earlier and click next and follow the instructions. Check the video tutorial for more details. Finally download the code.

Now let’s create another 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.

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

Click on the “Insert Component”, now click on “Components” and from the list select “Switcher”.
Now we have to fill in some information, follow the steps :

  • In the name field notice the search icon (magnifying glass). Click on the icon and select “Relay_1” .
  • Next you can label the button anything. I have labelled it “Relay 1”
  • Finally click on “Insert” to add the switch.

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

With that our devices are ready and we can move on to IFTTT.

Setting Up IFTTT :

In the previous step, we created variables and devices, We can use that setup for automation but it will be pretty basic and boring. To make it more interesting and futuristic we are going to use Google Assistant to take voice commands and turn on the appliances. 
First head over to IFTTT website and sign up with your Google account.
Note: Make sure you use the same google account you have on your smartphone or else the setup won’t work.

Follow the steps given below :

  1. Once you login, click on “My Applets” and then click on “New Applet”
  2. On the page, you will see a heading “If this then that”, here click on “this”
  3. In the search box type “Google” and select the “Google Assistant”
  4. Now select the first trigger “Say a simple phrase”
    • Here we will add some phases we want the assistant to recognize and do a task, I have used “Turn on relay 1” as the trigger, below there are two more optional where we can add the phrases in different forms.
    • Finally, in the last field, we have to add a response phrase which the google assistant will use to give a replay. I have used “Turning on relay one”
    • Now click on “Create Trigger” to finalize the trigger.
  5. Now we have to select “that”. Here we will create what happens when the trigger is set.
  6. In the search bar type “web” and select “Webhooks”. Click on “Make a web request”.
    • Now go back to RemoteMe and goto “Variables” tab.
    • Click on the burger menu on “Relay_1” and click on “Generate Set Variable Link”
    • Toggle on “use post method” and “Boolean”, Click on “GENERATE”.
    • Copy the link and head over to IFTTT, in the URL field paste the URL just copied.
    • Select method “POST” and content type “application/json”
    • Now from RemoteMe copy the body section and paste it in the body field on IFTTT
    • Finally hit create action and finish to make the Applet.
  7. Now again go to My Applets and create a new applet same as before.
    • This applet for turning the relay off. So use phrases like “Turn off relay one”
    • In Webhooks paste the same URL link and select method as “POST” and content type as “application/json”
    • Now in Body just paste same body but replace “true” with “false” and hit “Create Action” and “Finish” to create the Applet
  8. Similarly, make Applets for each relay with proper phrases. You should get 8 Applets in total.

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

Connections :

google assistant home automation

Check out the image above, and make the circuit according to it.

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

Now 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 AC supply if you do not have prior experience, Alternating current is dangerous and accidental shock could be fatal. I shall not be responsible if you harm yourself or anyone else.

Check out connections in this article for more details, make the connections to Relay follow the steps given below :

  • First, we have to find the live wire and cut it in the middle to attach it to relay.
  • Connect one end of the wire to the middle terminal of relay and another wire to normally open terminal.
  • Now you can plug it in, and start controlling it using your browser or google assistant.

Editing and Uploading Code :

Open the code file and make the changes as shown below :

1. 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

2. 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 retrospectively.

3. Lastly 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 ESP board.

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

With that the code is ready to be uploaded.

You can download the code from here, just add your WiFi name and password and token from RemoteMe.

Testing:

To test the build first power up the ESP and Relay module. It will take a few seconds to connect to the 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.

What I want to explain is, how to use the switches we created on webpage. To use it :

  • Head over to remoteme.org and login with your account.
  • Goto “Devices” tab and click on “Web_ESP”
  • Now click on “index.html” and select “Open in new tab”
  • From the new webpage it is possible to toggle on/off the relays.
  • To share the page with smartphone, select “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.
  • Scan this code using any mobile device and you will be redirected to the “Web_ESP”.

I hope you like the tutorial and learn something new. If you have any doubts or questions feel free to comment below.