Random Nerd Tutorials
Shares

Alexa (Echo) with ESP32 and ESP8266 – Voice Controlled Relay

Shares

In this project, you’re going to learn how to control the ESP8266 or the ESP32 with voice commands using Alexa (Amazon Echo Dot). As an example, we’ll control two 12V lamps connected to a relay module. We’ll also add two 433 MHz RF wall panel switches to physically control the lamps. 

Watch the Project Video Demonstration


We recommend the following tutorials as a reference:

Project Overview

This project works both with ESP8266 and ESP32. We provide instructions for both development boards. Before getting straight to the project, read this section to see what you’ll achieve by the end of this project.

Control Lamps using Alexa

By the end of this project you’ll be able to control two lamps (lamp 1 and lamp 2) with voices commands using Alexa. The figure below shows a high-level overview on how the project works to control lamp 1 – it works similarly for lamp 2.

Alexa will respond to the following commands:

  • “Alexa, turn on lamp 1”
  • “Alexa, turn off lamp 1”
  • “Alexa, turn on lamp 2”
  • “Alexa, turn on lamp 2”
  • “Alexa, turn on lamps” turns on both lamps
  • “Alexa, turn off lamps” turns off both lamps

When you say something like “Alexa, turn on lamp 1”, the ESP8266 or ESP32 will trigger a relay to turn on lamp 1. When you say something like “Alexa, turn off lamp 1”, the ESP8266 or ESP32 will send a signal to the relay to turn off the lamp. This works similarly for lamp 2.

Control Lamps using 433 MHz Wall Switches

In this project, we’ll also add two 433 MHz wall switches to physically control the lamps. You’ll have a switch for each lamp. The switch changes the lamp’s state to the opposite of its current state. For example, if the lamp is off, press the wall switch to turn it on. To turn it off, you just need to press the switch again. Take a look at the figure below that illustrates how it works.

Parts Required

Here’s a complete list of the parts required for this project (click the links below to find the best price at Maker Advisor):

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

How to Buy An Amazon Echo

You can use the links below to buy an Amazon Echo. There are several models available – all of them are compatible with this project.

Buying an Amazon Echo through Amazon is not possible for all countries. We provide links for Amazon in UK, USA, and Germany. If the Amazon Echo does not ship to your country through Amazon, you can get one from eBay (available worldwide).

Echo Dot (2nd Generation)

Echo (2nd Generation)

Echo Show

433 MHz RF Wall Panel Switch

The 433 MHz RF wall panel switch is a great way to remotely control devices. It can be easily attached to a wall with adhesive tap, without the need to make holes on the walls. Additionally, it is wireless, so you don’t need to worry about wiring and then hiding cables.

In this project we’re using two wall panel switches. Instead, you can use a panel switch with two buttons – there are also another version with three switches.

This wall panel switch has a push button in its circuit, as shown in the figure below, that when pressed emits a 433 MHz signal. You can use that signal to control whatever you want. This wall panel switch uses a 27A 12V type battery (not included in the package). So, you may want to buy one, when you get your wall panel switch.

Decode the Wall Panel Switch 433 MHz RF Signals

When you press the 433 MHz wall panel switch, it sends a 433 MHz signal. You need to decode that signal using a 433 MHz receiver. To learn how to decode the 433 MHz signal read the following post: Decode and Send 433 MHz RF Signals with Arduino – read the “Decoder Sketch” part. The sketch works with Arduino, ESP32, and ESP8266.

Take note of the decimal (24Bit) code for each of your switches, because you’ll need them later.

In my case:

  • switch 1: 6819768
  • switch 2: 9463928

You should get different values. You’ll then use these signals in your ESP8266 or ESP32 sketch. When you press the switch, it sends a 433 MHz signal. This signal is detected by the receiver that is connected to the ESP. This way, the ESP knows the switch was pressed and it inverts the lamp’s current state.

The FauxmoESP

To control your ESP8266 or ESP32 with Amazon Echo, you need to install the FauxmoESP library. This library emulates a Belkin Wemo device, allowing you to control your ESP32 or ESP8266 using this protocol. This way, the Echo or Echo Dot instantly recognizes the device, after uploading the code, without any extra skills or third party services. You can read more about FauxmoESP here.

Installing the FauxmoESP Library

  1. Click here to download the FauxmoESP library. You should have a .zip folder in your Downloads
  2. Unzip the .zip folder and you should get xoseperez-fauxmoesp-50cbcf3087fd folder
  3. Rename your folder from xoseperez-fauxmoesp-50cbcf3087fd to xoseperez_fauxmoesp
  4. Move the xoseperez_fauxmoesp folder to your Arduino IDE installation libraries folder
  5. Finally, re-open your Arduino IDE

Alexa – Echo Dot with ESP8266

Follow these next instructions if you’re using an ESP8266.

Installing the ESP8266 Board in Arduino IDE

In order to upload code to your ESP8266 using Arduino IDE, you should install an add-on for the Arduino IDE that allows you to program the ESP8266 using the Arduino IDE and its programming language. If you haven’t installed the ESP8266 add-on for the Arduino IDE, follow the next tutorial:

Installing the ESPAsyncTCP Library

You also need to install the ESPAsyncTCP Library library. Follow the next instructions to install it:

  1. Click here to download the ESPAsyncTCP library. You should have a .zip folder in your Downloads
  2. Unzip the .zip folder and you should get ESPAsyncTCP-master folder
  3. Rename your folder from ESPAsyncTCP-master to ESPAsyncTCP
  4. Move the ESPAsyncTCP folder to your Arduino IDE installation libraries folder
  5. Finally, re-open your Arduino IDE

Schematic

If you’re using an ESP8266 board, assemble your circuit by following the next schematic diagram – you can click the image to zoom.

If you’re having trouble following the circuit diagram, you can use the following table as a reference:

ESP8266 Connect to
GPIO 5 433 MHz receiver data pin
GPIO 4 Relay IN1 pin
GPIO 14 Relay IN2 pin

 

IMPORTANT NOTE: before applying power, make sure you set your step-down buck converter output voltage to 5V! Otherwise, you may damage your ESP.

Alexa – Echo Dot with ESP32

Follow these next instructions if you’re using an ESP32.

Installing the ESP32 Board in Arduino IDE

In order to upload code to your ESP32 using Arduino IDE, you should install an add-on for the Arduino IDE that allows you to program the ESP32 using the Arduino IDE and its programming language. If you haven’t installed the ESP32 add-on for the Arduino IDE, follow the next tutorial:

Installing the AsyncTCP Library

You also need to install the AsyncTCP Library. Follow the next instructions to install it:

  1. Click here to download the AsyncTCP library. You should have a .zip folder in your Downloads
  2. Unzip the .zip folder and you should get AsyncTCP-master folder
  3. Rename your folder from AsyncTCP-master to AsyncTCP
  4. Move the AsyncTCP folder to your Arduino IDE installation libraries folder
  5. Finally, re-open your Arduino IDE

Schematic

If you’re using an ESP32 board, assemble your circuit by following the next schematic diagram – you can click the image to zoom.

 

If you’re having trouble following the circuit diagram, you can use the following table as a reference:

ESP32 Connect to
GPIO 13 433 MHz receiver data pin
GPIO 14 Relay IN1 pin
GPIO 12 Relay IN2 pin

 

IMPORTANT NOTE: before applying power, make sure you set your step-down buck converter output voltage to 5V! Otherwise, you may damage your ESP.

Code

Copy the following code to your Arduino IDE, but don’t upload it yet! You need to make some changes to make it work for you.

Selecting the right board

This code works both with ESP32 and ESP8266. To make it work for your board, you need to select the board you’re using in Tools > Board. Select your ESP8266 or ESP32 model.

Add your network credentials

You need to modify the following lines to include your network credentials.

#define WIFI_SSID "REPLACE_WITH_YOUR_SSID"
#define WIFI_PASS "REPLACE_WITH_YOUR_PASSWORD"

Add your 433 MHz signal codes

You also need to include the signals you’ve decoded previously for your wall panel switches.

Replace the value highlighted in red with the value you’ve gotten for the switch that controls lamp 1:

if (mySwitch.getReceivedValue()==6819768) {
  digitalWrite(RELAY_PIN_1, !digitalRead(RELAY_PIN_1));
}

And the value for lamp 2 in the following:

if (mySwitch.getReceivedValue()==9463928) {
  digitalWrite(RELAY_PIN_2, !digitalRead(RELAY_PIN_2));
}

Uploading the code

After making all the necessary changes, you can upload code to your ESP. Make sure you have the right COM port selected, in Tools > Port. For demonstration purposes, you can open your Serial Monitor at a baud rate of 115200, while you prepare your Echo Dot.

Alexa, Discover Devices

With the circuit ready, and the code uploaded to your ESP8266 or ESP32, you need to ask alexa to discover devices.

Say: “Alexa, discover devices”. It should answer as shown in the figure below.

Alternatively, you can also discover devices using the Amazon Alexa app, by following the steps shown in the figure below.

Then, ask Alexa to turn on/off the lamps. You’ll also get information about the lamps state on the Serial Monitor.

After making sure everything is working properly, you can turn your circuit into a permanent solution.

Demonstration

For demonstration purposes, we’ve built our circuit in a prototyping stripboard, and attached everything in a wooden board, as shown in the figure below:

Now you can ask Alexa to control your lamps with the following voice commands:

  • “Alexa, turn on lamp 1”
  • “Alexa, turn off lamp 1”
  • “Alexa, turn on lamp 2”
  • “Alexa, turn on lamp 2”

You can also control both lamps at the same time by creating a group in the Amazon Alexa app. We called it “lamps”.

Now, you can control both lamps at the same time, using the following voice commands.

  • “Alexa, turn on lamps”
  • “Alexa, turn off lamps”

You can also physically control your lamps using the 433 MHz wall panel switches.

Wrapping Up

In this project we’ve shown how to control your ESP8266 and your ESP32 with voice commands using Amazon Echo. As an example, we’ve controlled two 12V lamps using a relay. Instead of 12V lamps, you can control any other electronics appliances. We’ve also shown you how you can remotely control your lamps using a 433 MHz wall panel switch.

We hope you’ve found this project useful. If you liked this post, you may also like:

Thanks for reading.


Learn ESP32 with Arduino IDE

This our complete guide to program the ESP32 with Arduino IDE, including projects, tips, and tricks! The registration closes soon, so SIGN UP NOW »

Leave a Comment:

Add Your Reply

⏰ Registrations are open ⏰
Sign up for "Learn ESP32 with Arduino IDE" Course