Full speed emulation on your old 3DS and 2DS – GBA, SNES, MegaDrive, Genesis
24th April 2024Hack your Nintendo DS and play every DS and GBA game + more
3rd June 2024Connect Your ESP32 to Alexa with FauxmoESP
Amazon’s Alexa is a great smart home system that lets you control a wide range of devices with your voice, or through the Alexa app. So, wouldn’t it be great if we could connect our own projects to Alexa and control them using the Alexa system.
In this video I’ll show you a simple way of connecting an ESP32 to your Alexa network using some ready made software. This will let us test the connection process and prove the basic operations so we can then go on to build our own Alexa devices in subsequent posts.
FauxmoESP
To make things easy we’ll be using a ready made library, FauxmoESP
https://github.com/vintlabs/fauxmoESP
This package mimics the Phillips Hue lightbulb protocol to allow us to make our ESP32 appear as a number of named lights on our Alexa network. Each light can receive an on/off signal along with a brightness value in the range 0 to 255 corresponding to 0 to 100% brightness. As far as our project is concerned this will give us a number of input channels each having a boolean value and an integer byte value associated with it. What we do with these values once we get them is up to us.
Installing the Library
For this project I’m going to be using the Arduino IDE just to make things a bit easier. We need to get the correct libraries installed so our IDE can compile our Alexa code.
If you look at the FauxmoESP repository page you’ll see that it requires the AsyncTCP library as a dependency.
So in the IDE open the library manager and search for and install the latest versions of AsyncTCP and FauxmoESP.
That’s everything set up!
Testing the Basic Connection
The FauxmoESP library comes with a basic connection example. So in the file menu open the examples section and find the FauxmoESP basic example. Open that up and then save it as a new sketch on your computer. We’ll need to modify the files so we don’t want to mess up the example code.
Once you’ve saved the sketch we need to rename the crfedentials.sample.h file to credentials.h.
We can then edit that file to add our WiFi SSID and password information.
We’re now ready to test the code. So connect your ESP32, put it into download mode and upload the sketch.
Connecting to Alexa
The ESP32 should now be acting as a set of Phillips Hue lamps. If you open up the serial monitor you should see a 5 second message appearing showing everything is running OK.
Next open up the Alexa App on your phone and go to the devices tab. Click the plus sign to add a device and then scroll down to select the other device type. Then ask Alexa to scan for devices.
At this point Alexa is going to scan your network for anything that responds to the Alexa protocol. This is where the AsyncTCP library comes into play as that is used by FauxmoESP to create a TCP server that will listen for the Alexa network commands.
If all’s correct Alexa should find 5 new devices, all labelled as various coloured lamps. These are the ESP32.
Controlling the ESP32 using Alexa
You can now send command to the ESP32.
Using the app you can select a lamp and turn it on and off, and change its brightness. Each command will be displayed in the serial terminal showing you the device being addressed, the on/off state and the brightness byte integer value.
If you give Alexa the command e.g. yellow lamp on 50, it will ask the yellow lamp to turn on at 50% brightness which should appear as 128 in the serial monitor.
Adding some control and electronics
We’ve now got an Alexa powered application!
Using the Phillips Hue protocol gets us up and running quickly. Although we have to use the Hue Alexa commands our project can use the values received to do whatever it wants.
In the video I connect LEDs to five GPIO pins to mimic the 5 lamps. I then use the brightness value for each lamp to control the blink rate of the corresponding LED.
What Next?
This project gets us up and running quickly. Next we’ll need to create our own protocol with our own command set so we can customise it to our own project.
Download the Code
I’ve put the code used in the video onto a GitHub repository.
https://github.com/getis/esp32-fauxmo
Parts used in this project
ESP32 : https://amzn.to/3y1zq3a
Breadboard : https://amzn.to/4bmGrKi
Alexa Echo : https://amzn.to/4driBip