Control Devices Using Google Assistant !

by Harsh Dethe in Circuits > Microcontrollers

7597 Views, 13 Favorites, 0 Comments

Control Devices Using Google Assistant !

HOMEAUTOMATION.png

I have posted a few different types of home automation tutorials before, but this one is different. Previously we turned on/off appliances with smartphone app or web page. This time we will do same but with our voice using Google Assistant. So you can use your smartphone or Google home to use this. As always I will try my best to explain everything keeping it as simple as possible.

The services we will be using are RemoteMe and IFTTT.
I suggest you check the following instructable before you start with this project :

  1. Getting started with ESP8266.
  2. Control devices over Internet.
  3. Home automation using RemoteMe.

Once you are familiar with above tutorials, We can move ahead.

NOTE :- iPhone users can download Google Assistant from App store.

Video Tutorial :

Control Devices Using Google Assistant !

What We Need :

IMG_20181101_101958.jpg
IMG_20181101_102613.jpg
mini breadboard.jpg

Here is a list of all the hardware and software components you will need.

Hardware Requirements :

Software Requirements :

After everything is gathered we can move on to first step, creating variables.

Creating Variables and Network Devices :

Relay_var.png
variables.png
New Network Device.png
Network_Device.png
Code_Generater.png
Variables_Wiz.png
WiFi_WIZ.png
Con_Wiz.png
Debug.png
Web_ESP.png
Components.png
Switcher.png
Switcher_Relay_1.png
Switches.png

First we have to create 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 bellow :

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

Now let's generate the code for the device.

To generate code click on the burger menu on the device and click on "Code Generator Wizard". Follow the steps below :

  • We will see a pop up window where we have to select the variables we created earlier. Now click on "Next"
  • On the next page enter your WiFi credentials so the ESP can connect to your network.
  • Next you can enable direct connection if you want, doesn't really matter.
  • Finally just turn on the "Debug" option and click on next.
  • Download the code. We will edit this code in next step.

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 :

IFTT_Home.png
IFTTT_Signup.png
Select_Myapplets.png
If_This.png
trigger.png
then_that.png
Webhooks.png
Web request.png
URL_Edit.png
first relay off.png
all app.png

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 use "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 relays 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.

Editing and Uploading Code :

code.png

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.

Below I have provided the edited code you can download, just add your WiFi name and password and token from RemoteMe.

Downloads

Connections :

circuit.png
IMG_20181101_103428.jpg
IMG_20181101_105331.jpg

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 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 instructable 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 middle to attach it to relay.
  • Connect one end of the wire to 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.

Testing :

open in new tab.png
QR.png

To test the build first power up the ESP and Relay module. It will take few seconds to connect to the WiFi.
Now almost every one 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 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 smatphone, select "Get anonymous link" option from 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"

Now if you want to control more than 4 devices, you can get a 8ch relay or even 16ch relay boards.
The ESP don't have enough pins to control 16ch so you can use i2c modules in that case.

Hope you like the tutorial, If you have any questions feel free to comment below.
If you are interested in robotics and still a beginner, Check out my eBook "Mini WiFi Robot"