IoT is always an intriguing subject as it gives a platform to pitch and formulate futuristic ideas to life. We all know how smart LEDs are commonly used in houses. Many companies are making a lot of money via selling such types of devices. Let us make a miniature of such types of smart LED using Node MCU
Basic Requirements
- NodeMCU
- LED
- Connecting wires
- Account in IFTTT
- A Smart Phone with Google Assistant-enabled
Circuit Connection
Setting Up IFTTT
- Go to https://ifttt.com/
- 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
Go to adafruit.io. Sign Up if you don't have an account. Then get the Auth key from the Key tab and paste it to the program as shown above. Then go to the Feeds tab and click view all and then create a new feed. I gave the name amigos.
After that click the Dashboards tab and click view all and then create a new dashboard and give a name. In our program change the amigos part of the below code to your feed name.
Adafruit_MQTT_Subscribe onoff = Adafruit_MQTT_Subscribe(&mqtt, MQTT_NAME "/f/amigos");
Now inside the created dashboard click the settings icon and select Create New Block and then add a toggle button as shown.
Now you are all Set.
Upload the Code to the Board
Upload the code and test if the Bulb is working by toggling the button to ON state. If it turns ON then your code is right. Now open the Google Assistant App and test your command. It will work !!!!
If you want to Turn your LED off using a voice command
You just need to create a new Applet in IFTTT as shown above.