PROJECT - SWITCH MAINS SUPPLY SOCKET USING AMAZON'S ALEXA

Objective: build ESP8266-based system that switches a mains supply socket in response to an Alexa command

INTRODUCTION

Echo Dot

The Amazon range of Echo devices allows you to speak natural English to request that some predefined action takes place. The voice interface responds to "Alexa" after which you can make your request. For example, you can ask general questions like "Alexa, how many days until Christmas" or "Alexa, what time is it in Peru?". Using an ESP8266 that is emulating a Belkin WeMo Switch you can configure it to respond to commands like "Alexa, switch on the lights", or "Alexa, turn off the radio".

This project is based on an emulation of the Belkin WeMo Switch using third-party software called FauxMo. The ESP8266 communicates with Alexa and accepts requests and responds to them in the same way as the Belkin WeMo Switch. The FauxMo version I used is on github.com.

LIST OF COMPONENTS

You will need the following components:

and the following tools:

5V POWER SUPPLY

Hi-Link 5VDC power supply

I have found these Hi-Link 5V power supplies to be very convenient - they give 0.6A, which is fine, and they are small and can be mounted on a PCB if required. However, they have pins spaced in multiples of 5mm so require slight adjustment to fit standard 0.1" Veroboard.

There's a wide range of voltage and current outputs to choose from and I buy these on Ebay. I typically buy from China to get the lower prices but have to wait a few weeks for them to arrive so always keep a few in stock.

SOLID STATE RELAY

Crydom CX240D5 solid state relay

You can choose from a variety of manufacturers and SSR ratings but I would recommend a Crydom CX240D5, which is 5A 240V with a control voltage of 3-15VDC. This will switch on lights, pond pumps and so on. If you choose a higher rated SSR be sure to use the correct gauge of mains cabling.

HOW IT WORKS

The ESP8266 connects to the LAN and makes itself available for discovery by Alexa. When you say "Alexa, discover devices", Alexa sends out an SSDP packet that attempts to discover compatible devices with which it can communicate. SSDP is the basis for the UPnP discovery protocol that's used to discover printers and network services. Once discovered, the ESP8266 will be able to respond to command keywords that are configured in the sketch. Since the ESP8266 is emulating a switch, it can only switch on and off, but you can give different names to different actions within the same ESP8266 and it can detect which command was issued. You can also give different names to the same action. An ESP8266 can control many devices subject only to having enough GPIO pins available.

To delete or rename a command you must do this in the Alexa app on a tablet, phone, etc. and then ask Alexa to discovery the devices again. Renaming a device in the ESP8266 sketch does not work. If you define a multi-word phrase in the sketch, Alexa sometimes only recognises the first word. If this happens, you can edit the name in the Alexa app.

MAINS WIRING

STOP sign

To build this this project you will need to connect mains wiring to components, wire a 13A plug and wire a 13A socket. If you're not absolutely sure that you have the skills and competence to do this, stop now.

To minimise the risk of electric shock and to avoid possible shorting between connections, you must use silicone or heat-shrink sleeving over each soldered joint. Ideally, the sleeving should be the same colour as the wire it is protecting and it should cover the whole joint so that no metal wire or pin is showing.

You can assemble the components by referring to the diagrams below.

Schematic diagram

CONNECTING THE ESP8266

The diagram below shows the connections to the ESP8266. If your board is different, you need to connect to the pins with the same labels and not just use their relative positions. Fritzing diagram

THE BOOT PROCESS

When the ESP8266 starts, it tries to connect to the access points that are hardcoded in the sketch. During this period the WiFi status LED blinks green until it connects successfully and then it shows a steady green. If it cannot connect then it shows steady red, which indicates that none of the configured access points are in range or that the WiFi password is wrong. It then retries the whole connection process.

The ESP8266 then registers the names that Alexa should recognise, set its communications port and enables communication. The final step is to say "Alexa, discover devices". The Echo sends a discovery request on to the LAN and waits for valid responses. Alexa should reply with the number of new devices found and an example command to try.

Exclamation sign

This project relies on a number of software products from different sources and there is much discussion about which versions are mutually compatible. This project was created with the following software versions, hardware and set-up parameters.

Application, Library, Set-Up ParameterVersion, Value
Arduino IDE1.8.13 from Windows Store 1.8.39.0
ESP8266 Boards Package2.7.1
FauxMoxoseperez-fauxmoesp-20956d966a1c
ESP8266NodeMCU 1.0 (ESP-12E Module)

You can download the sketch used in this project from here.