This is a continuation of my last tutorial: Alexa - NodeMCU: WeMo Emulation Made Simple, where we have introduced the great fauxmoESP library which simplifies a lot the coding needed to develop automation projects involving Alexa and Smart Home devices emulation using NodeMCU.
In this new tutorial, we will start from that concept (emulating WeMo devices), but instead of using relays to switch on/off electrical appliances, we will "trigger" more complex functions, where multiple outputs will be required.
Only for fun, we will Simulate some of Star Trek Enterprise weapons, as Photon Torpedos and Phasers!
We will have a NodeMCU controlling an RGB LED, that will be our "Photon Torpedo" and a Red LED as a "Phaser". To give a more realistic effect, we will also include a Buzzer that will generate some sound together with visual effect.
The below block diagram shows how the project will work:
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)
- 1 X RBG LED - Common Cathode 4 Pins 10mm ($0.10)
- 1 X Red LED
- 1 x 220 ohms resistor
- 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:
2. ESPAsyncTCP
So, Go to your Arduino IDE and install the 3 above libraries under your Arduino/Library directory.
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.Step 3: Installing and Testing the RGB LED
Once you have the libraries installed on your Arduino IDE, let's start installing the RDB LED as shown in the above diagram. I am using a Common Cathode LED, so, I will connect each one of its leads (red, green and blue) to one of the NodeMCU pins, that will be programmed as an output. The common cathode will be connected to ground.
We will create 3 "single smart devices", one for each single color:
- Red ==> NodeMCU D7
- Green ==> NodeMCU D5
- Blue ==> NodeMCU D6
and 2 groups of combined devices, for "mixed colors":
- Yellow ==> Red and Green
- White ==> Red, Green, and Blue
Download and open the file RGB_fauxmo_Control_EXT.ino from 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 connected:
#define RED_PIN D7
#define GREEN_PIN D5
#define BLUE_PIN D6
Now, we must define the "name" as our devices will be understood by Alexa:
// Device Names for Simulated Wemo switches
fauxmo.addDevice("Red");
fauxmo.addDevice("Green");
fauxmo.addDevice("Blue");
fauxmo.addDevice("Yellow");
fauxmo.addDevice("White");
And, we must at setup(), define the function that will call our devices, in this case, 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 an example for "Red"):
/* --------------------------------------------------------------------------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, "Red") == 0) )
{
if (state)
{
digitalWrite(RED_PIN, HIGH);
}
else
{
digitalWrite(RED_PIN, LOW);
}
}
Note that I used "state" inside the "if statement", because the RGB LED is a common cathode and use normal logic for activation (HIGH).
On the Serial monitor you can see the messages exchanged, confirming that the NodeMCU is connected to the network:
[WIFI] Connecting to ROVAI TIMECAP
............................. ==> CONNECTED!
[WIFI] STATION Mode, SSID: ROVAI TIMECAP, IP address: 10.0.1.33
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 do it directly using voice command, like: "Alexa (or "Computer"), discovery devices" as shown in the video below:
Once Alexa has discovery your device, you can give her voice commands.
The video below shows the complete process:
The above screenshot shows the Serial monitor response for all process
Step 4: Installing Weapons!Now we will install 2 additional single devices on our NodeMCU:
- Light (Red Led) ==> NodeMCU D4
- Alarm (Buzzer) ==> NodeMCU D8
and define 3 special simulation functions that will be also triggered by Alexa:
- Firing Torpedos
- Firing Phasers
- Firing All Weapons
In total, will be 10 "devices".
Download and open the file Star_Trek_Control_EXT.ino from 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 connected:
* Set Devices NodeMCU Pins */
#define RED_LED_PIN D4
#define RED_PIN D7
#define GREEN_PIN D5
#define BLUE_PIN D6
#define BUZZER_PIN D8
We will need to define 3 new boolean variables that will be triggered by Voice command:
bool fireTorpedos = LOW;
bool firePhasers = LOW;
bool fireAllWeapons = LOW;
Now, we must define the "name" as our devices and functions will be understood by Alexa:
// Device Names for Simulated Wemo switches
fauxmo.addDevice("Red");
fauxmo.addDevice("Green");
fauxmo.addDevice("Blue");
fauxmo.addDevice("Yellow");
fauxmo.addDevice("White");
fauxmo.addDevice("Light");
fauxmo.addDevice("Alarm");
// Device Names for functions
fauxmo.addDevice("Torpedos");
fauxmo.addDevice("Phasers");
fauxmo.addDevice("All Weapons");
And, we must at setup(), define the function that will call our devices, in this case, the "callback" function:
fauxmo.onMessage(callback);
The loop() now will include the 3 special functions that will be "triggered by Alexa:
void loop()
{
fauxmo.handle();
torpedosFiring ();
phasersFiring ();
allWeaponsFiring();
}
The callback function should be developed as did before with RGB led, nothing different, unless that when calling special functions, we will "trigger" the correspondent variable:
For example:
On a voice command: "Turn On Torpedo" ==> Only the variable fireTorpedo will be set to HIGH and on the next loop() cicle, this variable status will be used by "torpedosFiring()" function.
if ( (strcmp(device_name, "Torpedos") == 0) )
{
if (state)
{
fireTorpedos = HIGH;
}
else
{
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
}
}
Last, but not least, you must create the 3 special functions to command the weapons. Here you should use your imagination. I create 3 single functions, where:
Firing Phasers:
- I blinked the Red LED 15 times, firing on the buzzer accordantly
Firing Torpedos:
- I mixed the colors of the RGB LED with sound and different timings for each color
Firing All Weapons:
- Mixed torpedos and Phasers
Look the code for details. It is simple logic.
For fun, I changed the "Wake word" at Alexa APP to use "COMPUTER" instead of "ALEXA".
You must now discover the 10 "devices". I connected the RGB LED and the Red LED on an Enterprise model for a more realistic simulation:
The video below shows the discovery process:
The video below shows all 10 devices been tested:
Step 5: To Boldly Go Where No One Has Gone Before......It is your time, now!
Be creative! Include more actuators (or even sensors)! For example, use real sound for better effects.
Remember The sky is NOT the limit. Afterall, the man reached the Moon! And soon, Mars!
"Live Long and Prosper" Good luck! ;-)
Step 6: ConclusionAs always, I hope this project can help others find their way in the exciting world of electronics, robotics, and IoT!
Please visit my GitHub for updated files: Alexa Star Trek Simulation
For more projects, please visit my blog: MJRoBot.org
Saludos from the south of the world!
See you on my next tutorial!
Thank you,
Marcelo
Comments