Blink a light with Google Assistant

Photo by BENCE BOROS on Unsplash

Basic Requirements

  • NodeMCU
  • LED
  • Connecting wires
  • Account in IFTTT
  • A Smart Phone with Google Assistant-enabled

Circuit Connection

Source

Setting Up IFTTT

  • Click Start for free. Then create an account from any of the options. Then click Create. After that, you will see a screen as shown below
  • Click on Add then choose a service. In this case, select Google Assistant. Then click on the 1st card that is “Say a simple phrase” then a window will appear as shown.
  • Complete it according to your ideas.
  • 1st column is the voice command that will activate the process
  • After you finished it we need to add the Then part. Before doing that we need to code the program for our NodeMCU.
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#define WIFI_SSID “Name”
#define WIFI_PASS “password123”
#define MQTT_SERV "io.adafruit.com"
#define MQTT_PORT 1883
#define MQTT_NAME "sathyanarayanp"
#define MQTT_PASS "Your Auth Key from adafruit”
int led = D6;WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, MQTT_SERV, MQTT_PORT, MQTT_NAME, MQTT_PASS);
Adafruit_MQTT_Subscribe onoff = Adafruit_MQTT_Subscribe(&mqtt, MQTT_NAME "/f/amigos");
Adafruit_MQTT_Publish LightsStatus = Adafruit_MQTT_Publish(&mqtt, MQTT_NAME "/f/light status");
void setup()
{
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
//Connect to WiFi
Serial.print("\n\nConnecting Wifi>");
WiFi.begin(WIFI_SSID, WIFI_PASS);
digitalWrite(LED_BUILTIN, LOW);
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(">");
delay(50);
}
Serial.println("OK!");//Subscribe to the onoff topic
mqtt.subscribe(&onoff);
pinMode(led, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(led, LOW);
}void loop()
{
//Connect/Reconnect to MQTT
MQTT_connect();
//Read from our subscription queue until we run out, or
//wait up to 5 seconds for subscription to update
Adafruit_MQTT_Subscribe * subscription;
while ((subscription = mqtt.readSubscription(5000)))
{
//If we're in here, a subscription updated...
if (subscription == &onoff)
{
//Print the new value to the serial monitor
Serial.print("onoff: ");
Serial.println((char*) onoff.lastread);
//If the new value is "ON", turn the light on.
//Otherwise, turn it off.
if (!strcmp((char*) onoff.lastread, "ON"))
{
//active low logic
digitalWrite(led, HIGH);
LightsStatus.publish("ON");
}
else if (!strcmp((char*) onoff.lastread, "OFF"))
{
digitalWrite(led, LOW);
LightsStatus.publish("OFF");
}
else
{
LightsStatus.publish("ERROR");
}
}
else
{
//LightsStatus.publish("ERROR");
}
}
// if (!mqtt.ping())
// {
// mqtt.disconnect();
// }
}
void MQTT_connect()
{
// // Stop if already connected
if (mqtt.connected() && mqtt.ping())
{
// mqtt.disconnect();
return;
}
int8_t ret;mqtt.disconnect();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)
{
ESP.reset();
}
}
Serial.println("MQTT Connected!");
}
  • Now let's move to the Then part

Setting Up Adafruit

Adafruit_MQTT_Subscribe onoff = Adafruit_MQTT_Subscribe(&mqtt, MQTT_NAME "/f/amigos");

Upload the Code to the Board

If you want to Turn your LED off using a voice command

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Beginners Series Updated!

Build a Machine Learning Model API Using YOLOv5 With FAST API

Swarm communication has moved to Discord

2020/May  —  Reading List

Condition Monitoring for Industrial SMEs — Build or Buy?

Top 20 Tools for Android Development

Website & Web hosting

Android Studio: Constraint Layout

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sathya

Sathya

More from Medium

Which types of customers are out there for an IoT modem manufacturer?

IoT modems for different RF options from varying modem suppliers.

9 Ways IoT Is Redefining Web Design & Development Process

Ferran Torres — Barcelona’s Latest Reinforcement