Google Assistant Based Home Appliance Control using ESP32 and Adafruit IO

Google Assistant controlled Home Appliances 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 Home Appliances 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.

Here, we are controlling a bulb using relay. To create Adafruit IO and IFTTT account follow our previous articles. So, we will directly connect google assistant through IFTTT and will make applets for the same.

 

Requirements

  • ESP32 module
  • USB Cable
  • Relay module
  • AC mains supply
  • Bulb
  • Connecting wires.

 

Circuit Diagram

Circuit Diagram for Google Assistant Controlled Home Appliances using ESP32 and Adafruit IO

 

To connect your AC appliance to relay use the below connection:

Relay Module

 

Pins to be connected are as under:

NO pin of relay       -           Vcc pin of bulb and AC mains

COM pin of relay    -           Gnd pin of bulb and AC mains

+12V pin of relay    -           3V pin of ESP32

I/P pin of relay       -           D2/GPIO2 pin of ESP32

GND pin of relay    -           GND pin of ESP32

 

Follow the below steps to setup Adafruit IO and ESP32 for controlling Home Appliances:

 

Step1: Setting up Adafruit IO Account

To setup Adafruit IO account, and also to create new feed and dashboard follow our last article of Controlling LED using Adafruit IO and ESP32.

Adafruit IO Setup for Raspberry Pi GPIO Control

 

So, for this project, I have created a feed and dashboard with name Light_Control and Light_Switch respectively.

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

 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 Home Appliances 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 procedure as per our previous article of Controlling LED using Adafruit IO:

Set Trigger Command through IFTTT for IOT Controlled Home Appliances using ESP32

 

Programming ESP32 for Google Assistant Controlled Home Appliances

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.

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

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

Code for this project is same as that we used in last project of controlling of LED, so for more explanation, go through the link.

#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"

 

After this modifications, you are ready to upload the code to ESP32 from Arduino IDE.

 

Testing Home Appliances control with Google Assistant

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

Testing Home Appliance Control with Google Assistant

 

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

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         "350c30c9c8864eabb26458c547axxxxx"

int output=2;

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

WiFiClient client;

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 Light_Control = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/Light_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(&Light_Control);

}

uint32_t x=0;

void loop() { 

  MQTT_connect();

  Adafruit_MQTT_Subscribe *subscription;

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

    if (subscription == &Light_Control) {

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

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

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

      {

        //Active low logic

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

}

1 Comments

hi,
Thanks for the code. This is a great project.

A few remarks however:
You've declared the output variable in the beginning, but you've hard coded the pin number further down in the code.

I've tried this setup and code with an LED with resistor from the output pin to ground. The command through Google assistant and Ifttt is recognized but instead of a solid light (output high) the LED is flickering irregularly.
Do you have any idea why it's doing this? Is this related to the code? When the command is made to switch the light off it indeed switches off completely.

using esp32wroom, arduino ide, ifttt, google assistant

Thanks,
Ivan