[go: up one dir, main page]

Home Automation Using Google Assistant

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

Home Automation Using Google Assistant

To control any components connected to NodeMCU we can make use of IFTTT,


Google assistant and io.adafruit.com. In this experiment we will control a bulb
and fan connected to NodeMCU. After that by using Google assistant we’ll make a
voice controlled home automation system.

 The concept here is to use IFTTT which stands for ‘If This Then That’.
IFTTT helps us to create different commands to perform on the house which we
call as applets. We’ll learn about creating applets and other stuff later. For now
IFTTT basically connects our Google assistant to io.adafruit.com. We are gonna
use Google assistant to send the commands and io.adafruit.com is going to
understand them.
 io.adafruit.com is basically an interface used to create relays and other
events which our code will be using to control the house components. This
io.adafruit.com has an AIO key which will be used in our code. This code is
dumped on the NodeMCU using Arduino IDE. By using relays our NodeMCU is
connected to the components that we’ll be controlling such as a bulb or fan.

Block diagram :
Components:

1. NodeMCU board (quantity: 1 no.)


2. Micro USB A to B (quantity: 1 no.)
3. Breadboard (quantity: 1 no.)
4. Bulb and Fan
5. Jumper wires
6. Relays
7. Io.adafruit.com account
8. Ifttt.com account
9. Google assistant on phone
10. Adafruit MQTT library
11. Arduino IDE on your computer

Procedure:
1. Install Google Assistant on phone
2. Create an Adafruit account

 Open a browser and go to io.adafruit.com. Click on Get started for free.

 Fill in your details and click on Create Account. Note that the site would
have navigated to accounts.adafruit.com.

 Once you’ve successfully logged in, this page will be displayed. You can
change your details if you wish to.
Now, create dashboard at Adafruit. This dashboard is a user interface to
control things remotely.

After following above steps, provide name to the dashboard and save it.
We can see our dashboard as follows,

Now, create feed (user interface) to control light On-Off. To create it, just
click on ‘+’ symbol and select toggle feed shown below,

After selecting toggle feed, pop-up window appears as shown below.


Enter name of our feed (shown in red box) and create it. After creation,
select the feed (here mine is light and fan) and then click on Next Step.

Here, We used 0(OFF) and 1(ON) text for button and then click on create. This
will create toggle button on your dashboard which can be used to control things
remotely.
Now, my dashboard is ready for IoT application like home automation.
IFTTT (If This Then That)
If This Then That, also known as IFTTT is a free web-based service to create
chains of simple conditional statements, called applets. An applet is triggered by
changes that occur within other web services such as Gmail, Facebook, Telegram,
Instagram, or Pinterest.
For example, an applet may send an e-mail message if the user tweets using a
hashtag or copy a photo on Facebook to a user's archive if someone tags a user in
a photo.
Here, I used IFTTT to use google assistant service and Adafruit service in chain.
So, when I use google assistant to control light of my home by saying Ok google,
turn the light ON or OFF. Then IFTTT interpret the message and can send it to
Adafruit’s dashboard as a understandable command to the created feed.

Configure IFTTT:
First step is to create an account on IFTTT.
Note: Create account on IFTTT by using same e-mail id which you have used for
Adafruit.

After account creation, click on My Applets and then select New Applet shown
below,

After selecting a new applet, we get a new page in which we should click on to
THIS as shown in the below image.
Now enter the voice phrases which we will use as a command for Google
Assistant.

We can enter any phrase as per our application. As you can see, the phrases
entered in the above fields is for making Light ON. For making Light OFF, we
have to create another applet with different phrases.

Now, we get another page on which we have to click on that option which is used
to connect Google Assistant with Adafruit.
Then search for ADAFRUIT and select it

After selecting ADAFRUIT, choose action as shown below,


Now enter what data we need to send to which feed of Adafruit dashboard.

Click on Create Action.


So, when I use Google Assistant on my mobile and give voice command as “Ok
Google, Turn LED ON”, applet created in IFTTT receive this command and will
send data ‘1’ to the Adafruit feed. This will trigger the event on Adafruit
dashboard which is continuously monitored by the microcontroller (here
NodeMCU). This microcontroller will take action as per the data change on the
Adafruit dashboard.
Interfacing Diagram:
Library
Here, I used the Adafruit MQTT library for receiving data from the Adafruit
server. To install this library, select option Sketch -> Include Library ->
Manage Libraries.
In that library, search for Adafruit MQTT and installed it.
Control Home’s Light using Google Assistant and NodeMCU
I build an IoT based home automation application in which I control the 100 W
bulb at remotely using AI based Google Assistant.
Here, I used NodeMCU to read data from Adafruit server and act accordingly. 100
W bulb connected to NodeMCU via relay for controlling it voice command using
google assistant.

Program:

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

#define Relay1 D1

#define WLAN_SSID "Enter SSID NAME" // Your SSID


#define WLAN_PASS "SSID Password" // Your password

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

#define AIO_SERVER "io.adafruit.com"


#define AIO_SERVERPORT 1883 // use 8883 for SSL
#define AIO_USERNAME "Enter your UserName" // Replace it with your
username
#define AIO_KEY "Enter you AIO_KEY" // 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/Enter your Feed Name"); // FeedName

void MQTT_connect();

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

pinMode(Relay1, 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);
}

void loop() {

MQTT_connect();

Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(5000))) {
if (subscription == &Light1) {
Serial.print(F("Got: "));
Serial.println((char *)Light1.lastread);
int Light1_State = atoi((char *)Light1.lastread);
digitalWrite(Relay1, !(Light1_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!");
}

Result:

You might also like