Sie sind auf Seite 1von 7

ESP32 HOME AUTOMATION 11 Feb, 2019 / 0 Comments

Google Assistant Based Home Appliance Control using ESP32 and Adafruit IO

In previous IoT articles we have used ESP32 to control an LED using IFTTT and using Adafruit IO (
assistant-controlled-led-using-ESP32-and-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.

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

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

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 (
So, for this project, I have created a feed and dashboard with name Light_Control and Light_Switch respectively.

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.

For this project I just need a button, so select rst 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
After selecting your dashboard window will look like this:

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

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

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

Programming ESP32 for Google Assistant Controlled Home Appliances

Complete program is given at the end of this project. ESP32 is programmed using Arduino IDE (
started-with-esp32-to-program-with-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 brie y and will tell you where you have to do
the modi cations.
Code for this project is same as that we used in last project of controlling of LED (
controlled-led-using-ESP32-and-adafruit-io), so for more explanation, go through the link.
#define WLAN_SSID "Ashish"
#define WLAN_PASS "12345678"
#define AIO_SERVER ""
#define AIO_SERVERPORT 1883
#define AIO_USERNAME "khandelwalashish129"
#define AIO_KEY "350c30c9c8864eabb26458c547XXXXXX"

After this modi cations, 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:

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

#include <WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#de ne WLAN_SSID       "Ashish"
#de ne WLAN_PASS       "12345678"
#de ne AIO_SERVER      ""
#de ne AIO_SERVERPORT  1883                  
#de ne AIO_USERNAME    "khandelwalashish129"
#de ne 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() {
  // Connect to WiFi access point.
  Serial.println(); Serial.println();
  Serial.print("Connecting to ");
  while (WiFi.status() != WL_CONNECTED) {
  Serial.println("WiFi connected");
  Serial.println("IP address: "); Serial.println(WiFi.localIP()); 
uint32_t x=0;
void loop() { 
  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);
        digitalWrite(2, LOW);
void MQTT_connect() {
  int8_t ret;
  // Stop if already connected.
  if (mqtt.connected()) {
  Serial.print("Connecting to MQTT... ");
  uint8_t retries = 3;
  while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
       Serial.println("Retrying MQTT connection in 5 seconds...");
       delay(5000);  // wait 5 seconds
       if (retries == 0) {
         // basically die and wait for WDT to reset me
         while (1);
  Serial.println("MQTT Connected!");