Overview
In this IoT project, I demonstrate how to set up Smart Home Automation with Alexa using the ESP8266 and Amazon Echo Dot. This ESP8266 Alexa Home Automation project solely relies on the Amazon Alexa App for controlling home devices with 4 relays. Even without the internet, you can manage devices with push buttons. Once the device is online, the NodeMCU ESP8266 auto-connects to WiFi, preserving the relay module’s last state.
Earlier we built Alexa Home Automation with fauxmoESP Library, but here we used Espalexa Library. I have designed the custom PCB for this project to make the device portable. I have also provided the complete schematic, PCB details, Gerber File, Bill of Materials and Source Code. The system works without any third-party IoT apps, making it straightforward for home automation setup.
Bill of Materials
For this project, we will need the following components. The component list, footprint, and quantity are given below.
| S.N. | Component | Designator | Footprint | Quantity |
|---|---|---|---|---|
| 1 | Capacitor 100nf | C1, C3, C5, C7, C8, C9, C10, C11 | C0805 | 8 |
| 2 | Capacitor 10uf | C2, C4, C6 | C0805 | 3 |
| 3 | Capacitor Electrolytic 100uf/25v | C12 | CAPPRD250W52D630H1250 | 1 |
| 4 | Capacitor Electrolytic 47uf/25v | C13 | CAPPRD200W55D500H1200 | 1 |
| 5 | Didoe 1N4007 SMD | D1, D2, D3, D4 | DIOM5027X262N | 4 |
| 6 | Optocoupler IC PC817 | IC1, IC3, IC4, IC5 | DIP762W60P254L458H450Q4N | 4 |
| 7 | ESP-12E WiFi Module | IC2 | ESP8266-12E | 1 |
| 8 | Terminal Block (1x2) | J1 | 1935776 | 1 |
| 9 | Terminal Block (1x3) | J2, J3, J4, J5 | 1985894 | 4 |
| 10 | Relay SRA-05VDC | K1, K2, K3, K4 | SRA-05VDC-CL | 4 |
| 11 | LED Red | LED1, LED2, LED3, LED4, LED5 | LED0805 | 5 |
| 12 | PROG (1x6 Header) | P1 | HDR1X6 | 1 |
| 13 | HLK-10M05 AC-DC 5V | PS1 | HLK10M05 | 1 |
| 14 | Transistor BC847 SMD | Q1, Q2, Q3, Q4, Q5, Q6 | BC847B | 6 |
| 15 | Resistor 220R | R1, R3, R9, R11, R14, R16, R17, R19 | R0805 | 8 |
| 16 | Resistor DNP | R2, R10, R15, R18 | R0805 | 4 |
| 17 | Resistor 12K | R4, R5, R6, R7, R8, R12, R13 | R0805 | 7 |
| 18 | Resistor 10K | R20, R21, R22, R23 | R0805 | 4 |
| 19 | Resistor 1k | R24, R25, R26, R27, R28 | R0805 | 5 |
| 20 | Push Button Switch | S1, S2, S3, S4, S5 | B3W-1020 | 5 |
| 21 | HT7333 3.3V LDO | U1 | IC_HT7333 | 1 |
Circuit Diagram & Hardware Design
Let us take a look at the Schematic of Alexa Smart Home Automation Project with ESP8266. The schematic is drawn using the Altium Designer Software.
We used SMD resistors, capacitors, and LED with the 0805 package. To convert 220V AC to 5V DC, we used an AC-to-DC Converter from Hi-Link. For providing power to ESP8266 raw chip and other peripherals, a low-power LDO HT7333 IC was used. To separate the high-power line from the 3.3V circuit PC817 optocoupler IC is used. The LED5 in the circuit is used to indicate power.
Similarly the LED1, LED2, LED3 & LED4 is used to represent Relay1, Relay2, Relay3 & Relay4 output respectively. The 4 Relays are connected to ESP8266 via GPIO16, GPIO14, GPIO12, GPIO10. A Reset button when pressed resets the ESP8266 Chip. The push buttons SW1, SW2, SW3, and SW4 are used in the manual controlling of Relays. They are connected to GPIO5, GPIO4, GPIO2 & GPIO13. You may attach a manual switch there and program the controller to control the circuit.
To program the ESP8266 Board, the Prog pin is provided. We can connect an FTDI Module to program the ESP8266 Chip directly. Two large value capacitors C12 (100uF) and C13 (47uF) are added to control voltage fluctuations.
Project PCB Gerber File & PCB Ordering Online
We have designed the PCB using the Altium Designer Software. It took quite a lot of time fixing all the isses in the PCB but still we managed to design a complete working custom PCB. The PCB design looks like this.
Here is the 3D View of the PCB from the front side as well as from the backside.


The Gerber File for the PCB is given below. You can simply download the Gerber File and order the PCB from PCBGOGO at 1$ only.
You can use this Gerber file to order high-quality PCB for this project. To do that visit the PCBGOGO official website by clicking here: https://www.pcbgogo.com/.
You can now upload the Gerber File by choosing the Quote Now option. From these options, you can choose the Material Type, Dimensions, Quantity, Thickness, Solder Mask Color and other required parameters.
After filling all details, select your country and shipping method. Finally you can place the order.
PCB & Hardware Assembly
After ordering the PCB, it took almost 5 days and I got my PCB.
The PCB quality from PCBGOGO is superb with very high quality. That is why most people trust PCBGOGO for PCB/PCBA Services.
First solder all the SMD components like resistors, capacitors, transistors, LEDs, voltage regulators & diodes. Be careful about the SMD LED polarity, place it in the proper direction. The SMD soldering is to be done on the both sides.
After soldering all these, you can solder the ESP8266 raw chip. The final stage would be soldering all the through-hole components like Optocoupler IC, terminal block, Relays, male-female headers, and AC-to-DC Converter Module.
After soldering all the components, the ESP8266 Alexa Smart Home Automation Board is ready for the test. You can upload a blink sketch by connecting a USB-to-TTL Converter Module.
Note: There were some voltage fluctuation issues in the designed PCB, so I have updated the design by adding some large capacitors. Also, some connection issues have been fixed. The Gerber file above is updated and the PCB would be little different from shown above. But overall the functionality is same.
Espalexa Library
In this project, we will be using the Espalexa library to integrate the ESP8266 Home Automation Board with Alexa Echo Dot.
So, you’ve got this cool library called Espalexa. What’s it all about? Imagine being able to chat with Alexa and get her to control your ESP gadgets, whether they’re ESP8266 or ESP32. This library makes that chat a breeze, and the best part? It’s all packed into an Arduino-friendly format.
Now, what sets Espalexa apart from other similar tools? Well, it gives you a ton of flexibility with voice commands. Instead of just turning things on or off, you can get precise. Tell Alexa, “Hey, dim that light to 75%,” or “Can we get the room to 21 degrees?” And she’ll handle it.
Even cooler? Espalexa has jumped onto the color train! Now you can say, “Alexa, let’s get a blue mood in here,” and your light will change color. If you’re more into shades of white, it’s got you covered too. By the starting line, Espalexa can juggle up to 10 devices.
Source Code/Program for ESP8266 Alexa Home Automation Project
Here is the code for the Smart & Manual Home Automation Project with Alexa & ESP8266. This code is all about giving you two ways to control devices in your home. Imagine you have four lamps, and each lamp is connected to a relay (which is like a remote-controlled switch). Now, with this code:
- You can ask Alexa to turn on or adjust any of these lamps. “Alexa, turn on Lamp 1!” and it’ll do just that.
- But what if your internet goes down? No worries! There are manual switches you can press to control the lamps too.
- The system is smart enough to keep checking if it’s connected to the internet. If it finds a connection, it’ll set itself up so you can use Alexa. If not, it’ll keep working with the manual switches.
Here is the complete code. In this code make changes to the WiFi SSID and Password.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
// Include necessary libraries #include <ESP8266WiFi.h> #include <Espalexa.h> // =================== CONSTANTS =================== // Define GPIO pins for relays and switches #define RelayPin1 16 #define RelayPin2 14 #define RelayPin3 12 #define RelayPin4 10 #define SwitchPin1 5 #define SwitchPin2 4 #define SwitchPin3 2 #define SwitchPin4 13 const char* ssid = "**********"; const char* password = "**********"; String Device_1_Name = "Relay 1"; String Device_2_Name = "Relay 2"; String Device_3_Name = "Relay 3"; String Device_4_Name = "Relay 4"; // =================== VARIABLES =================== int toggleState_1 = 0; int toggleState_2 = 0; int toggleState_3 = 0; int toggleState_4 = 0; boolean wifiConnected = false; // Create an instance of the Espalexa class Espalexa espalexa; // =================== FUNCTION DECLARATIONS =================== boolean connectWifi(); void firstLightChanged(uint8_t brightness); void secondLightChanged(uint8_t brightness); void thirdLightChanged(uint8_t brightness); void fourthLightChanged(uint8_t brightness); void relayOnOff(int relay); void addDevices(); // =================== SETUP =================== void setup() { Serial.begin(115200); // Start serial communication // Initialize pins for relay and switches pinMode(RelayPin1, OUTPUT); pinMode(RelayPin2, OUTPUT); pinMode(RelayPin3, OUTPUT); pinMode(RelayPin4, OUTPUT); pinMode(SwitchPin1, INPUT_PULLUP); pinMode(SwitchPin2, INPUT_PULLUP); pinMode(SwitchPin3, INPUT_PULLUP); pinMode(SwitchPin4, INPUT_PULLUP); // Ensure all relays are turned off initially digitalWrite(RelayPin1, LOW); digitalWrite(RelayPin2, LOW); digitalWrite(RelayPin3, LOW); digitalWrite(RelayPin4, LOW); // Try connecting to WiFi wifiConnected = connectWifi(); if (wifiConnected) { addDevices(); } else { Serial.println("Cannot connect to WiFi. So in Manual Mode"); delay(1000); } } // =================== MAIN LOOP =================== void loop() { // Handle manual switches regardless of WiFi status if (digitalRead(SwitchPin1) == LOW) { delay(200); relayOnOff(1); } else if (digitalRead(SwitchPin2) == LOW) { delay(200); relayOnOff(2); } else if (digitalRead(SwitchPin3) == LOW) { delay(200); relayOnOff(3); } else if (digitalRead(SwitchPin4) == LOW) { delay(200); relayOnOff(4); } // Handle WiFi connections if (wifiConnected) { espalexa.loop(); delay(1); } else { wifiConnected = connectWifi(); // Try to reconnect if not connected if (wifiConnected) { addDevices(); } } } // =================== FUNCTION IMPLEMENTATIONS =================== boolean connectWifi() { int attempts = 0; boolean state = true; WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); Serial.println("Connecting to WiFi"); while (WiFi.status() != WL_CONNECTED && attempts < 20) { delay(500); Serial.print("."); attempts++; } // Handle successful connection if (WiFi.status() == WL_CONNECTED) { Serial.print("Connected to "); Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP()); } else { state = false; Serial.println("Connection failed."); } return state; } // Functions for each relay to handle toggle operations void relayOnOff(int relay) { // Based on the relay number, toggle its state switch (relay) { case 1: if (toggleState_1 == 0) { digitalWrite(RelayPin1, HIGH); // turn on relay 1 toggleState_1 = 1; Serial.println("Device1 ON"); } else { digitalWrite(RelayPin1, LOW); // turn off relay 1 toggleState_1 = 0; Serial.println("Device1 OFF"); } delay(100); break; case 2: if (toggleState_2 == 0) { digitalWrite(RelayPin2, HIGH); // turn on relay 2 toggleState_2 = 1; Serial.println("Device2 ON"); } else { digitalWrite(RelayPin2, LOW); // turn off relay 2 toggleState_2 = 0; Serial.println("Device2 OFF"); } delay(100); break; case 3: if (toggleState_3 == 0) { digitalWrite(RelayPin3, HIGH); // turn on relay 3 toggleState_3 = 1; Serial.println("Device3 ON"); } else { digitalWrite(RelayPin3, LOW); // turn off relay 3 toggleState_3 = 0; Serial.println("Device3 OFF"); } delay(100); break; case 4: if (toggleState_4 == 0) { digitalWrite(RelayPin4, HIGH); // turn on relay 4 toggleState_4 = 1; Serial.println("Device4 ON"); } else { digitalWrite(RelayPin4, LOW); // turn off relay 4 toggleState_4 = 0; Serial.println("Device4 OFF"); } delay(100); break; default : break; } } // Functions to handle Alexa brightness changes for each device void firstLightChanged(uint8_t brightness) { // If brightness is set to 255, turn on the relay, otherwise turn it off if (brightness == 255) { digitalWrite(RelayPin1, HIGH); Serial.println("Device1 ON"); toggleState_1 = 1; } else { digitalWrite(RelayPin1, LOW); Serial.println("Device1 OFF"); toggleState_1 = 0; } } void secondLightChanged(uint8_t brightness) { // If brightness is set to 255, turn on the relay, otherwise turn it off if (brightness == 255) { digitalWrite(RelayPin2, HIGH); Serial.println("Device2 ON"); toggleState_2 = 1; } else { digitalWrite(RelayPin2, LOW); Serial.println("Device2 OFF"); toggleState_2 = 0; } } void thirdLightChanged(uint8_t brightness) { // If brightness is set to 255, turn on the relay, otherwise turn it off if (brightness == 255) { digitalWrite(RelayPin3, HIGH); Serial.println("Device3 ON"); toggleState_3 = 1; } else { digitalWrite(RelayPin3, LOW); Serial.println("Device3 OFF"); toggleState_3 = 0; } } void fourthLightChanged(uint8_t brightness) { // If brightness is set to 255, turn on the relay, otherwise turn it off if (brightness == 255) { digitalWrite(RelayPin4, HIGH); Serial.println("Device4 ON"); toggleState_4 = 1; } else { digitalWrite(RelayPin4, LOW); Serial.println("Device4 OFF"); toggleState_4 = 0; } } // Function to add the devices for Alexa control void addDevices() { // Define the devices and their respective callback functions // and start the Espalexa service espalexa.addDevice(Device_1_Name, firstLightChanged); espalexa.addDevice(Device_2_Name, secondLightChanged); espalexa.addDevice(Device_3_Name, thirdLightChanged); espalexa.addDevice(Device_4_Name, fourthLightChanged); espalexa.begin(); } |
To progam the ESP8266 Raw Chip, connect the FTDI Module to your PROG Pin of PCB.
Then go to the tools & select ESP8266 Generic Module that you are using for this project. Also, select the COM port. Then click on the upload option to upload the code to the ESP8266 board.
Note: While uploading the code disconnect the relay from the power source.
Once the code uploading is done, open the Serial Monitor. The ESP8266 will try connecting to the WiFi Network. Once it gets connected to the WiFi Network, the Serial Monitor will display the IP Address of ESP8266.
Now remove the FTDI Module connection and power the device using the AC Supply.
Setting up Alexa App & Echo Dot
Now you need to setup the Alexa App and Alexa Echo Dot for the Home Automation using the ESP8266. Here are the steps for this.
- Open the Alexa app on your smartphone.
- Go to the Devices tab (found at the bottom right).
- Tap on the ‘+‘ (plus) sign at the top-right.
- Choose “Add Device“. Then Select “Light“.
- Scroll down and choose “Other“. Then Select “WiFi“.
- Tap on “Discover Devices“. Alexa will search for new devices.
- The Alexa app will start searching for smart devices. After searching 4 devices will appear and the devices should be named “Relay 1“, “Relay 2“, “Relay 3“, and “Relay 4” as provided in the code.
- Once Alexa finds the devices, they will be listed in the Alexa app.
You might encounter the “No new devices found” error when trying to add devices with Alexa. If this happens, restart your Echo Dot. Once it reconnects to the same WiFi network, select “Try discovery again“.
From this Dashboard, you can turn on/off the 4 Relays in case, you wanna control through the app instead of voice commands.
Testing Smart & Manual Home Automation with Alexa & ESP8266
Now that the relays are set up in the Alexa app, you can control them and turn on with voice commands. For example:
- “Alexa, turn on Relay 1.”
- “Alexa, turn on Relay 2.”
- “Alexa, turn on Relay 3.”
- “Alexa, turn on Relay 4.”
To turn off the Relays, you can control them with the voice command as:
- “Alexa, turn off Relay 1.”
- “Alexa, turn off Relay 2.”
- “Alexa, turn off Relay 3.”
- “Alexa, turn off Relay 4.”
Alternatively, you can manually control them through the Alexa app by tapping on the device and using the on-screen toggle.
You may press the manual push buttons to turn the on/off the Relays. While pressing the push button, the relays will toggle.
We did something great here! We combined normal home switches with Alexa’s voice power using ESP8266. The neat part? If you lose WiFi, you can still flip the switches by hand. With this project, we’re mixing old ways with new tech to make our homes smarter and simpler to use. The same project can be done using the ESP32 & Alexa device.




























1 Comment
This is NOT AUTOMATION, just a remote switch >> you need to push the button on the phone’ s screen to turn lights on/off