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.
To continue with the tutorial, you need the following components:
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.
/* Made on 6 July 2021 By Amirmohammad Shojaei
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-.
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.
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
First, go to “io.adafruit.com” and make an account.
Log in to your account. Click on “+ New Dashboard” to make a new project and choose a title for it.
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”.
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.
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.
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 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:
After selecting the “This” option, search the phrase “Google Assistant” and open it.
Select “Say a simple phrase”.
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.
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:
Click on “That”, search the word “Adafruit” and select it.
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.
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 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.
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.
- 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