Google assistant based home automation system
Electronics tutorial ESP32 Internet of things IOT

Google Assistant Based Home Automation System ESP32, Blynk & IFTTT

Control your home lighting and fans using this Google Assistant based home automation system. This is very easy to set up, simple to operate, and a cost-effective system for your home residence and even offices. You can operate it via google assistant, manual buttons, and Blynk application.

What are the things required for our project:

  • ESP32 Development Board
  • Relay Module (2 channel, 4 channel or even more)
  • Goggle Assistant
  • Blynk IOT Platform
  • IFTTT Webhooks Service
  • Arduino IDE

Home Automation System using ESP32

This is a little big project if you are a beginner to the world of IOT based projects. Here we will use different platforms and integrate them to make our home automation system. Now I will guide you step by step throughout this project, follow all the steps correctly.

Google Assistant Based Home Automation System:

home automation using google assistant block diagram

Step One Blynk App:

Step one is to configure the Blynk application

Download the Blynk app from play store or app store or you can go through the website.

Sign up using your email account or Sign up with your Google account.

Create a new project and give it a name, choose device type ESSP32 from the list.

Chose connection type Wi-Fi from given options, and press create.

You will receive an auth token at your registered email account. Do not share it, anyone having this code can control your device.

Click on the “+” icon and add a button from widgets, here you will find various types of widgets.

Click on the button and edit it, chose the pin number of ESP32 which you want to control? ( in our case it is 25).

Now choose the ON state to 0 and OFF state to Logic 1, this is an active-low logic signal. Select the mode to switch instead of the push-button.

Google assistant based home Automation

Step Two ESP32:

In step 2 we will set up our ESP32 module.

Download the Arduino sketch file and open it in Arduino IDE.

Paste the AuthToken generated from the Blynk app in the define section.

Enter your Wi-Fi SSID and password where it is mentioned.

Connect the relay Module to the ESP32 Board at pin 25 and 26.

Upload the sketch to the ESP32 board.

Connect it to your Wi-Fi, now you can control your relay module through Blynk App.

Home Automation System ESP32

Relay Driver Circuit:

This is a relay driver circuit to drive the relay and also to isolate and protect microcontroller ESP32 from high voltage AC. We have used MOC8711 optoisolator for complete isolation and control.

Relay Driver Circuit

Step Three IFTTT:

This is the final step where we will connect our Google assistant to ESP32 via the Blynk server using IFTTT webhooks service.

Download IFTTT mobile application or go to the website sign up to IFTTT with your email or Google account and get started.

IFTTT webhook applet

Here you can create various conditional applets. IFTTT stands for “If This Then That” if one event occurs then what to do next?

Google Assistant:

Create your first applet, click on create then select “If This” condition, and then select Google assistant. Select say a simple phrase. Add your phrase, Add what the Assistant will say in response, and continue.

IFTTT webhooks setup

Webhooks:

In the “Then That” section select webhooks and configure it accordingly. To do this we need the IP address of Blynk cloud.

188.166.206.43 is the IP address of blynk-cloud.com, you can get IP of the website by running the ping blynk-cloud.com command in CMD.

https://188.166.206.43/Blynk Auth Token/update/D26 

This is the webhooks URL, you need your Auth Token in order to access your application. Place your Auth Token in the URL.

D26 is the pin number which we are going to control.

Select Put method, application type as application JSON, and in the body type [“0”] and continue. Review your applet and finish you have successfully created your applet.

You need to create two applets one is to turn on and the other one to turn off. The previous one was to turn off and the second one will be to turn on, you need to change the triggering word and JSON value to [“1”] and everything else will remain the same.

IFTTT webhooks url

Now login to the Google app with the same Google account and access to Google assistant. Say the phrases to turn on and off your electrical appliances.

Google assistant based home automation system

With this home automation system, you can control your one device in three ways

  • Google Assistant
  • Blynk Application
  • Manual Buttons
 

This is a complete home automation solution you can control a large number of devices via these applications, all you need is to modify the code for new connections. Also, you can buy paid plans for those applications where you will get some extra features.

Google Assistant Based Home Automation System By ESP32, Blynk & IFTTT:

Watch The complete video.

Code Google Assistant Based Home Automation System:

#include <BlynkSimpleEsp32.h>
BlynkTimer timer;

#define RelayPin1 26 //D5
#define RelayPin2 27 //D25

#define SwitchPin1 32 //D32
#define SwitchPin2 33 //D15

#define wifiLed 25 //D2

#define VPIN_BUTTON_1 V1 
#define VPIN_BUTTON_2 V2

int toggleState_1 = 1; //Define integer to remember the toggle state for relay 1
int toggleState_2 = 1; //Define integer to remember the toggle state for relay 2

int wifiFlag = 0;

#define AUTH "AUTH TOKEN" // You should get Auth Token in the Blynk App. 
#define WIFI_SSID "WIFI NAME" //Enter Wifi Name
#define WIFI_PASS "WIFI PASSWORD" //Enter wifi Password


void relayOnOff(int relay){

switch(relay){
case 1: 
if(toggleState_1 == 1){
digitalWrite(RelayPin1, LOW); // turn on relay 1
toggleState_1 = 0;
Serial.println("Device1 ON");
}
else{
digitalWrite(RelayPin1, HIGH); // turn off relay 1
toggleState_1 = 1;
Serial.println("Device1 OFF");
}
delay(100);
break;
case 2: 
if(toggleState_2 == 1){
digitalWrite(RelayPin2, LOW); // turn on relay 2
toggleState_2 = 0;
Serial.println("Device2 ON");
}
else{
digitalWrite(RelayPin2, HIGH); // turn off relay 2
toggleState_2 = 1;
Serial.println("Device2 OFF");
}
delay(100);
break; 
default : break;
} 
}


void with_internet(){
//Manual Switch Control
if (digitalRead(SwitchPin1) == LOW){
delay(200);
relayOnOff(1); 
Blynk.virtualWrite(VPIN_BUTTON_1, toggleState_1); // Update Button Widget 
}
else if (digitalRead(SwitchPin2) == LOW){
delay(200);
relayOnOff(2); 
Blynk.virtualWrite(VPIN_BUTTON_2, toggleState_2); // Update Button Widget
}

}
void without_internet(){
//Manual Switch Control
if (digitalRead(SwitchPin1) == LOW){
delay(200);
relayOnOff(1); 
}
else if (digitalRead(SwitchPin2) == LOW){
delay(200);
relayOnOff(2);
}

}

BLYNK_CONNECTED() {
// Request the latest state from the server
Blynk.syncVirtual(VPIN_BUTTON_1);
Blynk.syncVirtual(VPIN_BUTTON_2);
}

// When App button is pushed - switch the state

BLYNK_WRITE(VPIN_BUTTON_1) {
toggleState_1 = param.asInt();
digitalWrite(RelayPin1, toggleState_1);
}

BLYNK_WRITE(VPIN_BUTTON_2) {
toggleState_2 = param.asInt();
digitalWrite(RelayPin2, toggleState_2);
}



void checkBlynkStatus() { // called every 3 seconds by SimpleTimer

bool isconnected = Blynk.connected();
if (isconnected == false) {
wifiFlag = 1;
digitalWrite(wifiLed, LOW); //Turn off WiFi LED
}
if (isconnected == true) {
wifiFlag = 0;
digitalWrite(wifiLed, HIGH); //Turn on WiFi LED
}
}
void setup()
{
Serial.begin(9600);

pinMode(RelayPin1, OUTPUT);
pinMode(RelayPin2, OUTPUT);

pinMode(wifiLed, OUTPUT);

pinMode(SwitchPin1, INPUT_PULLUP);
pinMode(SwitchPin2, INPUT_PULLUP);

//During Starting all Relays should TURN OFF
digitalWrite(RelayPin1, toggleState_1);
digitalWrite(RelayPin2, toggleState_2);

WiFi.begin(WIFI_SSID, WIFI_PASS);
timer.setInterval(3000L, checkBlynkStatus); 
// check if Blynk server is connected every 3 seconds
Blynk.config(AUTH);
}

void loop()
{ 
if (WiFi.status() != WL_CONNECTED)
{
Serial.println("WiFi Not Connected");
}
else
{
Serial.println("WiFi Connected");
Blynk.run();
}

timer.run(); // Initiates SimpleTimer
if (wifiFlag == 0)
with_internet();
else
without_internet();
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.