Speak To Your ESP32 & Control It by Google Assistant - ElectroPeak
Speak To Your ESP32 & Control It by Google Assistant - ElectroPeak
[porto_block name="current_post_header"]
Table of Contents
1. Overview
2. What Is Adafruit-IO?
4. What Is IFTTT?
5. Required Material
7. Step 2: Circuit
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.
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.
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?
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.
In the next parts of the tutorial, you will learn how to work with the IFTTT
website and make a new Applet.
Required Material
Breadboard × 1
4 Channel 5v Relay × 1
Software Apps
Arduino IDE
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 /*
3 By Amirmohammad Shojaei
4 Home
6 */
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
18
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.
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”.
Step 1:
First, go to “io.adafruit.com” and make an account.
Step 2:
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.
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.
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.
Step 1:
After selecting the “This” option, search the phrase “Google Assistant” and
open it.
Step 2:
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
Step 1:
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.
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.
You can see how to do the above steps in a detailed way in the video
below:
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?
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
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
wewtr Reply
Hi, gif is so fast , that I am not able to read what’s in.
October 30, 2019 at 1:28 pm
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
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
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
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
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
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
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
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
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.
POST COMMENT
COMPANY MY ACCOUNT CUSTOMER SERVICE