ESP8266IOT Projects

LED Control using Google Assistant & ESP8266 Nodemcu

LED control using Google assistant:

LED Control using Google Assistant & ESP8266 Nodemcu- In this project we will use Google assistant to turn ON and turn OFF the led, because it’s easy to get started with the LED as this is the beginner’s level thing. The led will be connected with the ESP8266 Nodemcu. This project is a little complicated for those guys who have never used the Nodemcu ESP8266 WiFi Module or the IFTTT. But if you carefully read this article and follow all the steps then I am sure you will successfully complete this project. This project is divided in to the following sections:

  • Circuit design
  • Thing speak server design
  • NodeMCU ESP8266 programming, and
  • IFTTT server design for Google assistant



Circuit design for the Google Assistant:

The circuit diagram for the Google Assistant project is very simple we will connect an Led with the digital pin 13 of the NodeMCU ESP8266 WiFi Module. Don’t worry if you have another version of the Nodemcu ESP8266, you can use any other pin so far you have the Nodemcu ESP8266 module. If you have never used the Nodemcu ESP8266 then I highly recommend to read my article “Getting started with Nodemcu ESP8266”. In this tutorial, I have covered the Pinout, technical specs, Nodemcu ESP8266  Board installation, and how to write a basic program. Anyways if you set up your Nodemcu ESP8266 module, then you can start with the interfacing.

Google Assistant

Thingspeak server design:

Thingspeak is an open data platform for the internet of things “IoT”. Thingspeak enables sensors, instruments, and websites to send data to the cloud where it is stored in either a private or public channel. Now we will design Thingspeak server which will helps us in making IOT projects. First of all open the Google chrome or other browser and paste the following link:

https://thingspeak.com/

Google Assistant

Now, first of all, create an account by clicking on the create and fill up the form and if you have already an account then enter the email and click on the sign in.

Google Assistant

Now after creating the account we will click on the new channel.

Google Assistant

When we will click on the new channel a form will appear in which we will write the name and description of the project. As we are controlling an led with it so in the name we will write the led_on_off  and in the description we will write, that we are going to control an led through Google assistant and then click on the save channel.

Google Assistant

Now scroll down and click on the save.

Google Assistant




After saving the channel the channel stats will appear

Google Assistant

Now click on the API keys

Google Assistant

Now the ThingSpeak server is created and we are going to write the code.

ThingSpeak ESP8266, Complete code:

#include <ThingSpeak.h>               // add librery
#include <ESP8266WiFi.h>

WiFiClient  client;
unsigned long counterChannelNumber = 1418792;                // Channel ID
const char * myCounterReadAPIKey = "IDSIB0D4K69Z41K5";      // Read API Key
const int FieldNumber1 = 1;                                 // The field you wish to read
const int FieldNumber2 = 2;                                 // The field you wish to read

void setup()
{
  pinMode(13,OUTPUT);
  Serial.begin(115200);
  Serial.println();

  WiFi.begin("AndroidAP3DEC", "engrhammad");                 // write wifi name & password           

  Serial.print("Connecting");
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  Serial.print("Connected, IP address: ");
  Serial.println(WiFi.localIP());
  ThingSpeak.begin(client);
}

void loop() 
{
 int A = ThingSpeak.readLongField(counterChannelNumber, FieldNumber1, myCounterReadAPIKey);
 Serial.println(A);
 digitalWrite(13,A);
}



ThingSpeak ESP8266, Code Explanation:

In the code, we will first, include the thingspeak library for which click on the sketch and in sketch click on the include libraries and then click on the manage libraries

Google Assistant

Then write thingspeak.h MathWorks library and click on the install.

Google Assistant

#include <ThingSpeak.h>

Then add the esp8266 library which we have already discussed in detail in the previous article.

#include <ThingSpeak.h>               // add librery

#include <ESP8266WiFi.h>

WiFiClient  client;

Then we will add the channel id by copying it and paste in the code.

unsigned long counterChannelNumber = 1418792;

Google Assistant

Now copy the read api key and paste in the code

Google Assistant

const char * myCounterReadAPIKey = "IDSIB0D4K69Z41K5";

Now in the setup function we will declare the pins which we will use in the project in our case we are using pin number 13 connected with the led.

pinMode(13,OUTPUT);

now to show it on the serial monitor we will write the command

Serial.begin(115200);

Serial.println();

Then to connect the esp8266 with the wifi we will write the wife name and password

>WiFi.begin("electronic clinic", "123456");

Then we will print a massage that wifi is connecting

Serial.print("Connecting");

Now to show the IP address of the module we will write the command

while (WiFi.status() != WL_CONNECTED)

{

delay(500);

Serial.print(".");

}

Serial.println();

Serial.print("Connected, IP address: ");

Serial.println(WiFi.localIP());

ThingSpeak.begin(client);

}

Now to send the command to the led to turn on or off we will write the function

void loop()

{

int A = ThingSpeak.readLongField(counterChannelNumber, FieldNumber1, myCounterReadAPIKey);

Serial.println(A);

digitalWrite(13,A);

}



IFTTT server design for Google assistant:

Now we will design IFTTT server for the Google assistant. Now, first of all, we will create an account in IFTTT by clicking to sign up.

https://ifttt.com/

If you Google click on the continue with Google or Facebook

Google Assistant

Now click on the create

Google Assistant

Google Assistant

Now scroll down and click on the Google assistant

Google Assistant

Now we will choose a trigger and click on simple phrase

Google Assistant



After clicking on the say a simple phrase we will complete the trigger fields

To fill the form:

What do you want to say?

Light on

What another way to say?

Turn on the light

What do you want the assistant to say in response?

Light is on

Google Assistant

After answering these question s we will click on the create trigger

Google Assistant

Now click on that

Google Assistant

Now scroll down and click on the web hooks

Google Assistant

Now click on the make web request

Google Assistant

When we will click on the make a web request the following form will appear

Google Assistant




Then to fill the URL we will copy the URL from the Thinkspeak section and paste in the url write channel field

https://api.thingspeak.com/update?api_key=L4S9IUCITZWQI6XW&field1=0

Google Assistant

Now paste this link in the URL section and change the 0 to 1.

Google Assistant

Now click on the create action

Google Assistant

After that click on the continue

Google Assistant

Now click on the finish

Google Assistant

After completion we will get

Google Assistant

This all process is for led on. Now again we write the similar steps for led off.

Now again click on the create

Google Assistant



Now click on if this

Google Assistant

After that click on the Google assistant

Google Assistant

Now click on simple phrase

Google Assistant

Now fill the form

Google Assistant

After that click on the trigger

Google Assistant

Now click on then that

Google Assistant

Now click on the web hook

Google Assistant

In the web hook we will paste the URL link from the think speak

Google Assistant

Now click on the create action and the led turn off section will be completed.



LED control using Google Assistant and Blynk:

We can also control from Google assistant using Blynk application. Now, first of all, we will create button for on and off in Blynk application. Open the Blynk application in the mobile and create a new project. Give the title name led control and in choose device select the Nodemcu and click on the create button.

Google Assistant

Now click on the button and resize it according to the requirement.

Google Assistant

Now click on the button and select digital pin 0.

Google Assistant

Now the Blynk app is created and token is already sent to the email that we will now use in the coding.

Google Assistant and Blynk, ESP8266 Code:

In the coding, we will first introduce Blynk library. Click on the sketch and in sketch click on the include library and then click on the manage libraries.

Google Assistant

Now in search write Blynk and install the blink library.

Google Assistant



Now we will open an example by clicking on file Menu > click on examples > Blynk > Boards_WiFi > ESP8266_Standalone.

Google Assistant

Google Assistant and Blynk, ESP8266 Complete code:

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
//This token is obtain from the email id copy it and paste in the char aurth
char auth[] = "37gBHW_cr1U72Y7UYeFbIkgn4JQa7DVk";
// Your WiFi credentials.
// Set password to "" for open networks.
//now give the name of the wifi module 
char ssid[] = "AndroidAP3DEC";
//give the password of the nodemcu module
char pass[] = "electroncilinic";
void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
}

void loop()
{
  Blynk.run();
}

Upload the code and we can control the led from the Blynk app. This is the easiest part, which I have already explained in my getting started tutorial. Controlling an LED using the Blynk application and the Nodemcu ESP8266 is a kind of very beginner’s level project. I think you should have no problem in controlling the LED using the Blynk application.  So, when you are done with controlling the LED using the Blynk application, then get ready for the next step, now we will interface it with the Google Assistant.




Interfacing with Google assistant:

Open the https://ifttt.com/home and click on the create button, the creation process is similar as we have discuss above the only difference is that in URL we will give the IP address  of the Blynk app. Now in order to find the IP of the Blynk we will open the command window and write the command.

Ping blynk-cloud.com

Google Assistant

Now we will get the ip address copy the ip address and paste it in the url.

Google Assistant

After the ip address give slash and paste the token of the Blynk app then again give slash and write update and then pin number which we are using D0. In method select the put. In this way we will create trigger. The remaining steps is similar as we have discussed above the only difference is in the URL.

Now we will open the Google assistant and when we will say led on the led will turn on and when we say the led off the led will turn off.

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...

Leave a Reply

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

Back to top button