LED Control using Google Assistant & ESP8266 Nodemcu
Table of Contents
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.
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:
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.
Now after creating the account we will click on the new channel.
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.
Now scroll down and click on the save.
After saving the channel the channel stats will appear
Now click on the API keys
Now the ThingSpeak server is created and we are going to write the code.
ThingSpeak ESP8266, Complete code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#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
Then write thingspeak.h MathWorks library and click on the install.
1 |
#include <ThingSpeak.h> |
Then add the esp8266 library which we have already discussed in detail in the previous article.
1 2 3 4 5 |
#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.
1 |
unsigned long counterChannelNumber = 1418792; |
Now copy the read api key and paste in the code
1 |
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.
1 |
pinMode(13,OUTPUT); |
now to show it on the serial monitor we will write the command
1 2 3 |
Serial.begin(115200); Serial.println(); |
Then to connect the esp8266 with the wifi we will write the wife name and password
1 |
>WiFi.begin("electronic clinic", "123456"); |
Then we will print a massage that wifi is connecting
1 |
Serial.print("Connecting"); |
Now to show the IP address of the module we will write the command
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
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
1 2 3 4 5 6 7 8 9 10 11 |
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.
If you Google click on the continue with Google or Facebook
Now click on the create
Now scroll down and click on the Google assistant
Now we will choose a trigger and click on simple phrase
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
After answering these question s we will click on the create trigger
Now click on that
Now scroll down and click on the web hooks
Now click on the make web request
When we will click on the make a web request the following form will appear
Then to fill the URL we will copy the URL from the Thinkspeak section and paste in the url write channel field
1 |
https://api.thingspeak.com/update?api_key=L4S9IUCITZWQI6XW&field1=0 |
Now paste this link in the URL section and change the 0 to 1.
Now click on the create action
After that click on the continue
Now click on the finish
After completion we will get
This all process is for led on. Now again we write the similar steps for led off.
Now again click on the create
Now click on if this
After that click on the Google assistant
Now click on simple phrase
Now fill the form
After that click on the trigger
Now click on then that
Now click on the web hook
In the web hook we will paste the URL link from the think speak
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.
Now click on the button and resize it according to the requirement.
Now click on the button and select digital pin 0.
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.
Now in search write Blynk and install the blink library.
Now we will open an example by clicking on file Menu > click on examples > Blynk > Boards_WiFi > ESP8266_Standalone.
Google Assistant and Blynk, ESP8266 Complete code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#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.
1 |
Ping blynk-cloud.com |
Now we will get the ip address copy the ip address and paste it in the url.
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.