Control ESP by Google Assistant

Introduction

Don’t forget to check out the video tutorial :

In this tutorial,we will use Google Assistant to control devices :

Assumption

We will use IFTTT and RemoteMe. In IFTTT, we can easily add voice commands for Google Assistant, which will call a “trigger” in IFTTT and this will call the http query to change the variable in RemoteMe. The RemoteMe will  forward command to the ESP. And the ESP will change LED or servo mechanism state

 

Let’s begin

 

RemoteMe

The Google Assistant will control two variables

After adding the variables, ESP will be “observing” it, when a variable value is changed by the Google Assistant our ESP will be notified

Adding Variables :

Name of Boolean variable is RELAY_01 – with this name it will be easier to add  android application later to control it. More about variables here

The same way we are creating integer type variable with the name “servo”

Now we have to send variable change into ESP

ESP

Connections

After we change variable the ESP will set LED or servo mechanism to given state.

We will start by generating the code with the functions needed to collaborate with variables  .

To add a device to RemoteMe, go to the “Devices” tab, then “New Device” and “New Network Device” in the window enter:

Then click “Submit” more here

To generate the code, click on the Burger Menu and the “Code Generator Wizard”:

In the first step, we select all variables, this will generate the appropriate code where the function will be created for the variable manipulation and called when variable is changed more about the wizard here 

In the second step, enter the name of our WiFi network and the password to it (we can leave it empty and complete it later in the code).

In the last step, we display the generated code and paste it into the Arduino IDE. Below the code completed with the implementation of the servo control mechanism and the LED.

new lines are mark by //ADDED

a library for servo control mechanism – it must be added before, as well as the RemoteMe library more   here

Function called when the Google Assistant changes our variables.

setting the servo mechanism and pins for controlling the LED .

After these modifications we upload our sketch

Testing

Our ESP after uploading the sketch and powering on will connect to RemoteMe:

and after going to the variables tab and expanding them:

ESP – in variables means that our device is connected and is ready to receive data .

let’s change the variables and see if the diode and servo mechanism reacts accordingly. From the burger menu select the “Set …” option

And in the window that appears we change the value of the variable and click Submit. Our LEDe and servo mechanism will react.

Generating links to change variables

IFTTT will be like the “Set …” function, change the value of a variable, but IFTTT will do it by calling the http query appropriately. Let’s generate a simple link to the GET method:

From the variable menu burger, click on “Generate Set Variable Link”:

generated link:

After copying the link and opening it in the browser (even in incognito mode – where we are not logged in ), our LED will turn off, the URL is using one of our tokens, that’s why it works even if you are not logged in

To generate a link to light up with a diode – change “boolean” to on and click “Generate” we will get a link after calling which our diode will light up.

IFTTT

Let’s create an Applet for voice control

  1. Adding the Google Assistant with the “turn light on” command
  2. Adding a connected “Webhook”, which will call the appropriate query on the RemoteMe, just like the links from the example above will turn on the diode.

 

In IFTTT, we go to My Applets, then New Applet, after clicking on “+ this”

we choose Google Assistant:

And then “Say a simple phrase”, we complete:

(Note: phrases such as “turn light on” can already be assigned to the default command in the Google Assistant – so when we get a different response after calling a voice command than “OK preforming action” we have to choose another voice command)

and click create trigger, then click “+ that” and choose webhooks:

and “Make a web Request” and fill with data generated by RemoteMe

let’s generate :

in this case we chose the “use Post Method” option (the POST method is more elegant to change the variables state than the GET method) and generated a link and POST query content that will enable the LED

after pasting into our webhook it should look like this:

 

(don’t forget to change Method to POST and Content Type to application/json)

we click “create action” and “finish”. After a while, when our account on Google Assistant is updated, we can say “okay google, turn on light” and our diode will be turned on.

Similarly, we create actions for the phrase “turn off light”, remembering to generate a link in remoteMe with the “boolean” option set to false. Now we can turn the diode on and off with the help of the Google Assistant.

IFTTT – servo

Like a diode, we can generate a command to control the servo mechanism, but in this case we will say in the command a number that will be properly recognized.

We create a new applet similarly to the previous one, but now in the Google assistant we choose “Say Phrase with a number” and we complete:

in the “Set servo to #” command instead of # we will tell the number that will be recognized. Let’s insert the URL, modifying the “body” generated by RemoteMe with the appropriate “tag” after pasting the webhook should look like this:

IFTTT will replace in the called {{NumberField}} json by the number we say to the Google Assistant.

Our Applets look like this:

Then from the Google Assistant, we can change our diode and the position of the servo mechanism. The movie from 10:40 shows the action:

Bonus – control via a website

We can also easily create a website – hosted in the RemoteMe cloud to control our ESP,

We choose “new Device”, “New WebPage” then fill

After adding, extend the belt website, click on index.html and choose “Edit With wizard”

then click on the Insert Component and complete:

the name property insert by clicking the “magnifying glass” we will minimize the mistake when entering the names of the variables. After clicking “Insert” add the component to control the servo mechanism:

after adding, close the wizard, click on index.html and choose “Open in new Tab”

by clicking the switch we turn off our led, and by moving the slider we change the positions of servo mechanism.

We can open the website on a mobile phone, the easiest way to scan the QR code generated by clicking on index.html -> “Get anonymous link” and clicking the QR code icon, after display the QR code we scan it with a smartphone and we have access to website control

Bonus – controlled by mobile applications

There is an application – provided by Nguyễn Duy Hiếu  available here

Which, when connected to our account, allows us to control our diode – from the Relays tab (of course, because we called our variable RELAY_01 diode at the beginning)

 

Summary

This simple tutorial shows the basics how by invoking http queries change variable states  i.e. by IFTTT.

I encourage you to create your own examples 🙂