Google Assistant Based Home Appliance Control Using ESP32 and Adafruit IO
Google Assistant Based Home Appliance Control Using ESP32 and Adafruit IO
Google Assistant Based Home Appliance Control Using ESP32 and Adafruit IO
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 (https://iotdesignpro.com/projects/google-
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 (https://iotdesignpro.com/projects/iot-controlled-led-using-esp32-and-blynk-app). 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
(https://iotdesignpro.com/projects/getting-started-with-esp32-to-program-with-arduino-ide).
Here, we are controlling a bulb using relay. To create Adafruit IO and IFTTT account follow our previous articles.
(https://iotdesignpro.com/projects/google-assistant-controlled-led-using-ESP32-and-adafruit-io) 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
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:
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.
After this modi cations, you are ready to upload the code to ESP32 from Arduino IDE (https://iotdesignpro.com/projects/getting-started-
with-esp32-to-program-with-arduino-ide).
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.
Code
#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 "io.adafruit.com"
#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() {
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!");
}