100% found this document useful (2 votes)
300 views22 pages

Speak To Your ESP32 & Control It by Google Assistant - ElectroPeak

Uploaded by

Mehdi Bouchami
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
300 views22 pages

Speak To Your ESP32 & Control It by Google Assistant - ElectroPeak

Uploaded by

Mehdi Bouchami
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

 Search ...

 Product Categories  Learn  Videos  Deals & Discounts  About Us  Questions?

[porto_block name="current_post_header"]

 Featured, Internet of Things

Speak to your ESP32 & Control It


by Google Assistant

Written by Amir Mohammad Shojaei

Table of Contents 

1. Overview

1.1. What You Will Learn

2. What Is Adafruit-IO?

2.1. What Can Be Done Using the Adafruit-IO?

3. What Is Google Assistant?

4. What Is IFTTT?

5. Required Material

5.1. Hardware Components

5.2. Software Apps

6. Step 1: Establish Connection Between ESP32 & Adafruit

6.1. The Code to Connect ESP32 to Adafruit-IO

6.2. Register and Make a Project in Adafruit-IO

6.3. Full Video of the Steps in Adafruit-IO Website

6.4. Upload the Code on ESP32

7. Step 2: Circuit

8. Step 3: Connect IFTTT with Google Assistant & Adafruit-IO

8.1. Connect to Google Assistant

8.2. Connect to Adafruit-IO

8.3. The Video of the Steps in IFTTT Website

9. Step 4: Send the Voice Command Through Google Assistant

10. What’s Next?


Overview

Have you ever been sitting in your room, and want to turn things like light
bulbs or fans on and off, but you don’t feel like doing it? Or you want to
turn the cooling system on before arriving home, so it would be pleasantly
cool by the time you’ve arrived?

So, if you like controlling electrical devices of your home remotely through
your cell phone, this tutorial can be so useful for you.

There is an application called “Google Assistant” installed on most


smartphones. You can use Google Assistant to speak to your phone and
send voice commands to it. Now, imagine your phone is connected to a
microcontroller like Arduino or ESP32 via internet. Then, you’ll be able to
speak to the microcontroller and send your voice commands for
controlling electrical devices. You can use a relay module to connect the
microcontroller to various electrical devices of your house such as light
bulbs, fans, cooling system and so on and control them remotely.

What You Will Learn

As stated in the previous section, the goal of this tutorial is to learn how
you can remotely control your electrical appliances. In this tutorial, we will
be using an ESP32 board as our microcontroller. Having WiFi is one of its
most important advantages.

To establish the connection, you can’t just connect the Google Assistant to
the microcontroller directly. We need to use two interfaces to establish the
connection between the Google Assistant and the ESP32. One for
connecting to the Google Assistant and the other for connecting to the
ESP32.

We use the IFTTT platform to communicate with Google Assistant. And


since the IFTTT platform can’t be directly connected to the ESP32, we use
another interface called Adafruit-IO to establish a communication between
the IFTTT and ESP32.

In this tutorial, we first need to register in the “Adafruit-IO” website and


build a block for controlling an LED. Then, using the Arduino IDE, we
upload a code on our ESP32 for communicating with the Adafruit-IO. Next,
we connect an LED to the ESP32 using a relay module. In the following, we
need to create an account in the “IFTTT” website to set up the
communication between the Google Assistant and Adafruit-IO.

Finally, we control the LED with some predefined voice commands. The
steps explained in this article can be repeated for controlling different
microcontrollers or different electrical devices.

What Is Adafruit-IO?

Adafruit-IO is actually a cloud storage space that you can connect to


through internet. You can use it to connect to a microcontroller like an
ESP32 and control its pins. You can actually use it to communicate with any
microcontroller in an internet cloud space.

What Can Be Done Using the Adafruit-IO?

Online display of data


Make projects that need to be connected to internet
Connect your project to other devices that are connected to internet
Connect projects to web services

What Is Google Assistant?

Google Assistant is an AI assistant developed by google that is mainly


available in almost all smart devices. Unlike the previous version, the new
version of the Google Assistant can participate in two-way conversations. It
can recognize words better than any other device and respond to your
commands quickly.

The users can communicate with the Google Assistant with their natural
voice, although it also supports the keyboard. Like the Google Now, the
Google Assistant can search the internet, schedule events, change the
hardware setting of your device, show data from Google account and so
on. Google has also announced that the Google Assistant is able to detect
objects and collect information about them through the camera. It also
supports buying, sending money and identifying songs.
What Is IFTTT?

IFTTT is short for “If This, Then That”. It is a software platform in which
different applications, devices and services can be connected to each other
to create one or several automations. In fact, the IFTTT acts as a
communication interface between them.

This automation is done through Applets. In other words, several programs


are connected to each other to perform automated tasks. You can activate
or deactivate these Applets in “IFTTT.com” website. You can either make
your own Applets or use different Applets already available on the website.

In the next parts of the tutorial, you will learn how to work with the IFTTT
website and make a new Applet.

Required Material

To continue with the tutorial, you need the following components:


Hardware Components

NodeMCU ESP32S Edition Module × 1 

Breadboard × 1 

4 Channel 5v Relay × 1 

5mm Red LED × 1 

330 Ohm Resistor × 1 

10cm Male-Male Jumper Wire × 1 

20cm Female to Female Jumper Wire × 1 

20cm Male-Female Jumper Wire × 1 

Software Apps

Arduino IDE 

Step 1: Establish Connection Between


ESP32 & Adafruit

As already mentioned, we need to use a platform called Adafruit-IO to


connect the ESP32 to IFTTT. In this step, we are going to connect the ESP32
to Adafruit-IO.

The Code to Connect ESP32 to Adafruit-IO

First, we write the code we need. There are also some variables in the code
we will set in the next steps.
Go to Sketch → Include Library → Manage Libraries. Search for “Adafruit
MQTT” and install it like the following image.

The code below is taken from “Github” website. Copy and paste it on your
Arduino IDE.

1 /*

2 Made on 6 July 2021

3 By Amirmohammad Shojaei

4 Home

5 based on “ajaynikam2410” Example

6 */

8 //for esp32 use <wifi.h>, for esp8266 use <esp8266wifi.h>

9 // this code is for esp32 for using esp8266 you just need to change
10 <wifi.h> into <esp8266wifi.h> and also change the relay pin

11 #include <WiFi.h>

12 #include "Adafruit_MQTT.h"

13 #include "Adafruit_MQTT_Client.h"

14

15

16 /************************* Pin Definition


17 *********************************/

18

19 //Relays for switching appliances

20 #define Relay1 12

21 #define Relay2 13

22 #define Relay3 14

23 #define Relay4 27

24 #define buzzer 26

The code above is actually written to control 4 different devices. But in this
tutorial, we use it to control only one device –turning an LED on and off-.

Note

4 parts of the code are yet to be completed. They can have
different values for each user. To complete the code, you should
delete the phrase inside “X” and replace it with your desired
values.
Tip

Do not forget to connect the VCC and the GND of the module to
5V and the GND of Arduino.

Those 4 parts are the following:

WLAN_SSID: This is the name of the Wifi you want to connect the
ESP32 to.
WLAN_PASS: The password of the Wifi.
AIO_USERNAME: This variable will be taken from “Adafruit-IO”
website.
AIO_KEY: This variable will also be taken from “Adafruit-IO” website
later.

In the section describing Adafruit-IO, we will learn how to get the two
variables “AIO_USERNAME” and “AIO_KEY”.

Register and Make a Project in Adafruit-IO

Step 1:
First, go to “io.adafruit.com” and make an account.

Step 2:

Log in to your account. Click on “+ New Dashboard” to make a new project


and choose a title for it.

Step 3:

Now, you can make a new block by clicking on “Create New Block” section
in the menu on the right. In this part, choose “Toggle” block, choose a
name for it and click on “Create”. We have named it “LED”.

Step 4:

Now, you can copy the value of “Username” from “My Key” part and use
that as the “AIO_USERNAME” variable in the code of the previous part. You
can do the same thing for “Active Key” and copy its value for the “AIO_KEY”
variable.
Step 5:

Click on “Edit Layout”, then, click on “Block Info” in the settings section. The
last part of the “API URL”, meaning “/feed/led”, must be written in a part of
the code. Do that according to the pictures below.

Full Video of the Steps in Adafruit-IO Website


The video below shows how to do the above stages in a detailed way:

Upload the Code on ESP32

We obtained the two parameters “AIO_USENAME” and “AIO_KEY” in the


previous part. We also need to set the parameters “WLAN_SSID” and
“WLAN_PASS” which are the Wifi username and password.

After setting these 4 parameters, we can upload the code on ESP32.

Note

While uploading, press and hold the “Boot” button to correctly
upload the code on ESP32.

Make sure you have chosen the right Board and PORT, like the image
below.

Once the code is successfully uploaded, open the Serial Monitor and set its
baud rate to 115200. If everything has gone right, you should see the
message “MQTT Connected!” on the output of Serial Monitor This means
message “MQTT Connected!” on the output of Serial Monitor. This means
the connection between the ESP32 and Adafruit-IO has been successfully
established.

Step 2: Circuit

After uploading the code, connect the ESP32 to the relay module and the
LED. The following image shows how you can do that.

Step 3: Connect IFTTT with Google


Assistant & Adafruit-IO
First, go to “ifttt.com” and create an account.

Next, click on “Create”. At this moment, you will face two options: “This”
and “That”. “This” is used for communicating with input and “That” is used
for connecting the “IFTTT” to output. In this tutorial, the Google Assistant
and Adafruit-OI communicate with the IFTTT as the input and output,
respectively.

In other words, the IFTTT platform acts as a communication interface


between Google Assistant and Adafruit-IO.

Connect to Google Assistant

To connect to Google Assistant, do the following steps:

Step 1:

After selecting the “This” option, search the phrase “Google Assistant” and
open it.

Step 2:

Select “Say a simple phrase”.

Step 3:

Write down your voice command in 3 different ways. Also, write the
response you want to get from Google Assistant. At the end, select your
desired language and click on “Create Trigger”.

For example, here are the setting for turning an LED on and off.
Note

Sometimes you need to confirm your email address to continue.

Of course, try to create all your accounts with the same email
address so that there would be no problem in setting up.

Connect to Adafruit-IO

To connect to Adafruit-IO, do the following steps:

Step 1:

Click on “That”, search the word “Adafruit” and select it.

Step 2:

Select “Send data to Adafruit-IO”, then, select the name of the created
block. You should also enter the desired pin value in ESP32 when the voice
command in sent.

Note

If the relay module you’re using is Active Low, you should set the
ESP32 pin to 0.

And if it is Active High, set the ESP32 pin to 1.

In this tutorial, we have used an Active Low relay module, that’s


h h t th ESP32 i t 0
why we have set the ESP32 pin to 0.

Finally, click on “Continue”, then, “Finish”.

Do the same steps above for turning off the LED. The difference is that in
the first part, you should use the word “Off” instead of “on”; and in the
second part, put the value 1 instead of 0.

The two new created blocks can be seen in “My Applets” section.

Note

In the free trial of the “IFTTT” website, you are able to create only
three new Applets.

The Video of the Steps in IFTTT Website

You can see how to do the above steps in a detailed way in the video
below:

Step 4: Send the Voice Command


Through Google Assistant

In this step, turn on the internet of your cell phone and open the Google
Assistant. Say out loud the voice commands that you have entered the
“IFTTT” website and see how the LED status changes. You can see the LED
turning on and off in the video below.
0:00 / 0:43

What’s Next?

Use other boards, like NodeMCU ESP8266


Control the light of the LED using PWM
Control 4 different devices
Provide a new way to connect Google Assistant to the microcontroller
Replace Adafruit-IO with Blynk application

Liked What You See?


Get Updates And Learn From The Best

Enter Your Email Send 

PREVIOUS NEXT
 
Play with Fire Over WIFI! ESP8266 … Improve Hard Drives Security by A…

More To Explore

Arduino Arduino
Interfacing 3D Printer Interfacing DS3231 Real
20X04 LCD Smart Time Clock RTC Module
Controller With Arduino With Arduino

Mohammad Damirchi Mehran Maleki

Comments (28)

Farshid Reply
Thanks a lot for sharing
The server part is a bit vague to me. what should we do with the zip file? and where should we get the “URL of
our Server to receive data”?
December 26, 2018 at 4:47 pm

Mehran Maleki Reply


Hi, here is the URL you need: “http.begin(“http://31.184.135.137/api/Home/GetAllApp”
December 9, 2020 at 8:17 am

wewtr Reply
Hi, gif is so fast , that I am not able to read what’s in.
October 30, 2019 at 1:28 pm

Saeed Hosseini Reply


Hi, it is just a sample
November 2, 2019 at 6:05 am

Félix-Antoine Bédard Reply


Hi,
I can’t figure out where I can get the URL to enter in the first line of “that” on IFTTT. I’m new with this, what is
this URL? I found a link on another site and the link simply didn’t work.
Thanks for your support
F-A
February 25, 2020 at 4:10 pm

Saeed Hosseini Reply


Hi.
Here it is. “http.begin(“http://31.184.135.137/api/Home/GetAllApp”);”
March 1, 2020 at 6:12 am

Matteo Marchi Reply


What do we do with the Server part?
download it, unzip it and?
March 21, 2020 at 11:34 am

Mehran Maleki Reply


You can check the following link for more details. “https://www.instructables.com/Control-
House-Lights-With-Google-Assistant-Using-A/”
December 9, 2020 at 9:23 am

Josue Reply
Hello .
How can i send data from Arduino to Google Assistant?
March 22, 2020 at 9:05 am

Josue Reply
Hello…
Thanks a lot for the article it is really helpfull.
I would like to send data from arduino to Google Assistant , Do you know how do that?
Thanks in advance.
March 23, 2020 at 12:00 am

Mehran Maleki Reply


Hello.
First you need to alter the code provided for ESP-01 in the article to get data from Arduino via
Serial port and send it to the database. Then change the other code for Arduino to send data
to ESP-01. It’s pretty much like the original code of the this article. Good luck!
December 9, 2020 at 8:22 am

Ryan OKeefe Reply


For the URL and server you would need to use an app like Blynk or an amazon hosting server. You can watch
this video https://www.youtube.com/watch?v=5SvRolROPxA
April 12, 2020 at 1:59 pm

Mehran Maleki Reply


Thanks so much for sharing!
December 9, 2020 at 6:11 am

Neil B Reply
I am getting a ESP8266Wifi.h: No such file or Directory Error (SEE BELOW:
Arduino: 1.8.10 (Mac OS X), Board: “Arduino/Genuino Uno”
ESP_WIFI:1:10: error: ESP8266WiFi.h: No such file or directory
#include “ESP8266WiFi.h”
^~~~~~~~~~~~~~~
compilation terminated.
exit status 1
ESP8266WiFi.h: No such file or directory
This report would have more information with
“Show verbose output during compilation”
option enabled in File -> Preferences.
April 12, 2020 at 11:38 pm

Amir Mogoeir Reply


Have you added the ESP8266WiFi.h library? Make sure it is properly added and let us know if
the error persists.
May 30, 2020 at 1:18 pm

Malika Reply
follow the first few steps of the link below to add the esp8266 board.
https://create.arduino.cc/projecthub/electropeak/getting-started-w-nodemcu-esp8266-on-
arduino-ide-28184f
then in Tools->Board select the esp8266 board before uploading your code.
June 4, 2020 at 6:07 am

SAMRA KHAN Reply


Hey, do we need premium iftttp for this or the normal one will work?
February 18, 2021 at 12:15 pm

MIKU Reply
hello,
how can i build a project like jarvis in iron man. that can be answer my question?
June 3, 2021 at 6:22 pm

Mehran Maleki Reply


Hi.
To get a good answer, you better give a little more details of the project you intend to do. You
can also take a quick scan over our tutorials in this link and see if any of them can be useful for
you. “https://electropeak.com/learn/”
June 4, 2021 at 10:33 am

milli Reply
Hi, I have to do this project with a bit different feature. can you please help me?
All I have to do is when we tell google assistant that I’m busy, the led color changes to red and when we tell
itthat i’m available now, the led color changes to green.
Being a beginner i don’t know how to do that.
November 10, 2021 at 11:48 am

Mehran Maleki Reply


Hi.
The project you want to do is quite easy and almost the same as the project done in this
tutorial. So, first, you better do the exact project explained in this article to get a good view on
how you can make that small change and do your own project.
In overall, all you need to do is use an RGB LED, like this one: https://electropeak.com/4-pin-
rgb-led. It has four pins, 3 for Red, Blue and Green and the other one is the common cathode
pin. Using this LED, you can easily change its color using Google Assistant.
November 13, 2021 at 6:07 am

Mastaf Reply
Hello and thanks for that nice job.
I’m working on a project and you helped me so much !
I’m stucked with a piece of code. What I’ve done : IFTTT and Adafruit are set up, I have 2 applets. The first is a
slider which manage the brightness of a ws2812 ledstrip : when I say “hey google, put the light at 40 %”, it
sends 40 to the esp32 and with :

uint8_t Brightness_State = atoi((char *)Brightness.lastread);
Brightness_State = map(Brightness_State, 0, 100, 0, 255);

I’m able to adjust brightness perfectly and it works !
The second one is a text applet, when I say “Hey google, set the light to blue”, it sends blue to the esp. I have 8
different colors so I decided to use a switch in the code. I first made an enum to set colors as int:
enum Colors {rouge, orange, jaune, verte, turquoise, bleue, violette, rose, blanche};
it works !
Serial.print(F(“Reçu: “));
Serial.println((char *)Couleur.lastread);
unsigned char * colors = (Couleur.lastread); (IT DOESN’T WORK because colors is not an int and the switch
needs an int for colors!)
uint8_t Couleur_State;
switch (colors) {
case rouge:
Couleur_State = 0;
break;
case orange:
Couleur_State = 32;
break;
case jaune:
Couleur_State = 64;
break;
case verte:
Couleur_State = 96;
break;
case turquoise:
Couleur_State = 128;
break;
case bleue:
Couleur_State = 160;
break;
case violette:
Couleur_State = 192;
break;
case rose:
Couleur_State = 224;
break;
case blanche:
Couleur_State = 255;
break;
}
for(int i = 0; i < NUM_LEDS; i++) {
leds[i] = CHSV(Couleur_State,255,255);
};
FastLED.show();
Serial.println(Couleur_State);

How can I convert ((char *)Couleur.lastread) to an int or is there a way to do that ?
January 3, 2022 at 4:03 pm

Mehran Maleki Reply


Hi.
You’re quite welcome! The input to a “switch case” must be an integer or a single character. So,
in this case that you’re working with strings, you either need to use “if, else if” instead of
“switch case” OR you can define a 2D string array assigning each color to a specific number.
Then you can read the color, specify the number assigned to it, and use that as the case in
your “switch case” statement.
January 4, 2022 at 7:12 am

Mastaf Reply
Hello,
Thanks for your fast reply 😉 I’ve managed in the night with “if and else if”
solution. It works….except for special effects with moving lights, they are
freezed.
I’m using ESP32 module, so I decided to split the code on both cores as it
exploits a little bit more the chip. I also use 1 push button (to switch between
special effects in manual mode) and 2 potentiometers ( one to set brightness
manually and the other one to switch between hue colors). When I use the
push button special effects run normally, but when google calls it (the special
effect) within the “if, else if”, it is switched on but it’s freezed (no moving lights
as it should). So the next step is to try with the 2D string array in a “switch case”
as it seems to not freezing the code… Have a nice day !
January 4, 2022 at 10:07 am

Mehran Maleki Reply


Wish you good luck! You have a nice day too.
January 5, 2022 at 5:41 am

Dawid Reply
First of all, many thanks for this article. My problem is that the serial monitor displays such messages from
time to time (I will add that I have entered correctly WLAN_SSID” and „WLAN_PASS” ):
ets Jan 8 2013, rst cause: 4, boot mode: (3,6)
wdt reset
load 0x4010f000, len 1384, room 16
tail 8
chksum 0x2d
csum 0x2d
v482516e3
~ ld
⸮ r r⸮n # ⸮phAdafruit MQTT demo
Connecting to DawidziuNET
January 7, 2022 at 12:52 am

Dawid Reply
I corrected the library- My mistake sorry 🙂
January 7, 2022 at 1:07 am

Mehran Maleki Reply


It’s alright!
January 8, 2022 at 7:25 am

Leave a Reply

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

Comment

Name *

Email *

Website

Save my name, email, and website in this browser for the next time I comment.

Je ne suis pas un robot


reCAPTCHA
Confidentialité - Conditions

POST COMMENT
COMPANY MY ACCOUNT CUSTOMER SERVICE

About us My Wishlist FAQ

Contact us My Account Return Policy

My Account Order History Payment & Shipping

ElectroPeak Inc. © 2019. All Rights WORKING DAYS/HOURS

Reserved Mon – Fri / 9:00-18:00 GMT+8

© All rights reserved      

You might also like