Please see an update of this project (December 2020) on this link: Home Automation with Alexa
A few months ago, I have explored here, how to use Alexa, an intelligent personal assistant made popular by the Amazon Echo and Echo-Dot, in Home Automation projects:
When IoT Meets AI: Home Automation With Alexa and NodeMCU
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. In our case, we will emulate the WeMo, using the fauxmoESP library,
Here is the main difference between this tutorial and my previous one. The fauxmoESP library simplifies extremely the code needed to develop home automation projects.
WeMo is a series of products from Belkin International, Inc. that enables users to control home electronics from anywhere. The product suite includes a switch, motion sensor, Insight Switch, light switch, camera, and app. The WeMo Switch (our case here) can be plugged into any home outlet, which can then be controlled from an iOS or Android smartphone running the WeMo App, via home WiFi or mobile phone network.
The below block diagram shows what will be developed on our project:
The below video, shows how the project will look like at the end:
Step 1: Bill of Material (BoM)(All values are referenced in USD)
- NodeMCU ESP8266-12E ($8.79)
- Echo Dot (2nd Generation) - Black ($49.99)
- 2 X Mini BreadBoard ($1.00)
- 4-Channel Relay Module ($8.49)
- Male-Female Dupont Cables ($1.00)
- External 5V power Supply or battery
This tutorial is based on the great open source lib & example developed by Xosé Perez (Tinkerman). See his original post here: Emulate a WeMo device with ESP8266. Tinkerman based its work on Python example code developed by Maker Musings.
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.
The library can be found on Xose Perez's BitBucket: fauxmoESP
In order to run it in your code, you will also need 2 more libraries by Hristo Gochkov:
So, Go to your Arduino IDE and install the 3 above libraries under your Arduino/Library directory.
Some Technical stuff (optional):
If you are interested in understanding more deeply what's going on, read the article: HOW TO MAKE AMAZON ECHO CONTROL FAKE WEMO DEVICES, written by Rick Osgut where you can learn the basis of WeMo emulation.
The WeMo devices use UPnP to perform certain functions over the network. The device detection function starts out with the Echo or Dot (in our case here, the Dot) searching for WeMo devices using UPnP. The device then responds to the Dot with the device’s URL using HTTP over UDP. The Dot then requests the device’s description using that HTTP URL. The description is then returned as an HTTP response.
The actual “on/off” functionality of the WeMo devices is simpler since the Dot already knows about the device. The Dot simply connects to the WeMo over the HTTP interface and issues a “SetBinaryState” command. The WeMo then obliges and returns a confirmation via HTTP (see the Echo/Dot Communication diagram above).
Step 3: Creating Our WeMo Devices With NodeMCULet's connect a 4 Channel relay module to control 2 lamps and 2 outlets as shown in above diagram.
We will create 4 "single smart devices":
- Light1 ==> Relay 1 ==> NodeMCU D5
- Light2 ==> Relay 3 ==> NodeMCU D7
- Outlet1 ==> Relay 2 ==> NodeMCU D6
- Outlet2 ==> Relay 4 ==> NodeMCU D8
and 3 groups of combined devices:
- All Devices (Light1, Light2, Outlet1 and Outlet2)
- Living Room (Light1 and Outlet1)
- Bed Room (Light2 and Outlet2)
Download and open the file NODEMCU_ALEXA_WeMos_4X_Fauxmo_EXT.inofrom my GitHub and change the dummy wifi credentials, with your own:
/* Network credentials */
#define WIFI_SSID "YOUR SSID HERE"
#define WIFI_PASS "YOUR PASSWORD HERE"
Confirm that you have properly defined the pins where the relays are connect:
/* Set Relay Pins */
#define RELAY_1 D5
#define RELAY_2 D6
#define RELAY_3 D7
#define RELAY_4 D8
Now, we must define the "name" as our devices will be understood by Alexa:
// 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");
And, we must at setup(), define the "callback" function:
fauxmo.onMessage(callback);
The loop() should be:
void loop()
{
fauxmo.handle();
}
The callback function should be developed as below (this is only for Light1):
/* ---------------------------------------------------------------------------
Device Callback
----------------------------------------------------------------------------*/
void callback(uint8_t device_id, const char * device_name, bool state)
{
Serial.print("Device "); Serial.print(device_name);
Serial.print(" state: ");
if (state)
{
Serial.println("ON");
}
else
{
Serial.println("OFF");
}
//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);
}
}
...
}
Note that I used "!state" inside the "if statement", because the 4 channel relays use reversed logic for activation.
On the Serial monitor you can see the messages exchanged:
[WIFI] Connecting to ROVAI TIMECAP ............................. ==> CONNECTED! [WIFI] STATION Mode, SSID: ROVAI TIMECAP, IP address: 10.0.1.36
Now, let's ask to Alexa to find your device. There are 2 methods to do it:
- Using the Alexa App in your Smartphone
- Asking Alexa to do it directly using voice command, like: "Alexa (in our case "Computer"), Find connected devices" as shown in the video below:
Once Alexa has discovery your device, you can give her voice commands as shown below:
The below screenshot shows the Serial monitor response.
As always, I hope this project can help others find their way in the exciting world of electronics and IoT!
Please visit my GitHub for updated files:
Home-Automation-with-Alexa-and-NodeMCU/tree/master/Version_FauxmoESP
For more projects, please visit my blog: MJRoBot.org
Saludos from the south of the world!
See you at my next tutorial!
Thank you
Marcelo
Comments