Voice Based Home Automation with NodeMCU & Alexa using fauxmoESP

Home Automation with NodeMCU & Alexa (Last Updated On: December 1, 2018)

Introduction:

This post is about Voice Based Home Automation with NodeMCU & Alexa using fauxmoESP. This IOT Project tutorial is based on the great open source lib & example developed by Xosé Perez (Tinkerman).

fauxmoESP is a library for ESP8266-based devices that emulates a Belkin WeMo device and thus allows you to control them using this protocol, in particular from Alexa-powered devices like the Amazon Echo or the Dot.




Alexa & Echo Dot

What is Alexa ?

Alexa is capable of voice interaction, music playback, making to-do lists, setting alarms, streaming podcasts, playing audiobooks, and providing weather, traffic, and other real time information. Alexa can also control several smart devices using itself as a home automation hub. We will use on this project, the “Echo-Dot”, that allows users to activate the device using a wake-word, such as “Alexa” or “Computer”, as in “Star Trek!.

In the home automation space, Alexa can interact with several different devices as Philips Hue, Belkin Wemo, SmartThings, etc.

What is Echo Dot ?

Echo Dot is a voice-controlled speaker that uses Alexa to play music, control smart home devices, make calls, answer questions, set timers and alarms and more.

Home Automation with NodeMCU & Alexa




Features
1. Play music from Amazon Music, Spotify, Pandora, iHeartRadio, TuneIn, and SiriusXM
2. Call or message family and friends hands-free, or drop in from the Alexa App to your Echo device
3. Controls lights, locks, thermostats, and more with compatible connected devices
4. Use the built-in speaker, or for bigger sound, connect to speakers through Bluetooth or audio cable


Components Required for this Project:

Following are the list of components for designing Voice Based Home Automation with NodeMCU & Alexa Echo Dot.

Home Automation with NodeMCU & Alexa

1. NodeMCU ESP8266-12E - (Buy Online from Amazon)
2. Echo Dot (2nd Generation) -  (Buy Online from Amazon)
3. 2 X Mini BreadBoard
4. 4-Channel Relay Module - (Buy Online from Amazon)
5. Male-Female Dupont Cables
6. External 5V power Supply or Battery

Block Diagram:

The below block diagram shows what will be developed in this project:

Home Automation with NodeMCU & Alexa


Circuit Diagram & Connections:

Assemble the hardware as shown in the figure below on your breadboard for Voice Based Home Automation with NodeMCU & Alexa using fauxmoESP.

Home Automation with NodeMCU & Alexa




Setup for uploading Code & Libraries:

Step 1: Setup Ardunio Preference for NodeMCU
Go to file of Arduino IDE and select Preferences . Then add the following link in the “Additional Boards Manager URLs”, as shown in the figure below:

http://arduino.esp8266.com/stable/package_esp8266com_index.json

Home Automation with NodeMCU & Alexa

Step 2: NodeMCU Board Installation
Go to Board Manager and Instal NodeMCU Board version 2.3.0 Its very important other wise Alexa dont discover devices.

Home Automation with NodeMCU & Alexa

Step 3: Board Selection
Select Your NodeMCU board with correct model as shown in the figure:

Home Automation with NodeMCU & Alexa

Step 4: Adding Libraries
You need to download 3 different libraries and add it to Arduino IDE Library. The library are:
1. fauxmoESP Library
2. ESPAsync TCP Library
3. ESPAsync Web Server Library

Download Library




Video Tutorial & Explanation:


Source Code/Program:

#include <Arduino.h>
#ifdef ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#include "fauxmoESP.h"

#define WIFI_SSID "..your wifi name.."
#define WIFI_PASS "..your wifi password.."

#define SERIAL_BAUDRATE 115200
#define LED 2

/* Set Relay Pins */
#define RELAY_1 D5
#define RELAY_2 D6
#define RELAY_3 D7
#define RELAY_4 D8

fauxmoESP fauxmo;
// -----------------------------------------------------------------------------
// Wifi
// -----------------------------------------------------------------------------

void wifiSetup() {

// Set WIFI module to STA mode
WiFi.mode(WIFI_STA);

// Connect
Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASS);

// Wait
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.println();

// Connected!
Serial.printf("[WIFI] STATION Mode, SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());

}

void setup() {

Serial.begin(SERIAL_BAUDRATE);
//setup and wifi connection
wifiSetup();

//Set relay pins to outputs
pinMode(RELAY_1, OUTPUT);
pinMode(RELAY_2, OUTPUT);
pinMode(RELAY_3, OUTPUT);
pinMode(RELAY_4, OUTPUT);

//Set each relay pin to HIGH ====== NOTE THAT THE RELAYS USE INVERSE LOGIC =====
digitalWrite(RELAY_1, HIGH);
delay(500);
digitalWrite(RELAY_2, HIGH);
delay(500);
digitalWrite(RELAY_3, HIGH);
delay(500);
digitalWrite(RELAY_4, HIGH);
delay(500);

// You have to call enable(true) once you have a WiFi connection
// You can enable or disable the library at any moment
// Disabling it will prevent the devices from being discovered and switched
fauxmo.enable(true);
fauxmo.enable(false);
fauxmo.enable(true);

// Device Names for Simulated Wemo switches
fauxmo.addDevice("Light One");
fauxmo.addDevice("Light Two");
fauxmo.addDevice("Outlet One");
fauxmo.addDevice("Outlet Two");
fauxmo.addDevice("Bed Room");
fauxmo.addDevice("Living Room");
fauxmo.addDevice("All Devices");

// fauxmoESP 2.0.0 has changed the callback signature to add the device_id,
// this way it's easier to match devices to action without having to compare strings.
fauxmo.onSetState([](unsigned char device_id, const char * device_name, bool state, unsigned char value) {
Serial.printf("[MAIN] Device #%d (%s) state: %s value: %d\n", device_id, device_name, state ? "ON" : "OFF", value);
digitalWrite(LED, !state);

//Switching action on detection of device name

if ( (strcmp(device_name, "Light One") == 0) )
{
if (!state)
{
digitalWrite(RELAY_1, HIGH);
}
else
{
digitalWrite(RELAY_1, LOW);
}
}

if ( (strcmp(device_name, "Light Two") == 0) )
{
if (!state)
{
digitalWrite(RELAY_3, HIGH);
}
else
{
digitalWrite(RELAY_3, LOW);
}
}

if ( (strcmp(device_name, "Outlet One") == 0) )
{
if (!state)
{
digitalWrite(RELAY_2, HIGH);
}
else
{
digitalWrite(RELAY_2, LOW);
}
}

if ( (strcmp(device_name, "Outlet Two") == 0) )
{
if (!state)
{
digitalWrite(RELAY_4, HIGH);
}
else
{
digitalWrite(RELAY_4, LOW);
}
}

if ( (strcmp(device_name, "Bed Room") == 0) )
{
if (!state)
{
digitalWrite(RELAY_3, HIGH);
digitalWrite(RELAY_4, HIGH);
}
else
{
digitalWrite(RELAY_3, LOW);
digitalWrite(RELAY_4, LOW);
}
}

if ( (strcmp(device_name, "Living Room") == 0) )
{
if (!state)
{
digitalWrite(RELAY_1, HIGH);
digitalWrite(RELAY_2, HIGH);
}
else
{
digitalWrite(RELAY_1, LOW);
digitalWrite(RELAY_2, LOW);
}
}

if ( (strcmp(device_name, "All Devices") == 0) )
{
if (!state)
{
digitalWrite(RELAY_1, HIGH);
digitalWrite(RELAY_2, HIGH);
digitalWrite(RELAY_3, HIGH);
digitalWrite(RELAY_4, HIGH);
}
else
{
digitalWrite(RELAY_1, LOW);
digitalWrite(RELAY_2, LOW);
digitalWrite(RELAY_3, LOW);
digitalWrite(RELAY_4, LOW);
}
}

});

}

void loop() {

// Since fauxmoESP 2.0 the library uses the "compatibility" mode by
// default, this means that it uses WiFiUdp class instead of AsyncUDP.
// The later requires the Arduino Core for ESP8266 staging version
// whilst the former works fine with current stable 2.3.0 version.
// But, since it's not "async" anymore we have to manually poll for UDP
// packets
fauxmo.handle();

static unsigned long last = millis();
if (millis() - last > 5000) {
last = millis();
Serial.printf("[MAIN] Free heap: %d bytes\n", ESP.getFreeHeap());
}

}




2 Comments

    1. edit the code. set your wifi password and wifi ssid. then open serial monitor, alexa will automatically get conected.

Leave a Reply

Your email address will not be published. Required fields are marked *