ESP32IOT Projects

IFTTT Google Assistant Voice and Adafruit io based Home Automation using ESP32

IFTTT, Google Assistant, & Adafruit io:

IFTTT Google Assistant Voice and Adafruit io based Home Automation using ESP32– In this article, you will learn how to make an IoT based Voice controlled Home Automation system using IFTTT, Google Assistant Voice, and the Adafruit IO. This advanced level Home Automation system is based on the ESP32 WiFi + Bluetooth module. I will explain each and every detail including how to create a very simple dashboard in Adafruit io, how to create different applets for voice commands in IFTTT using Google Assistant, and how to link all these different IoT platforms to make a completely functional Home Automation system or smart home system.

Since this project is based on the IoT “Internet of Things” so this Home Automation system can be controlled from anywhere in the world. For the demonstration purposes, I have used an electronic door lock and a 110/220Vac light bulb. If you want you can use some other types of loads and of course you can increase or decrease the number of loads; it’s totally up to you.

Anyways, before, I am going to explain the circuit diagram, IFTTT Google assistant settings, and programming; first, let’s watch this Home Automation System in action.

ifttt google assistant voice

My 12Vdc electronic door lock and the 220Vac light bulb are connected with the relays. I have already powered up my ESP32 board and it’s connected with the WiFi. When everything is powered up never touch the relays contacts as 220Vac can be really dangerous. So, when everything is ready, simply go ahead and open the Google assistant and start controlling your electrical devices using voice commands

Open the Google Assistant on your mobile phone, click on the mic and say the command which are predefined while making the Applets in IFTTT. Watch video tutorial for the practical demonstration.

So, you can control different electrical appliances using voice commands, or you can use a dashboard on your laptop or cell phone to control the same devices from anywhere in the world. I designed my dashboard using the Adafruit io.

ifttt google assistant voice

The Adafruit io is unbelievably fast, I don’t see any delay and I think the Adafruit io can be a perfect choice for the real-time sensors monitoring over the long distance.

I have been testing this Voice Controlled Home Automation system for a while and I didn’t see any false triggering and long delays. The IFTTT Google Assistant and Adafruit io are working perfectly as a team and using these IoT platforms we can make amazing monitoring and control systems. So, anyways you have got the idea of what exactly you are going to learn after reading this article. So, without any further delay, let’s get started!!!


Amazon Links:

12v Adaptor:

ESP32 WiFi + Bluetooth Module(Recommended)

LM7805 Voltage Regulator:

470uf capacitor:

DC Female Power Jack:

Female Headers:

1n4007 diode:

10k Resistor:

2n2222 NPN transistor

12V SPDT Relay:

Other Tools and Components:

Top Arduino Sensors:

Super Starter kit for Beginners

Digital Oscilloscopes

Variable Supply

Digital Multimeter

Soldering iron kits

PCB small portable drill machines

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!



ESP32 Home Automation Circuit Diagram:

ifttt google assistant voice

J1 is the female power jack and this is where we connect a 12v adaptor, battery or a solar panel. Two 470uf capacitors are connected at the input and output sides of the voltage regulator. The output of the voltage regulator is connected with the 5v pin of the ESP32 module and the ground of the power supply is connected with the ground of ESP32 module.

These are 12v SPDT type relays and can’t be directly controlled using the ESP32 Module, So, that’s why we need a driver to control these relays. You can use a relay driver IC or you can use 2n2222 NPN transistor and a 10k resistor. One pin of the relay coil is connected with the collector of the 2n2222 NPN transistor while the other pin of the relay coil is connected with the 12 volts. The emitter of the transistor is connected with the ground while the base is connected with the 10k ohm resistor.

Now to control these relays you simply need to connect these 10k resistors with the ESP32 I/O pins. In this project I am using the GPIO pins 13, 12, 14, and 27. I will be using the same pins in the programming.

For, now I am using only two relays. Relay1 I am using for controlling the Electronic Door Lock and Relay2 I am going to use for controlling a light bulb.

ifttt google assistant voice

Here is my ESP32 development board which I use for testing my ESP32 based projects. If you want to make the same ESP32 development board then read my previous article on the ESP32 based Home Automation using Blynk application and the android cell phone App.

ifttt google assistant voice

I connected the bulb and the electronic door lock as per the circuit diagram. Now, let’s start with the Adafruit io.


Altium Designer:

altium designer sponsor

Altium Designer is the world’s most trusted PCB design system. Altium Designer enables engineers to effortlessly connect with every facet of the electronics design process. Over 35 years of innovation and development focused on a truly unified design environment makes it the most widely used PCB design solution. With Altium Designer you can create PCB designs with an intuitive and powerful interface that connects you to every aspect of the electronics design process. Route it your way through any angle, tune for the delay, Push, Slide, and Walkaround faster than ever. Interact and collaborate with mechanical designers like never before in a photo-realistic, 3D design environment. If you want to get started with the Altium designer, you can click on the get started.


Adafruit IO:

First, we are starting with the Adafruit IO. We will make a very simple dashboard consisting of two toggle buttons. Later if you want, you can increase the number of buttons.

https://io.adafruit.com/

Once you have opened the io.adafruit.com page; then you can click on the Get Started for Free to create a free account.

ifttt google assistant voice

Then we will give some information by filling the following form and click on the create account.

ifttt google assistant voice

Once you have created your Adafruit account then you will need to reopen the Adafruit io. Click on the dashboard. Then click on the new dashboard and add the name of the project which is home automation and in the description give some information about the project and click on the create button.

ifttt google assistant voice

After that we will open the home automation on the dashboard and then click on the create new block

ifttt google assistant voice


Then in order to create new block we will click on the toggle button

ifttt google assistant voice

Then we will create the relay1 and relay2 variables and click on the create button.

ifttt google assistant voice

Then select the relay1 and click on the Next Step in which we have to fill the table in which will give block title as light and in the button on text we will write 0  and 1, and then click on the create button.

ifttt google assistant voice

In the similar we will create another button for the door lock to open and close it.

 

 

ifttt google assistant voice

Now the adafruit task is completed and we can turn on and off the device from these buttons if we have uploaded the code to the ESP32.  Now we will interface the Adafruit  with the IFTTT. On the top you can see My Key, if you click on this you will see your username and the key, which we will later use in the ESP32 programming.



Interfacing Adafruit with IFTTT:

First of all we will create an account in the IFTTT for which will click on the get started and continue with Google account.

ifttt google assistant voice

After creating the account in IFTTT we will click on the create button in order to interface it with Google assistant.

ifttt google assistant voice

Then in the search button we will search for the Google assistant and click on it.

ifttt google assistant voice

Then click on the “say a simple phrase”  and click on the connect then we will click on the allow button. Now simple phrase will be open in which we will give the command with which we want to turn on our light. So we will fill up the fields.

ifttt google assistant voice


Then click on the create trigger button.

ifttt google assistant voice

Now the if part is completed and now we have to create then that part so click on that

ifttt google assistant voice

When we will click on that a search bar will open in which we will search for the Adafruit and click on it.

ifttt google assistant voice


After that we will click on the send data to Adafruit IO and click on the connect button. After that a page will open in which we will click on the authorize button.

ifttt google assistant voice

Then we will select the relay 1 and in the send data we will give 1 so that If we say light on it will trigger the relay.

ifttt google assistant voice

The same process will be used for the led off section the only change will be that in the data save we will send 0. The similar process will be used for the relay 2. Now all the process is completed.


ESP32 Home Automation Programming:

Before, you start the programming, first of all, make sure you install the Adafruit_MQTT library. For this click on the Sketch menu, go to Include Library, and then Click on the Manage Libraries. Search for the Adafruit MQTT library and install the latest version.

ifttt google assistant voice

After installing the library we have to go to the Adafruit io and click on the My Key.

ifttt google assistant voice

Now when we will click on the key we will receive the following information.

ifttt google assistant voice

This information will be used in the code to interface the ESP32 with Adafruit.



Complete Code:

#include <WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

#define Relay1   12
#define Relay2   13
#define Relay3   14
#define Relay4   27
int LED_BUILTIN = 2;

#define WLAN_SSID       "AndroidAP3DEC"             // Your SSID
#define WLAN_PASS       "password"        // Your password

/************************* Adafruit.io Setup *********************************/

#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  1883                   // use 8883 for SSL
#define AIO_USERNAME    "electroclinic"            // Replace it with your username
#define AIO_KEY         "aio_oFom78I2eRk7oxOanpdMFJIEma8W"   // Replace with your Project Auth Key

/************ Global State (you don't need to change this!) ******************/

// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// or... use WiFiFlientSecure for SSL
//WiFiClientSecure client;

// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);

/****************************** Feeds ***************************************/


// Setup a feed called 'onoff' for subscribing to changes.
Adafruit_MQTT_Subscribe Light1 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME"/feeds/relay1"); // FeedName
Adafruit_MQTT_Subscribe Light2 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/relay2");
Adafruit_MQTT_Subscribe Light3 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/Relay3");
Adafruit_MQTT_Subscribe Light4 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/Relay4");



void MQTT_connect();

void setup() {
  Serial.begin(115200);

  pinMode(Relay1, OUTPUT);
  pinMode(Relay2, OUTPUT);
  pinMode(Relay3, OUTPUT);
  pinMode(Relay4, OUTPUT);
   
  pinMode(LED_BUILTIN, OUTPUT);
  // Connect to WiFi access point.
  Serial.println(); Serial.println();
  Serial.print("Connecting to ");
  Serial.println(WLAN_SSID);

  WiFi.begin(WLAN_SSID, WLAN_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();

  Serial.println("WiFi connected");
  Serial.println("IP address: "); 
  Serial.println(WiFi.localIP());
 

  // Setup MQTT subscription for onoff feed.
  mqtt.subscribe(&Light1);
  mqtt.subscribe(&Light3);
  mqtt.subscribe(&Light2);
  mqtt.subscribe(&Light4);
}

void loop() {
 
  MQTT_connect();
  

  Adafruit_MQTT_Subscribe *subscription;
  while ((subscription = mqtt.readSubscription(20000))) {
    if (subscription == &Light1) {
      Serial.print(F("Got: "));
      Serial.println((char *)Light1.lastread);
      int Light1_State = atoi((char *)Light1.lastread);
      digitalWrite(Relay1, Light1_State);
      digitalWrite(LED_BUILTIN, Light1_State);
      
    }
    if (subscription == &Light2) {
      Serial.print(F("Got: "));
      Serial.println((char *)Light2.lastread);
      int Light2_State = atoi((char *)Light2.lastread);
      digitalWrite(Relay2, Light2_State);
    }
    if (subscription == &Light3) {
      Serial.print(F("Got: "));
      Serial.println((char *)Light3.lastread);
      int Light3_State = atoi((char *)Light3.lastread);
      digitalWrite(Relay3, Light3_State);
    }
    if (subscription == &Light4) {
      Serial.print(F("Got: "));
      Serial.println((char *)Light4.lastread);
      int Light4_State = atoi((char *)Light4.lastread);
      digitalWrite(Relay4, Light4_State);
      
    }
  }

  
}

void MQTT_connect() {
  int8_t ret;

  // Stop if already connected.
  if (mqtt.connected()) {
    return;
  }

  Serial.print("Connecting to MQTT... ");

  uint8_t retries = 3;
  
  while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
    Serial.println(mqtt.connectErrorString(ret));
    Serial.println("Retrying MQTT connection in 5 seconds...");
    mqtt.disconnect();
    delay(5000);  // wait 5 seconds
    retries--;
    if (retries == 0) {
      // basically die and wait for WDT to reset me
      while (1);
    }
  }
  Serial.println("MQTT Connected!");
  
}

After uploading the code to the ESP32 we will open the Google assistant if we do not have Google assistant in mobile we can download it from the play store and when we will say light on, the light will turn ON. One thing must to remembered that the Google assistant must be signed in with the same email which we have used in the IFTTT and Adafruit. The E-mail should be same for all these application.

Watch Video:

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

One Comment

  1. Hey, can you please help me to do this project by adding sensors and operating through MQTT dash application.

Leave a Reply

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

Back to top button