Control ESP8266 NodeMCU Outputs using Google Assistant and Adafruit IO

In this user guide, we will control the output of the ESP8266 NodeMCU using Google assistant and Adafruit IO. Previously, we controlled the ESP module’s outputs through applications such as Telegram, Google Firebase, and Blynk applications. You can view the tutorials by accessing the links below.

This article is divided into these sub-topics:

  • Introduction to Adafruit IO
  • Getting Adafruit IO ready and using IFTTT to configure a trigger
  • Connecting Google Assistant through IFTTT
  • Setting up Arduino IDE to control LED using ESP8266 and Google Assistant

Adafruit IO Introduction

This time we will show you another interesting application where we will be able to control any output connected with the ESP8266’s GPIO pins through voice commands. We will use Adafruit IO which is free cloud-based software built around MQTT protocol. It is a highly interactive tool whereby the user can manage their project over the internet. From controlling LEDs, relays, electric motors to obtaining sensor data, Adafruit IO makes your project very accessible. For the user, it becomes extremely interactive and easy to use this application to control any AC appliance from anywhere over the internet.

Control ESP8266 NodeMCU Outputs using Google Assistant and Adafruit IO

Some key features of Adafruit IO

  • Makes your project internet-connected.
  • Connects your project to various web services
  • Demonstrates your data online in real-time through a visually pleasing dashboard. The user can display charts, graphs and display data e.g., sensor readings.
  • Uses triggers that can control or react to the user’s data. Triggers can be configured according to the user’s preference. Some examples include sending sensor readings to personal email after a set amount of time or displaying messages as a response to a temperature/pressure sensor. The sensors could be connected with different web services through the integration of Adafruit IO with IFTTT or Zapier.
  • Free and beginner-friendly

For this article, we will keep it simple and control the onboard LED of our ESP8266 module through Google Assistant and Adafruit IO in Arduino IDE. We will use IFTTT to access the Google Assistant which will control the onboard LED through voice commands. After that controlling any other ESP8266 module’s output with your voice will be relatively easy to follow.

Getting Adafruit IO ready

Firstly, type https://io.adafruit.com/ in your browser search tab and press enter. This will open the main page. You will have to log in by creating an account. Click ‘Get Started for Free’ as highlighted in the red rectangular box.

Adafruit IO getting started 1
  • Now type all the relevant information and click ‘Create Account.’
Adafruit IO getting started 2
  • After you signed in with your account a new window will open. It will consist of the dashboard as shown below. Click ‘Feeds’
Adafruit IO getting started 3
  • Then go to ‘view all’
Adafruit IO getting started 4
  • Now, click ‘+ New Feed’
Adafruit IO getting started 5
  • Specify the name of your feed and description if you want to add that. We are naming the feed as “ESP8266_LED_CONTROL”. Click, ‘Create’ to proceed.
  • Your feed will be created. Next, go to Dashboards > view all > + New Dashboard
Adafruit IO getting started 7
  • Specify the name of your dashboard and description if you want to add on. Then click ‘Create’. You can use any name according to your preference.
ESP8266 adafruit IO getting started pic1
  • Click on the newly made dashboard. It will open up a window as shown below. Click the settings icon as highlighted in red below. Then go to, ‘+ Create New Block’.
ESP8266 adafruit IO getting started pic2
  • The following window will open. We want a toggle button in our project so we will click on the ‘toggle’ block.
Adafruit IO getting started 10
  • Now select the feed which you created and move to the next step.
ESP8266 adafruit IO getting started pic3

Specify the Button settings. We are using the default settings. Click ‘Create block’ to proceed.

Adafruit IO getting started 12

Your dashboard will look something like this:

ESP8266 adafruit IO getting started pic4

Accessing secret AIO key

Click ‘My Key’

ESP8266 adafruit IO getting started pic5

Now you will be able to view your Adafruit IO key. Keep it safe with yourselves. We will need it while programming our ESP8266 module in Arduino IDE. For safety concerns, do not share the key with anyone.

Adafruit IO getting started 15

Connecting to Google Assistant through IFTTT

Recommended readings:

ESP32 HTTP POST using Arduino IDE (ThingSpeak and IFTTT)

MicroPython: Send Sensor Readings via Email (IFTTT) with ESP32 and ESP8266

In this project, our goal is to control the onboard LED of ESP8266 through voice commands. Thus, after setting up our Adafruit IO dashboard, the next step is to integrate Google Assistant into Adafruit IO. This will allow us to toggle the LED with our voice.

As we mentioned previously, Adafruit IO allows the user to set triggers for a particular action and its response. We will use the IFTTT service to configure the trigger for our project.

IFTTT means ‘If this, then that.’ It is an open-source service that gives the user the freedom to program a response to an event according to their likes. We can create an applet which are chains of conditional statements by a combination of several app services and add triggering parameters. For our project, we will be using this service, whenever the user will say a particular phrase then that data will be sent to Adadruit IO. To work with this web service, we will have to follow a series of steps to ensure the proper functionality.

Creating an IFFT Account

Although the IFTTT service is free to use, we will have to create an account. First go to the following website: https://ifttt.com/

The following window will appear. Click on the ‘Get Started’ button.

Getting started with IFTTT 1

The following window will appear. You can select any one from these three options (Apple, Google or Facebook) to connect. Or you can simply ‘sign up’with your own given email. We will be following this scheme.

IFTTT sign up page 2

Click the ‘sign up’ tag. You will see the following window pop up. Enter your email address and password to start working in IFTTT. This whole process is free of cost for the first three applets.

IFTTT sign up page 3

Creating an Applet

After you have created your account, we will be directed to the page where we will create our applet. Click on ‘Create.’

IFTTT create applets 4

The following window opens up. Click the following Add button in the ‘If This’ section.

IFTTT create applets webhock alert 5

Another page will open in which we will have to choose our service. There is a lot of options to choose from. Write down ‘Google Assistant’ in the search option and its icon will appear:

IFTTT Control LED ESP32 1

Set Trigger:

Click the icon and choose your trigger. We will use ‘Say a simple phrase’ as our trigger.

IFTTT Control LED ESP32 choose trigger

Next, click the ‘Connect’ button to establish the connection.

IFTTT Control LED ESP32 connect service

A new window will open which will let you sign in with your Google account. It will take permission for the voice commands as well. Now, we will specify the commands. You can add any text according to your preference.

Control LED ESP32 choose text

After the trigger is created, we are taken back to the web page where we first added the service for the ‘IF THIS’ section. Now we will click the ADD button for the ‘THEN THAT’ section.

IFTTT Control LED ESP32 2

Now we will choose the service. We have to choose what will happen if a voice command is received. Thus, we will type ‘Adafruit’ in the search option and click on its icon.

IFTTT Control LED ESP32 Adafruit

Next, we will select the action. Click ‘Send data to Adafruit IO’.

IFTTT Control LED ESP32 Adafruit choose action

Click the ‘Connect’ button.

IFTTT Control LED ESP32 Adafruit connect

A new web page will open which will ask for your permission. Click ‘Authorize’.

IFTTT Control LED ESP32 Adafruit authorize

Now specify the feed which you created and the data which will get saved. Click ‘Create action’.

IFTTT Control LED ESP8266 Adafruit create action

Click ‘Continue’ to proceed.

IFTTT Control LED ESP32 3

The applet title is shown. You can also turn on the notifications when it runs. Click ‘Finish’ to complete the process for the ‘Turn LED ON’ voice command.

IFTTT Control LED ESP8266 Adafruit finish

Similarly, we will create another applet for ‘Turn LED OFF’ voice command as well. In the end, we will have two applets running.

IFTTT Control LED ESP8266 applets

Setting up Arduino IDE

We will use Arduino IDE to program our ESP8266 development board. Thus, you should have the latest version of Arduino IDE. Additionally, you also need to install the ESP8266 plugin. If your IDE does not have the plugin installed you can visit the link below:

Installing ESP8266 library in Arduino IDE

Installing Adafruit MQTT Library

To use Adafruit IO with our ESP8266 board we will require a library. We will install the Adafruit MQTT library from the Library Manager in our Arduino IDE. Open your Arduino IDE. Go to Sketch > Include Library > Manage Libraries. Type ‘Adafruit MQTT’ in the search bar and press enter. Install the latest version of the library.

Adafruit MQTT library

Arduino Sketch

Open your Arduino IDE and go to File > New to open a new file. Copy the code given below in that file. You need to enter your network credentials. Additionally, you also have to provide your Adafruit IO key and Adafruit username.

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

#define SSID       "Your_SSID"
#define Password      "Your_Password"
#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  1883                  
#define AIO_USERNAME    "Your_AdafruitIO_Username"
#define AIO_KEY         "Your_AdafruitIO_Key"

int led_gpio=2;
WiFiClient client;     

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

void MQTT_connect();

void setup() {
  Serial.begin(115200);
  delay(10);
  pinMode(led_gpio,OUTPUT);
  digitalWrite(led_gpio,HIGH);
  Serial.println(); Serial.println();
  Serial.print("Connecting to WIFI ");
  WiFi.begin(SSID,Password);
  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(&ESP8266_LED_CONTROL);

}
uint32_t x=0;

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

      }
    }
  }
}

void MQTT_connect() {
  int8_t again;
  if (mqtt.connected()) {
    return;
  }
Serial.print("Connecting to Adafruit IO");
uint8_t retry = 5;
  while ((again = mqtt.connect()) != 0) { 
       Serial.println(mqtt.connectErrorString(again));
       Serial.println("Retrying Adafruit connection in 5 seconds...");
       mqtt.disconnect();
       delay(5000);  
       retry--;
       if (retry == 0) {
         while (1);
       }

  }
  Serial.println("");
  Serial.println("Adafruit IO is Connected!");
}

How the Code Works?

Including Libraries

Firstly, we will include the necessary libraries required for this project. ESP8266WiFi.h will help in establishing the connection between the ESP8266 module to a wireless network. We will also include the Adafruit MQTT library to successfully connect our board with it.

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

Defining Network credentials

Next, we will define the network credentials. One for the SSID and the other for the password. These will be our network credentials which will be used to connect to our wireless network. Replace both of them with your credentials to ensure a successful connection.

#define SSID       "Write_your_SSID"
#define Password   "Write_your_Password"

Defining Adafruit IO Credentials

Thirdly, we will specify the server name and port for Adafruit IO. You do not need to change these values. We will also define the Adafruit username through which we signed into our account and the IO key which we previously saved. Keep this key safe with you for security concerns. You will have to replace these two with your own credentials.

#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  1883                  
#define AIO_USERNAME    "Your_AdafruitIO_username"
#define AIO_KEY         "Your_AdafruitIO_Key"

Output Variable

Then, we will define the variable of data type int called ‘led_gpio’. It will save the GPIO pin through which our onboard LED is connected i.e., GPIO2. You can use any other GPIO pin according to your preference. Any other output e.g., relays or buzzers could also be controlled. Just change the PIN to control it instead.

int led_gpio = 2;

Connecting with Adafruit IO

Next, we will create a WiFiClient and the Adafruit MQTT client. The Adafruit MQTT client will be set up with the WiFiClient, the Adafruit server name, and its port value. These will be configured as global variables so we can access them throughout our program code.

WiFiClient client;     
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);        

Then create an instance of the feed which we created for our project named ‘ESP8266_LED_CONTROL’. We will pass the MQTT object, the path for our feed with the Adafruit username.

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

Call the function to connect with your feed.

void MQTT_connect();

Setup() function

Inside the setup() function, we will open a serial connection at a baud rate of 115200.

Serial.begin(115200);

By using the pinMode() function, the ‘led_gpio’ variable (GPIO2) will be passed as a parameter inside the function. This will configure the pin as an output.

pinMode(led_gpio, OUTPUT); 

The following section of code will connect our ESP8266 board with the local network whose network credentials we already specified above. We will use the WiFi.begin() function. The arguments will be the SSID and the password which we defined earlier in the code.

Serial.print("Connecting to WIFI ");
WiFi.begin(SSID,Password);
while (WiFi.status() != WL_CONNECTED) 
{ delay(500);
 Serial.print(".");
 }

After a successful connection is made, the serial monitor will display that message and also print the IP address of your ESP8266 module.

  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

Finally, we will connect to the MQTT server:

mqtt.subscribe(&ESP32_LED_Control);

loop() function

In the infinite loop, we will call the MQTT_connect() function and then use if-else statements to check if our subscription is being changed or not. The state of the LED will be changed accordingly. This will be achieved through digitalWrite() function. We will pass the led_gpio as the first parameter and either HIGH or LOW as the second parameter inside it.

Note: For ESP8266, the onboard LED works on an opposite logic as compared to ESP32. To turn the onboard LED ON, a low signal is sent, and to turn it OFF, a high signal is sent. This is the opposite in the case of ESP32.

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

      }
    }
  }

In the void MQTT_connect() function we will try to connect our ESP8266 with the MQTT server. This can be seen in the following definition of the function.

void MQTT_connect() {
  int8_t again;
  if (mqtt.connected()) {
    return;
  }
Serial.print("Connecting to Adafruit IO");
uint8_t retry = 5;
  while ((again = mqtt.connect()) != 0) { 
       Serial.println(mqtt.connectErrorString(again));
       Serial.println("Retrying Adafruit connection in 5 seconds...");
       mqtt.disconnect();
       delay(5000);  
       retry--;
       if (retry == 0) {
         while (1);
       }

  }
  Serial.println("");
  Serial.println("Adafruit IO is Connected!");
}

Demonstration

Choose the correct board and COM port before uploading your code to the board.
Go to Tools > Board and select NodeMCU 1.0

select ESP8266 NodeMCU board

Next, go to Tools > Port and select the appropriate port through which your board is connected.

ESP8266 COM Port

Click on the upload button to upload the code to your ESP8266 development board.
After you have uploaded your code to the development board, press its RST button.

ESP8266 NodeMCU reset button

In your Arduino IDE, open up the serial monitor and you will be able to see the status of your WIFI and Adafruit connection.

Control ESP8266 Outputs using Google Assistant and Adafruit IO demo serial monitor
Serial Monitor

We will use an Android smartphone for this project. So go to Google Play. Search for ‘Google Assistant’ and install the application.

Adafruit IO getting demo google assistant install

Now open your Google Assistant app on your smartphone.

Make sure your Google Assistant is connected with the same email as your Adafruit IO and IFTTT account.

Touch the microphone icon and say ‘Turn LED ON’ or ‘LED ON’ and the onboard LED will light up.
Likewise, say ‘Turn LED OFF’ or ‘LED OFF’ and the onboard LED will turn OFF.
The Google Assistant will also display a message after each updating process.

Adafruit IO getting demo google assistant
Google Assistant

The serial monitor will also print relevant messages as the state of the LED gets updated.

Control ESP8266 Outputs using Google Assistant and Adafruit IO demo2 serial monitor
Serial Monitor

Conclusion

In conclusion, we were able to control our ESP8266 module’s output by using Google Assistant. This was achieved by integrating Adafruit IO and IFTTT as well. We controlled the onboard LED connected to GPIO2 which was configured as an output. Similarly, you can use any other output GPIOs easily and control it with your voice.

Leave a Comment