GOOGLE ASSISTANT CONTROLS LIGHT BULB

OK, Google! Call Rohan…Everyone today with an Android phone uses Google Assistant to do all kinds of stuff from phone calls to sending texts. But what if I told you that you could even turn on and off your room's light bulb or fan or any other electronic component using Google Assistant. The same can also be done for DC components such as an LED, Buzzer, DC motor, etc.

Note: Exercise extreme caution when working with live AC voltages since it is extremely dangerous and can cause serious injury.

Let us begin. To achieve this feat, we need an ESP8266 NodeMCU (or any other Wi-Fi MCU), a 5v relay, an Android Phone, and a laptop.

We will start with understanding how things are in brief. When we give the command to Google Assistant, it will trigger some kind of change in ThingSpeak which the NodeMCU will detect and take actions accordingly. If nothing is clear here don’t worry, we will go through each element in detail.

ThingSpeak

ThingSpeak is an open-source cloud platform by MathWorks which can be used for storage or retrieval of data as per our needs using APIs through different protocols like HTTP and MQTT. There are a lot of other cloud platforms that can be used but we will use ThingSpeak as it is very user-friendly and easy to use.

Setting up ThingSpeak

Log in or create a new account on ThingSpeak.

Now you will see a similar screen. Then select the New Channel button as shown.

Now you will be seeing a list of boxes that are hungry to be filled but don't worry we don’t need to fill all the boxes but if you want you can. We just need to name our channel and name the field and save the channel. Now, let’s understand what are fields in our ThingSpeak channel. So, Fields are the place where all your data is stored. Here, we will be using a single field but if you need more fields you just need to select the checkbox and name them. Now you must be wondering why you need to do all these? We are trying to configure a system such that when a Google Assistant command is given a value corresponding to the command is updated in the ThingSpeak field. Now, we can read the values from the ThingSpeak field using our NodeMCU and take action accordingly.

After saving the channel you will see a similar screen. Now go to the API Keys section and note the Write API Key, ChannelID, and Write API request as shown, we will be needing them later.

Now, we are done setting up our ThingSpeak. Now, we would be setting up our Google Assistant.

IFTTT

If This Then That is a web-based service that allows creating a chain of events using different services by using simple if-else conditional statements. It is a great place where you can integrate several services. So, we will use it to enable our Google Assistant to post some value corresponding to a command to the ThingSpeak Channel.

Login or create a new account. (NOTE: Use the same Gmail account with which you will use your Google Assistant)

Now you will see a similar screen. Then select Create as shown.

Now you will see something like this. So, select the Add

It is the service from which an event will be triggered. In our case, it would be Google Assistant. So, search for Google Assistant among the services and select it.

After allowing authentication for your Google Assistant, there will be several options about how you want to give the command to your Google Assistant. So, we will use a simple phrase to give commands to our Google Assistant.

Now fill in what you want to say to trigger your event and Google Assistant’s response. You can also have more than one way of triggering your event.

Now, we will set up our Then Select the Add button on the Then service and search for Webhooks. Webhooks are automated messages sent from apps when something happens.

After authenticating Webhooks, fill the URL section with the Write API request which we noted from our ThingSpeak channel. Also, change the value of the field1 to 1 or any other number. Here, we will use 1 for ON and 0 for OFF for simplicity. Also, change the content type to url-encoded.

With this, we are done creating an applet for turning ON our light. So, now we need to create another applet similarly to turn OFF the light. NOTE: Don’t forget to change the value of Write API request to 0 in the second applet which will be used for turning OFF.

Now, we have completed setting up our Google Assistant and ThingSpeak. So, now we will set up our electronics.

NodeMCU

Now, we need to code our NodeMCU such that it can detect the change in the value in ThingSpeak value and according to the value can control the relay which in turn will control our electric appliance, the electric bulb in our case.

We will be using the ThingSpeak library but there are several other ways to write this code.

To code NodeMCU, you can use Arduino IDE or any other IDE you prefer. If you are using Arduino IDE you need to configure it for using it with NodeMCU. You can go here to configure your Arduino IDE to program NodeMCU.

Code:

#include <ESP8266WiFi.h>  

#include "ThingSpeak.h"  

 

const char* ssid = "XXXX";//Your Wi-Fi SSID  

const char* pass = "XXXX";//Your Wi-Fi password  

unsigned long channel_number = XXXX;  // Channel ID obtained from ThingSpeak channel  

const char * read_api_key = "XXXX"; // Read API Key obtained from ThingSpeak channel  

const int field_1 = 1; //field from which you want to read data  

int led ,statusCode;  

WiFiClient  client;  

void setup() {  

  pinMode(D4,OUTPUT);  

  Serial.begin(9600);  

  Serial.println("Connecting to Wifi");  

  WiFi.begin(ssid,pass);//Connect to WiFi  

  while(WiFi.status()!=WL_CONNECTED){//Wait for connection to the WiFi  

    Serial.print(".");  

    delay(500);  

  }  

  Serial.println("Wifi Connected");  

  ThingSpeak.begin(client);  

   

}  

void loop() {  

  long led = ThingSpeak.readIntField(channel_number, field_1, read_api_key);//Reading the Thingspeak Field and storing the value obtained in a variable  

  Serial.println(int(led));  

  statusCode = ThingSpeak.getLastReadStatus();//Returns 200 if there is a change in value in ThingSpeak field  

  if (statusCode == 200)//If there is a change of value in ThingSpeak field  

  {  

    if (led == 0)//If the value is zero then relay is set High  

    digitalWrite(D4,HIGH);  

    else//or else the relay is set low  

    digitalWrite(D4,LOW);  

  }  

  else 

  {  

    Serial.println("Unable to read channel / No internet connection");  

  }  

  delay(100);  

}  

 

Now that we are done with the code, upload it to NodeMCU, and let us start building our circuit.

Set up your circuit as shown in the figure.

  • Pin D4 of NodeMCU -> Input of relay
  • 5v pin of NodeMCU -> VCC of relay
  • GND of NodeMCU -> GND of relay

Note: Exercise extreme caution when working with live AC voltages since it is extremely dangerous and can cause serious injury.

The same experiment can be tried with an LED as well instead of using an AC bulb.

Now, we have completely set up our project. It's time to test it using Google Assistant if it's working or not.

This blog has been submitted by Cyborg, NIT Rourkela under the Robocraze Club Outreach Program.

Author: Shivanshu Sahoo

Leave a comment

Please note, comments must be approved before they are published