Google Assistant controlled LED using ESP32 and Adafruit IO

Google Assistant controlled LED using ESP32 and Adafruit IO

In previous IoT articles we have used ESP32 to control an LED using IFTTT and using Adafruit IO. IFTTT and Adafruit IO are two popular cloud platform to build IoT (Internet of Things) based projects easily and rapidly. We also used another popular android application “Blynk” for controlling the ESP32 GPIO using Smart phone. With some minor changes in hardware you can replace the LED with any AC home Appliances to control it remotely from anywhere using internet.

In this article we will use Google Assistant with Adafruit IO to control an LED with ESP32. Here we have used IFTTT to access Google Assistant and to control LED by voice commands. ESP32 has been programmed using Arduino IDE.

 

Components Required

  1. ESP32 module
  2. USB cable
  3. Breadboard
  4. LED
  5. Resistor and jumper wires
  6. Google assistant enabled device.
  7. Account on Adafruit IO
  8. Account on IFTTT
  9. Google account- same account for which you are using Google Assistant.

 

Circuit Diagram

 Circuit Diagram for Google Assistant Controlled LED using ESP32 and Adafruit IO

 

Step1: Setting up Adafruit IO Account for IOT controlled LED

Adafruit IO is an IOT platform built around the MQTT Protocol. MQTT is a lightweight messaging protocol that provides resource-constrained network clients with a simple way to distribute telemetry information. The protocol which uses a publish/subscribe communication pattern, is used for machine to machine (M2M) communication and plays an important role in the Internet of Things (IoT).

For this project you have to follow following steps to getting started with Adafruit IO:

  • Visit https://io.adafruit.com and create an account.
  • After creating your Account you will be taken to your home screen. Click on “Feeds” from the left hand menu.

 Setting up Adafruit IO Account for IOT controlled LED

 

  • Now click on Actions and then create a New feed. Then it will ask you to name your feed I am giving it LED_Control, you can give according to you and then create and your feed is created.

 Create New Feed on Adafruit IO for IOT Controlled LED using ESP32                        

 

  • Now go to “Dashboards” from the left hand menu. Click on Actions and then click on create a new dashboard, give it name as you want; I am giving “LEDSwitch” and then click on Create and your dashboard will now created.

 Create New Dashboard on Adafruit IO for IOT controlled LED using ESP32

 

  • Now open your new dashboard by simply clicking on it and you should be taken to a mostly blank page. Clicking on blue + button will let you add new UI components to the dashboard.

 Create New Block on Adafruit IO for IOT controlled LED using ESP32

                                   

  • For this project I just need a button, so select first option, it will ask you to select the feed, so select the one you just made and keep the defaults for the rest of the settings.

 Choose Block on Adafruit IO for IOT controlled LED using ESP32

 

  • After selecting your dashboard window will look like this:

 Adafruit IO Dashboard for IOT controlled LED using ESP32

 

  • During programming you will required your unique AIO key so for this click on key button at right hand corner of your window.

 Set AIO key for Adafruit IO for IOT controlled LED using ESP32

                                   

  • After clicking on key button your Active key for this project is generated, don’t share this key with anyone this must be confidential.

AIO key for Adafruit IO for IOT controlled LED using ESP32

 

Step2. Connecting to Google Assistant through IFTTT

In this step, we will connect our Google Assistant to the Adafruit IO MQTT Broker to allow us to control the lights with voice commands. To do this I am using IFTTT (If This Then That) platform.

To perform this you need to follow the following procedure:

  • Go to www.IFTTT.com website and create a new account if you are not having already. You can sign up using your Google account also.
  • After creating an account click on your username at right hand corner of the window and click on “New Applet” button.

 Connect to Goolge Assistant through IFTTT for IOT controlled LED using ESP32

                                 

  • After clicking on New applet you will find a window which ask you ‘If this then that’. The term IF THIS THEN THAT means if something happens on the “This” then we have do something on “that”.

 New Applet on IFTTT for IOT controlled LED using ESP32

 

  • Click on + blue button and search for “Google Assistant”, and then select “Say a simple phrase” from the menu of specific triggers. This will ask you some details, fill according to you and create trigger.

 Create Trigger through IFTTT for IOT controlled LED using ESP32

                           

  • Now you have to give Action so click on + button of “That”, and search for Adafruit and click on “Send data to Adafruit IO
  • Now it will ask you to select the Feed name so select the feed that you made earlier for this project and in Data to save we will send ON for this applet and click on Create action.

 Create Action through IFTTT for IOT controlled LED using ESP32

                  

  • Once you have created this applet, you have to create another applet for turning the LED “OFF”. You have to follow the same steps to create another applet.
  • After creating both the applets go to “My Applets” and you can see both the applets here.

 Set Trigger Command through IFTTT for IOT controlled LED using ESP32

 

Programming ESP32 for Google Assistant Controlled LED

Complete program is given at the end of this project. ESP32 is programmed using Arduino IDE. Connecting ESP32 with Adafruit IO is very easy using Arduino IDE, you have to include Adafruit MQTT Client Library in your IDE, for this open your Arduino IDE and go to Sketch--> include library-->Manage library and search for “adafruit mqtt” then a library associated with this will be shown to you; you just have to install it.

 Programming ESP32 for Google Assistant Controlled LED

 Install Adafruit Library for Google Assistant Controlled LED

 

After installing this library you are ready to use Adafruit IO with the ESP32.

The complete code for this project is given at last of this article, I will explain you about the code briefly and will tell you where you have to do the modifications.

At the start of the program, you have to add following libraries:

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

 

Now you have to define your network SSID, password, your Adafruit username and your AIO key that you got while doing Adafruit IO setup.

#define WLAN_SSID       "Ashish"
#define WLAN_PASS       "12345678"
#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  1883                  
#define AIO_USERNAME    "khandelwalashish129"
#define AIO_KEY         "350c30c9c8864eabb26458c547XXXXXX"

 

Now you have to define LED to which you are taking your output.

int output=2;

 

Now, create WiFiClient and Adafruit_MQTT_Client objects as global variables, and instantiate the feed for your LED_Control.

WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);       
Adafruit_MQTT_Subscribe LED_Control = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/LED_Control");

 

Now in the setup function we will define our LED pin, baud rate and we will connect to WiFi and MQTT Server.

void setup() {
  Serial.begin(115200);
  delay(10);
pinMode(2,OUTPUT);
  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());
  mqtt.subscribe(&LED_Control);
}

 

Now, in the Loop function, we need to check to see if our subscription has been updated, and act accordingly.

void loop() {
MQTT_connect();
Adafruit_MQTT_Subscribe *subscription;
  while ((subscription = mqtt.readSubscription(5000))) {
    if (subscription == &LED_Control) {
      Serial.print(F("Got: "));
      Serial.println((char *)LED_Control.lastread);
       if (!strcmp((char*) LED_Control.lastread, "ON"))
{
        digitalWrite(2, HIGH);
      }
      else
      {
        digitalWrite(2, LOW);
      }
    }
  }
}

 

Finally, add the MQTT Connect function so that MQTT connection is established.

void MQTT_connect() {
  int8_t ret;
  if (mqtt.connected()) {
    return;
  }
  Serial.print("Connecting to MQTT... ");
  uint8_t retries = 3;​​​​​​
  while ((ret = mqtt.connect()) != 0) {
       Serial.println(mqtt.connectErrorString(ret));
       Serial.println("Retrying MQTT connection in 5 seconds...");
       mqtt.disconnect();
       delay(5000); 
       retries--;
       if (retries == 0) {
         while (1);
       }
  }
  Serial.println("MQTT Connected!");
}

 

Once your code is ready with modifications, you are ready to upload it to your ESP32 from Arduino IDE.

 

Testing LED control with Google Assistant

After uploading of code open your serial monitor and your serial monitor should look like this:

 Testing LED Control with Google Assistant

 

Now open Google assistant in your Android and give voice command like “Turn LED on” or “Turn LED off” and it will respond you like you defined earlier and you will observe change of LED state also.

 Google Assistant Controlled LED using ESP32 and Adafruit IO

Code

#include <WiFi.h>

#include "Adafruit_MQTT.h"

#include "Adafruit_MQTT_Client.h"

#define WLAN_SSID       "Ashish"

#define WLAN_PASS       "12345678"

#define AIO_SERVER      "io.adafruit.com"

#define AIO_SERVERPORT  1883                  

#define AIO_USERNAME    "khandelwalashish129"

#define AIO_KEY         "350c30c9c8864eabb26458c547XXXXXX"

int output=2;

WiFiClient client;     // Create an ESP8266 WiFiClient class to connect to the MQTT server.

Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);        // Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.

Adafruit_MQTT_Subscribe LED_Control = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/LED_Control");

void MQTT_connect();

void setup() {

  Serial.begin(115200);

  delay(10);

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

  mqtt.subscribe(&LED_Control);

}

uint32_t x=0;

void loop() {

   MQTT_connect();

Adafruit_MQTT_Subscribe *subscription;

  while ((subscription = mqtt.readSubscription(5000))) {

    if (subscription == &LED_Control) {

      Serial.print(F("Got: "));

      Serial.println((char *)LED_Control.lastread);

       if (!strcmp((char*) LED_Control.lastread, "ON"))

      {

        digitalWrite(2, HIGH);

      }

      else

      {

        digitalWrite(2, LOW);

      }

    }

  }

 

 

}

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!");

}

30 Comments

In line 25 of the code, instead of "/feeds/LED_Control" I put "/feeds/led-control" and it solved the issue. Also note that the circuit connection given in the picture is kinda wrong. If you use the code given, then the inbuilt led of esp32 (if its devkit v1 board) will turn ON/OFF.

I got cconnected to wifi, mqtt is also connected but not getting data from adafruit as its not entering in while part of void loop() function. Please help me with this

Can I simply say what a relief to find somebody who really is aware of what theyre talking about on the internet. You definitely know learn how to convey an issue to mild and make it important. More individuals have to read this and understand this facet of the story. I cant imagine youre not more common since you positively have the gift.

I'm also writing to make you be aware of what a impressive experience my friend's child encountered reading your site. She figured out some issues, which include what it is like to have a wonderful helping mood to get many others easily thoroughly grasp a variety of extremely tough subject areas. You really did more than readers' expectations. Thank you for displaying the precious, healthy, revealing not to mention unique thoughts on that topic to Mary.

My husband and i got very ecstatic when Albert managed to round up his basic research while using the ideas he obtained in your weblog. It's not at all simplistic just to be giving out guides which many others might have been making money from. And we all realize we have you to give thanks to for this. The entire illustrations you made, the simple web site menu, the relationships you will make it possible to instill - it is all fabulous, and it's aiding our son and our family do think that subject is thrilling, and that's tremendously pressing. Thank you for the whole lot!

Youre so cool! I dont suppose Ive read anything like this before. So good to find somebody with some unique thoughts on this subject. realy thanks for beginning this up. this website is something that is wanted on the net, somebody with a bit of originality. useful job for bringing one thing new to the internet!

I simply wanted to type a brief remark to be able to thank you for these superb items you are giving out here. My time-consuming internet lookup has at the end been recognized with good quality tips to talk about with my guests. I 'd tell you that we visitors actually are quite endowed to exist in a really good place with many wonderful people with insightful strategies. I feel quite grateful to have seen your entire web site and look forward to plenty of more amazing moments reading here. Thanks again for all the details.

I wanted to make a remark in order to express gratitude to you for some of the magnificent strategies you are writing here. My extended internet investigation has now been rewarded with pleasant knowledge to exchange with my good friends. I would declare that we readers actually are quite endowed to dwell in a magnificent website with many awesome people with insightful principles. I feel truly grateful to have encountered your website page and look forward to many more brilliant times reading here. Thanks a lot again for a lot of things.

I want to show my passion for your kind-heartedness in support of persons who have the need for assistance with this niche. Your special dedication to passing the solution up and down had been certainly important and have regularly helped associates like me to get to their desired goals. The helpful advice can mean a whole lot to me and a whole lot more to my colleagues. Regards; from everyone of us.

Oh my goodness! an amazing article dude. Thank you Nevertheless I am experiencing challenge with ur rss . Don抰 know why Unable to subscribe to it. Is there anyone getting identical rss problem? Anyone who is aware of kindly respond. Thnkx

I together with my friends ended up going through the great hints on your web page while the sudden came up with a horrible suspicion I had not expressed respect to the blog owner for them. All the people became certainly warmed to study all of them and have in reality been using those things. Appreciation for indeed being very considerate as well as for finding this sort of amazing areas most people are really needing to know about. Our own sincere regret for not expressing gratitude to you earlier.

I wanted to draft you that little bit of word to finally say thanks a lot yet again for your personal breathtaking thoughts you have shown on this page. This is certainly extremely generous of you to offer without restraint just what a few people would have offered for sale for an e book in order to make some bucks for themselves, precisely considering that you might well have done it in case you wanted. Those principles likewise acted to become a easy way to fully grasp that other people have the same desire much like my very own to grasp way more with reference to this issue. I'm sure there are a lot more enjoyable sessions ahead for individuals who examine your site.

The following time I learn a weblog, I hope that it doesnt disappoint me as a lot as this one. I mean, I do know it was my choice to learn, but I really thought youd have one thing fascinating to say. All I hear is a bunch of whining about something that you would repair if you happen to werent too busy looking for attention.

Youre so cool! I dont suppose Ive learn something like this before. So nice to seek out somebody with some original thoughts on this subject. realy thank you for beginning this up. this website is one thing that is needed on the internet, somebody with slightly originality. helpful job for bringing one thing new to the web!

That is the right blog for anybody who desires to seek out out about this topic. You notice a lot its almost onerous to argue with you (not that I really would want匟aHa). You positively put a new spin on a topic thats been written about for years. Nice stuff, simply nice!

I have to express some thanks to this writer just for rescuing me from this particular problem. As a result of looking through the the web and finding thoughts that were not productive, I thought my entire life was done. Being alive devoid of the answers to the issues you have solved through your main write-up is a crucial case, as well as ones that would have negatively affected my career if I hadn't noticed your web blog. Your personal ability and kindness in dealing with every aspect was vital. I'm not sure what I would've done if I had not come across such a thing like this. It's possible to now look forward to my future. Thanks for your time so much for the expert and results-oriented help. I will not think twice to refer your blog post to anybody who requires tips about this situation.

I not to mention my buddies ended up reading the best pointers located on your web blog and immediately I got a terrible suspicion I had not expressed respect to the web blog owner for those tips. The guys were as a result passionate to see all of them and already have sincerely been having fun with them. Thanks for being very kind and then for getting this form of important information millions of individuals are really desirous to be informed on. Our honest apologies for not expressing appreciation to you earlier.

There are some attention-grabbing deadlines on this article however I don抰 know if I see all of them center to heart. There may be some validity however I will take maintain opinion until I look into it further. Good article , thanks and we want more! Added to FeedBurner as well

There are actually a number of particulars like that to take into consideration. That could be a nice level to convey up. I provide the ideas above as common inspiration however clearly there are questions like the one you bring up where crucial thing will likely be working in sincere good faith. I don?t know if finest practices have emerged round issues like that, but I'm sure that your job is clearly identified as a fair game. Each boys and girls really feel the affect of just a second抯 pleasure, for the rest of their lives.

Oh my goodness! an amazing article dude. Thank you However I am experiencing problem with ur rss . Don抰 know why Unable to subscribe to it. Is there anybody getting an identical rss downside? Anyone who is aware of kindly respond. Thnkx

I simply wanted to make a small word so as to appreciate you for some of the pleasant items you are posting at this website. My time intensive internet lookup has at the end of the day been honored with excellent concept to talk about with my friends and classmates. I would believe that most of us readers are extremely lucky to be in a great website with very many awesome people with helpful hints. I feel rather lucky to have discovered the web site and look forward to plenty of more thrilling times reading here. Thank you once more for a lot of things.

Add new comment

The content of this field is kept private and will not be shown publicly.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.