In this NodeMCU project, I have shown how to make an Alexa home automation project with manual switches using NodeMCU ESP8266, relay module, and amazon echo dot. For this ESP8266 Alexa automation project, I have only used Amazon Alexa App to control the home appliances with Alexa.

Alexa Home Automation with NodeMCU

If there is no internet, still you can also control the home appliances from the push buttons and when the internet comes back the NodeMCU will automatically connect with the WiFi. It will also remember the previous state of the relay module.

For this IoT project, I have not used any third party IoT applications or any custom design PCB, so you can easily make this amazon echo home automation system at home.

Circuit of ESP8266 Alexa Automation

ESP8266 Alexa Automation circuit

The circuit is very simple, here I have used D1, D2, D5, D6 GPIOs to connect the relay module and the push buttons are connected with SD3, D3, D7, RX GPIO pins of NodeMCU.

I have used a 5-Volt 2-Amp mobile charger to supply this home automation system.

Required Components for this NodeMCU Project:

Components for amazon echo home automation
  1. NodeMCU
  2. 4-channel Relay Module (5V)
  3. Push Buttons 4nos
  4. Amazon Alexa Echo Dot

Tutorial video on Amazon Echo Home Automation

In this tutorial video, I have shown all the steps to make amazon echo home automation using ESP8266 NodeMCU. I have also explained how to add devices in Amazon Alexa app to control the home appliances with Alexa.

No third party IoT App

In this home automation project, I have not used any third party IoT Application like Blynk or IFTTT.

Program NodeMCU with Arduino IDE

program NodeMCU with Arduino IDE

For this Alexa home automation project, I have used the Arduino IDE to program NodeMCU.

First update the Preferences –> Aditional boards Manager URLs: https://dl.espressif.com/dl/package_esp32_index.json, http://arduino.esp8266.com/stable/package_esp8266com_index.json

Then install the ESP8266 board, I have used the 2.5.1 version, you may get issues with the latest version of ESP8266.

After that install the ESPAlexa library. Click Here to download ESPAlexa library.

Code for Alexa Home Automation

#ifdef ARDUINO_ARCH_ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#include <Espalexa.h>
// define the GPIO connected with Relays and switches
#define RelayPin1 5  //D1
#define RelayPin2 4  //D2
#define RelayPin3 14 //D5
#define RelayPin4 12 //D6

#define SwitchPin1 10  //SD3
#define SwitchPin2 0   //D3 
#define SwitchPin3 13  //D7
#define SwitchPin4 3   //RX

#define wifiLed   16   //D0

int toggleState_1 = 0; //Define integer to remember the toggle state for relay 1
int toggleState_2 = 0; //Define integer to remember the toggle state for relay 2
int toggleState_3 = 0; //Define integer to remember the toggle state for relay 3
int toggleState_4 = 0; //Define integer to remember the toggle state for relay 4

// prototypes
boolean connectWifi();
//callback functions
void firstLightChanged(uint8_t brightness);
void secondLightChanged(uint8_t brightness);
void thirdLightChanged(uint8_t brightness);
void fourthLightChanged(uint8_t brightness);

// WiFi Credentials
const char* ssid = "WiFi Name";
const char* password = "WiFi Password";

// device names
String Device_1_Name = "Study Lamp";
String Device_2_Name = "CFL";
String Device_3_Name = "Yellow light";
String Device_4_Name = "Red light";

boolean wifiConnected = false;

Espalexa espalexa;

//our callback functions
void firstLightChanged(uint8_t brightness)
{
  //Control the device
  if (brightness == 255)
    {
      digitalWrite(RelayPin1, LOW);
      Serial.println("Device1 ON");
      toggleState_1 = 1;
    }
  else
  {
    digitalWrite(RelayPin1, HIGH);
    Serial.println("Device1 OFF");
    toggleState_1 = 0;
  }
}

void secondLightChanged(uint8_t brightness)
{
  //Control the device 
  if (brightness == 255)
    {
      digitalWrite(RelayPin2, LOW);
      Serial.println("Device2 ON");
      toggleState_2 = 1;
    }
  else
  {
    digitalWrite(RelayPin2, HIGH);
    Serial.println("Device2 OFF");
    toggleState_2 = 0;
  }
}

void thirdLightChanged(uint8_t brightness)
{
  //Control the device  
  if (brightness == 255)
    {
      digitalWrite(RelayPin3, LOW);
      Serial.println("Device3 ON");
      toggleState_3 = 1;
    }
  else
  {
    digitalWrite(RelayPin3, HIGH);
    Serial.println("Device3 OFF");
    toggleState_3 = 0;
  }
}

void fourthLightChanged(uint8_t brightness)
{
  //Control the device 
  if (brightness == 255)
    {
      digitalWrite(RelayPin4, LOW);
      Serial.println("Device4 ON");
      toggleState_4 = 1;
    }
  else
  {
    digitalWrite(RelayPin4, HIGH);
    Serial.println("Device4 OFF");
    toggleState_4 = 0;
  }
}

void relayOnOff(int relay){

    switch(relay){
      case 1: 
             if(toggleState_1 == 0){
              digitalWrite(RelayPin1, LOW); // turn on relay 1
              toggleState_1 = 1;
              Serial.println("Device1 ON");
              }
             else{
              digitalWrite(RelayPin1, HIGH); // turn off relay 1
              toggleState_1 = 0;
              Serial.println("Device1 OFF");
              }
             delay(100);
      break;
      case 2: 
             if(toggleState_2 == 0){
              digitalWrite(RelayPin2, LOW); // turn on relay 2
              toggleState_2 = 1;
              Serial.println("Device2 ON");
              }
             else{
              digitalWrite(RelayPin2, HIGH); // turn off relay 2
              toggleState_2 = 0;
              Serial.println("Device2 OFF");
              }
             delay(100);
      break;
      case 3: 
             if(toggleState_3 == 0){
              digitalWrite(RelayPin3, LOW); // turn on relay 3
              toggleState_3 = 1;
              Serial.println("Device3 ON");
              }else{
              digitalWrite(RelayPin3, HIGH); // turn off relay 3
              toggleState_3 = 0;
              Serial.println("Device3 OFF");
              }
             delay(100);
      break;
      case 4: 
             if(toggleState_4 == 0){
              digitalWrite(RelayPin4, LOW); // turn on relay 4
              toggleState_4 = 1;
              Serial.println("Device4 ON");
              }
             else{
              digitalWrite(RelayPin4, HIGH); // turn off relay 4
              toggleState_4 = 0;
              Serial.println("Device4 OFF");
              }
             delay(100);
      break;
      default : break;      
      }
}
// connect to wifi – returns true if successful or false if not
boolean connectWifi()
{
  boolean state = true;
  int i = 0;

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.println("");
  Serial.println("Connecting to WiFi");

  // Wait for connection
  Serial.print("Connecting...");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    if (i > 20) {
      state = false; break;
    }
    i++;
  }
  Serial.println("");
  if (state) {
    Serial.print("Connected to ");
    Serial.println(ssid);
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
  }
  else {
    Serial.println("Connection failed.");
  }
  return state;
}

void addDevices(){
  // Define your devices here.
  espalexa.addDevice(Device_1_Name, firstLightChanged); //simplest definition, default state off
  espalexa.addDevice(Device_2_Name, secondLightChanged);
  espalexa.addDevice(Device_3_Name, thirdLightChanged);
  espalexa.addDevice(Device_4_Name, fourthLightChanged);
  espalexa.begin();
}
void setup()
{
  Serial.begin(115200);

  pinMode(RelayPin1, OUTPUT);
  pinMode(RelayPin2, OUTPUT);
  pinMode(RelayPin3, OUTPUT);
  pinMode(RelayPin4, OUTPUT);
  pinMode(wifiLed, OUTPUT);

  pinMode(SwitchPin1, INPUT_PULLUP);
  pinMode(SwitchPin2, INPUT_PULLUP);
  pinMode(SwitchPin3, INPUT_PULLUP);
  pinMode(SwitchPin4, INPUT_PULLUP);

  //During Starting all Relays should TURN OFF
  digitalWrite(RelayPin1, HIGH);
  digitalWrite(RelayPin2, HIGH);
  digitalWrite(RelayPin3, HIGH);
  digitalWrite(RelayPin4, HIGH);

  // Initialise wifi connection
  wifiConnected = connectWifi();

  if (wifiConnected)
  {
    addDevices();
  }
  else
  {
    Serial.println("Cannot connect to WiFi. So in Manual Mode");
    delay(1000);
  }
}

void loop()
{
   if (WiFi.status() != WL_CONNECTED)
  {
    //Serial.print("WiFi Not Connected ");
    //Serial.println(wifiConnected);
    digitalWrite(wifiLed, HIGH); //Turn off WiFi LED
    //Manual Switch Control
    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);
    }
    
  }
  else
  {
    //Serial.print("WiFi Connected  ");
    //Serial.println(wifiConnected);
    digitalWrite(wifiLed, LOW);
    //Manual Switch Control
    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);
    }
    //WiFi Control
    if (wifiConnected){
      espalexa.loop();
      delay(1);
    }
    else {
      wifiConnected = connectWifi(); // Initialise wifi connection
      if(wifiConnected){
      addDevices();
      }
    }
  }
}

Enter the following WiFi credential in the code:

  1. WiFi Name at “WiFi Name”
  2. WiFi Password at “WiFi Password”

Then enter the device names in the code. Alexa will identify that device with the name mentioned in the code.

Now select the board as NodeMCU 1.0 and the PORT in Arduino IDE. Then click on the upload button to program the NodeMCU board.

no wifi control with nodemcu

After uploading the code to NodeMCU, the blue LED connected with pin D0 should turn on as shown in picture.

If the internet is not available, the blue LED will turn off. Then you can control the relay module with push buttons.

And when the internet comes back the NodeMCU will automatically connect with the WiFi and the blue LED will turn on.

Then you can control the home appliances with Alexa and the push buttons.

Configure Amazon Alexa App

Configure Amazon Alexa App

In the tutorial video, I have explained all the steps to add devices in Amazon Alexa App.

While configuring the Amazon Alexa App, the NodeMCU or ESP8265, Amazon echo dot and the Mobile should be connected with same the wifi.

Once the Alexa App found all the devices, you can control those devices with voice command using Alexa echo dot.

no device found in Alexa App

You may face No new devices found error while adding the devices with Alexa.

Then restart the echo dot, after restart once the echo dot connect with the same WiFi, tap on “Try discovery again”.

Connect Home Appliances with Relay

Connect Home Appliances

Now, please refer the above circuit to connect 4 home appliances with relay module.

Please take proper safety precaution while working with high voltage.

Amazon Echo Home Automation system is ready

Alexa turn on light
controlling light with Alexa

Now, the IoT project on Alexa home automation is ready, so you can control the home appliances with voice command.

“Alexa, turn on light”, “Alexa, turn off light”, “Alexa , turn on studio light”, etc.

The NodeMCU and Echo Dot should be connected with same wifi.

manually controlling light
Controlling light from push buttons

I hope you like this Smart home IoT projects idea with the ESP8266 and Alexa app.

Click Here for more such ESP32 projects.

Please do share your feedback on this IoT project. Thank you for your time.

This Post Has 13 Comments

  1. Erik Hoffman

    You are magic!
    After Amazon stopped supporting Alexa nearly 2 years ago I was gutted that my project stopped working, so thank you so much for reviving my idle project.
    Many thanks
    Erik Hoffman

  2. narendra lohar

    hey i’m unable to find device after completing all the above process….plz give me feedback…….7727932917

    1. Subhajit

      Restart the EchoDot then tap on discover devices in the Alexa app. Please refer the tutorial video. Thank you.

  3. V B Srivastava

    Please explain how to operate Fan, Lights using Alexa app, Node MCU esp 8266, Relay and without any branded smart devices.( Alexa, Philips, Oakter, Google Eacho etc)

    1. Subhajit

      Ok. Will publish a new article on this.

  4. Lo

    @ Subhajit
    Nice tutorial, really usefull, thank you :p

  5. Juan

    Hello friend, an exceptional job, could it also be used to open the garage door? with magnetic sensor to know when the door is open or closed?

  6. rob

    great tutorial.

    Had to copy the code direct from here as the link seemed to involve a code with dimming etc when i just wanted to turn on and off relays. Had an issue half way through where i couldn’t find the port again after modifying the code for the esp device and uploading the new code, but was down to a crappy cable.

    Hats off to you, Subhajit, for helping ppl like me :]

  7. Jaison Meneghel

    My Alexa generation 3 can’t find the device, I already hung up and turned on Alexa but can’t find it, can you help me?

    1. Subhajit

      First check, if the devices already added to Alexa App. If not then restart echo dot, and try again.

  8. Vipin Tiwari

    How to change ssid and password without hardcode

Leave a Reply