Fauxmo is an open source library that emulates Belkin WeMo devices. WeMo
devices are smart products that can be controlled by voice (Alexa) or
from a mobile (Via the Alexa App).
You can easily make an Arduino Alexa ESP8266 or ESP32 project with this
library. Typical devices include switches and lights basically
anything that can be controlled using a
relay.
The latest update to the fauxmo library allows a brightness value to be
included in the control of a device. So you could use a dimmable LED as a
light source and control it through Alexa with a voice command such as
"Alexa, set the brightness of the living room lights to 50%".
How does fauxmo work?
The fauxmoESP library code creates a virtual WeMo device on the ESP chip.
This emulates a "real" WeMo device so Alexa controls it as a WeMo device
which you can control using your voice.
Here are some examples of what could do with fauxmo:
Control lights, appliances, and other devices in your home with voice commands.
Create custom smart home devices that are not available from commercial vendors.
Automate tasks in your home, such as
turning on the lights when you come home or turning off the sprinklers
when it starts to rain.
What does the word fauxmo mean?
Fauxmo is probably a combination of the words faux and WeMo, which stand for fake and the WeMo
device. It is a lightweight open source library that allows you to make
your own Arduino ESP8266 devices appear as smart devices to Amazon
Alexa.
Fauxmo Example Sketch
The following example shows you how to control two outputs:
An LED on a pin (which, for a real device, would be attached to a relay control input).
Another LED on a PWM pin.
Hardware connections for fauxmo testing
It's quite simple - two LEDs, connecting via two resistors to D5
and D8. The on board LED is used to show activity. Note that the 1k
resistor goes to the positive side of the LED while ground goes to the
negative (flat) side of the LED.
image by Fritzing
Libraries
Fauxmo depends on two libraries that you need to install, if not
installed already. Which one you install depends on which chip you are
using:
ESPAsyncTCP (for the ESP8266)
AsyncTCP (for the ESP32)
You can install both so you'll be ready for either chip.
Arduino ESP8266/ESP32 fauxmo example
// Two device Alexa voice control
// 1 relay (LED)
// 1 Dimmable LED
#ifdef ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#include <fauxmoESP.h>
const char* ssid = "****";
const char* password = "****";
#define DEV1 "Main light"
#define DEV2 "Reading Light"
#define device1_pin 14 // GPIO14 is D5.
#define device2_pin 15 // GPIO15 is D8.
#define LED_PIN 2 // GPIO2 is D4 - Blue LED on board.
#define MAX_PWM 255
fauxmoESPfauxmo;
voidsetup(){
Serial.begin(115200);setup_wifi();
Serial.println("\n\nfauxmo = Control by Alexa. With two outputs.");
fauxmo.setPort(80);
fauxmo.enable(true);
fauxmo.addDevice(DEV1);fauxmo.addDevice(DEV2);pinMode(LED_PIN,OUTPUT);
pinMode(device1_pin,OUTPUT);
pinMode(device2_pin,OUTPUT);
digitalWrite(LED_PIN,LOW);
digitalWrite(device1_pin,LOW);
digitalWrite(device2_pin,LOW);
// For the reading light allow pwm control of the LED
// Initialize the PWM pin
analogWriteRange(MAX_PWM);// Set the PWM range (0-255)
analogWriteFreq(1000);// Set the PWM frequency (in Hz)
fauxmo.onSetState([](unsignedchardevice_id,constchar*device_name,boolstate,unsignedcharvalue){
// Callback when a command from Alexa is received.
// You can use device_id or device_name to choose the element to perform an action onto (relay, LED,...)
// State is a boolean (ON/OFF) and value a number from 0 to 255 (if you say "set kitchen light to 50%" you will receive a 128 here).
// Just remember not to delay too much here, this is a callback, exit as soon as possible.
// If you have to do something more involved here set a flag and process it in your main loop.
Serial.printf("[MAIN] Device #%d (%s) state: %s value: %d\n",device_id,device_name,state?"ON":"OFF",value);
// Checking for device_id is simpler if you are certain about the order they are loaded and it does not change.
// Otherwise comparing the device_name is safer.
if(strcmp(device_name,DEV1)==0){
digitalWrite(device1_pin,state?HIGH:LOW);
Serial.println("DEV1 switched");
}elseif(strcmp(device_name,DEV2)==0){// Dimmable LED
Serial.println("DEV2 switched");
if(state==LOW)digitalWrite(device2_pin,LOW);
else{// Control PWM with gamma correction.
intv=(int)((float)MAX_PWM*pow((float)value/MAX_PWM,2.2));
analogWrite(device2_pin,v);charbuffer[25];
sprintf(buffer,"The value is: %d",v);
Serial.println(buffer);}
}
});
}
staticlongtimewas=millis();
staticintstate=0;
voidloop(){
fauxmo.handle();
if(millis()-timewas>500){
timewas=millis();
if(state)digitalWrite(LED_PIN,HIGH);elsedigitalWrite(LED_PIN,LOW);state=!state;
}
}
voidsetup_wifi(void){
WiFi.mode(WIFI_STA);
WiFi.begin(ssid,password);
Serial.println("");
// Wait for connection
while(WiFi.status()!=WL_CONNECTED){
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
Note: Set your credentials (password and ssid) to match your router's name and router's password.
Code Explanation for Fauxmo example
Device Definitions: Two devices, "Main
light" and "Reading Light," are defined with corresponding GPIO pins.
The "Main light" is controlled by a relay connected to GPIO14
(device1_pin), and the "Reading Light" is controlled by a dimmable LED
connected to GPIO15 (device2_pin).
LED Pin Definition: The code also defines
LED_PIN as GPIO2 (D4), which is used for a blue LED on the board. The
state of this LED toggles every 500 milliseconds in the loop function to
show activity.
PWM Configuration: For the "Reading Light,"
the code configures PWM (Pulse Width Modulation) on the GPIO15 pin to
enable dimming control. It sets the PWM range to 0-255 and the PWM
frequency to 1000 Hz.
FauxmoESP Configuration: The fauxmoESP
library is used to handle Alexa voice commands. Devices "Main light" and
"Reading Light" are added to fauxmo with the addDevice function.
Callback Function: A callback function is
defined using fauxmo.onSetState. This function is called when Alexa
sends a command. It checks the state (ON or OFF) and the value (0-255)
sent by Alexa for each device. For the "Main light," it turns the relay
on or off based on the state. For the "Reading Light," it uses PWM to
control the dimmable LED's brightness, applying gamma correction to
achieve a smooth dimming effect. The callback also prints information
about the received command to the serial monitor.
loop Function: In the loop function,
fauxmo.handle is called to handle Alexa commands. Additionally, it
toggles the state of the blue LED connected to GPIO2 (D4) every 500
milliseconds, creating a flashing effect.
Wi-Fi Setup: The setup_wifi function
configures the Wi-Fi connection to connect the ESP32 or ESP8266 to the
local network using the provided SSID and password.
A small insight into Alexa
While programming and using the fauxmo library I came across an
unusual problem. When I re-coded the above example from using one output
pin, to using two, I changed the device name label string (dev1).
The original name was 'RELAY' and I previously asked Alexa to find
devices. So the name relay appeared on the echo dot screen, and Alexa App, as a
controllable light bulb (a single LED).
After re-coding the sketch to use two LEDs I changed the names to
'Main light' and 'Reading light', then re-flashed the ESP8266. When I
asked Alexa to turn on 'RELAY' it actually turned on 'Main light'.
String compare still works!
This is interesting as the sketch specifically does a string match and so should not have reacted at all to the 'RELAY' command.
It shows that the underlying data transfer is not using strings sent
over the interface, it is using a token. That token is re-made into a
string at the ESP end so that the string match can work. This makes
sense as you don't want lots of string data to delay operations. The
problem is, unless you re-do the Alexa device list the names won't match
the expected device names.
How to verify Alexa strings are not updated but the interface still works:
It's very easy for the following two lines:
#defineDEV1"Main light"
#defineDEV2"Reading Light"
Change them to anything you want for example:
#defineDEV1"One"
#defineDEV2"Two"
Now recompile code for the ESP, and say "Alexa, turn on main light",.
You will see that it still works even though the device name in Alexa
is "Main light" but the encoded device name is "one". For confirmation
of the string in use look at the serial port output; you can see that
the string "one" is used when "Main light" is spoken.
The next section shows you how to get the new names into Alexa.
Deleting devices in Alexa
While developing code you might change device names and add or
subtract a few devices (these are the controlled output pins) so you
need to be able to change the Alexa echo dot information displayed (and
used).
The annoying thing is that you can't update echo dot from the echo
dot itself, you have to install the Alexa app on a mobile or the PC.
How to Delete devices in the mobile Alexa App
Start the Alexa App
The following screenshots show the Alex App actions you need to take
to delete a device. After that say to Alexa 'find devices' to obtain the
correct (or new) device names.
1.From the home screen
- select devices (bottom right):
2. Then select "Main light"
- middle group
3. Click the settings cog
- (top right)
4. Now hit the bin icon
- to delete the device.
After deleting all the items you need say to Alexa "Find
devices" and the correct string names will be populated in the Alexa
App and screen.
Conclusions
The fauxmoESP library makes it trivial to add voice controlled relay outputs to your Alexa network using an ESP32 or ESP8266.
You can also add dimmable light control so you can say things like
"Alex, turn on reading light at 50%". For this though its not just a
relay output - you would need a FET controlled output to LED drive for
the PWM signal to operate.
One tricky bit is that if you change device names in your
code during development, they won't match your Alexa device names; oddly
they will still work though with voice command! To correct it you
will need to delete the Alexa device names using the Alexa App, and then
re
initialize Alexa devices, by saying "Alexa find devices".
Written by John Main who has a degree in Electronic Engineering.
Note: Parts of this page were written using bard and chatgpt as research assistants.
Get started with an Arduino humidity sensor using the DHT11, which reports both humidity and temperature. Complete guide with full code for using this sensor
The Arduino delay function is an Extremely Useful function which you can use to get small delays. However, sometimes it's not the right function to use � there is another!
Control the colors of an RGB LED through Arduino code. This beginners guide covers connections and provides example sketches for interesting LED control...
Tired of wasting time trying to get the notoriously bad KY-032 IR sensor module to work reliably? How to use a 3 component substitute that actually works!
Comments
Have your say about what you just read! Leave me a comment in the box below.
Don’t see the comments box? Log in to your Facebook account, give Facebook consent, then return to this page and refresh it.