Amazon Alexa Smart Home Using Node MCU

Introduction: Amazon Alexa Smart Home Using Node MCU

About: ignite.innovate.ideas

UPDATE : THE UPDATE VERSION OF THIS PROJECT CAN BE FOUND HERE

One of the most common hobby projects you will come across the internet is HomeAutomation. There you will be controlling devices in your house through your smartphone via wifi/Bluetooth. But here we won't be using any smartphone. we will be using our own voice for it. No need to take phone everytime to control any device. Just say "Alexa turn on the device" and it will automatically do it for you.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: THINGS YOU NEED

Step 2: WHAT IS ALEXA? WHO IS SHE ?? 

Amazon Alexa is a virtual assistant developed by Amazon. This assistant is used in Amazon speaker(Echo, Echo dot, Plus). This virtual assistant do many things like play music, read the flash news and much more if you want to know more about it then read this article

Step 3: WATCH THE VIDEO TUTORIAL

Step 4: Creating Virtual Smart Home Devices

For interfacing NodeMCU and Amazon Alexa, we will be using an Amazon skill called Sincric

  • First of all, you need to create an account in Sinric. Sinric is a website which allows linking your development boards like RaspberryPi, ESP8226, ESP32 or Arduino with Amazon Alexa.
  • Once the account is created login into it. Then click on the Smart Home DeviceAdd button to create a new smart home device
  • Create as many devices which you want to control. In my case, I have created 4 Smart Home Devices.
  • For this project, we will be using NodeMCU wifi module. So for programming the board using Arduino IDE you need to download some additional boards. I have written a separate article showing how to do it

Step 5: Configuring the Virtual Device


  • Download the example Sinric code from Github by kakopappa
  • Download and install WebSocketsClient.h and ArduinoJson.h library
  • Now open switch_example.ino example from the downloaded file
    Do the following changes in the code.

#define MyApiKey ""
  • Enter your unique API key from the Sinric website, In my case, it was something like this db8d0309-84ce-485b-8957-xxxxxxxxxxxx
#define MySSID ""
  • Enter your wifi network name here (case sensitive), your Amazon echo device should be in this same network.
<p>#define MyWifiPassword ""</p>
  • Enter your wifi network password (case sensitive).
void turnOn(String deviceId)
{<br>  if(deviceId == "xxxxxxxxxx") // Device ID of device 1
  {<br>    Serial.print("Turn on device id: ");<br>    Serial.println(deviceId);
    digitalWrite(device1, HIGH);<br>  }
  • In the If condition enter your device 1 ID from Sinric website. In my case, it was 5b13a012870d4c3a139be009
  • Do the same for all the devices in both turnOn and turnOn function
  • Once everything is done. Select the correct board and upload the sketch.
  • The NodeMCU will automatically connect to the wifi network
  • .Make sure your wifi is turned ON and your Echo device is connected to same.

Step 6: CONFIGURING SMARTHOME IN ALEXA APP

  • Open the Alexa App in your smartphone
  • Go to the Skills & Games section and search for Sinric skill and enable it
  • Now search for devices or just ask say "Alexa, discover devices"It will show the number of available devices (In my case four).
  • You can Rename the device name to whatever you want like fan, light ...

Step 7: ELECTRONICS

The circuit is very simple, but be careful while connecting AC load to relay.

Always turn OFF the mains before opening any switchboard.



Now Almost everything is done. All you need is connect the AC loads to relay and sit back and enjoy the automation. Feel your self as Tony Stark ; )


Feel free to ask any queries.

Subscribe to blog for more DIY projects

http://igniteinnovateideas.wordpress.com

Be the First to Share

    Recommendations

    • Finish It Already Speed Challenge

      Finish It Already Speed Challenge
    • Arduino Contest 2020

      Arduino Contest 2020
    • First Time Author Contest

      First Time Author Contest

    11 Discussions

    0
    luca.mihai.viorel
    luca.mihai.viorel

    4 weeks ago

    I am trzing to do it for ESP32 with no succes can u help me please
    [code]
    /*
    Version 0.4 - April 26 2019
    */

    #include
    #include
    #include
    #include // https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries
    #include // https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries (use the correct version)
    #include

    int Auto_0 = 16;
    int Auto_1 = 17;
    int Auto_2 = 18;
    int Auto_3 = 19;

    WiFiMulti WiFiMulti;
    WebSocketsClient webSocket;
    WiFiClient client;

    #define MyApiKey " " // TODO: Change to your sinric API Key. Your API Key is displayed on sinric.com dashboard
    #define MySSID " " // TODO: Change to your Wifi network SSID
    #define MyWifiPassword " " // TODO: Change to your Wifi network password

    #define HEARTBEAT_INTERVAL 300000 // 5 Minutes

    uint64_t heartbeatTimestamp = 0;
    bool isConnected = false;


    // deviceId is the ID assgined to your smart-home-device in sinric.com dashboard. Copy it from dashboard and paste it here

    void turnOn(String deviceId) {
    if (deviceId == " ") // Device ID of 1 device
    {
    Serial.print("Turn on device id: ");
    Serial.println(deviceId);
    digitalWrite (Auto_0, HIGH);

    }
    else if (deviceId == " ") // Device ID of 2 device
    {
    Serial.print("Turn on device id: ");
    Serial.println(deviceId);
    digitalWrite (Auto_1, HIGH);
    }
    else if (deviceId == " ") // Device ID of 3 device
    {
    Serial.print("Turn on device id: ");
    Serial.println(deviceId);
    digitalWrite (Auto_2, HIGH);
    }
    else if (deviceId == " ") // Device ID of 4 device
    {
    Serial.print("Turn on device id: ");
    Serial.println(deviceId);
    digitalWrite (Auto_3, HIGH);
    }
    else {
    Serial.print("Turn on for unknown device id: ");
    Serial.println(deviceId);
    }
    }

    void turnOff(String deviceId) {
    if (deviceId == " ") // Device ID of 1 device
    {
    Serial.print("Turn off Device ID: ");
    Serial.println(deviceId);
    digitalWrite (Auto_0, LOW);
    }
    else if (deviceId == " ") // Device ID of 2 device
    {
    Serial.print("Turn off Device ID: ");
    Serial.println(deviceId);
    digitalWrite (Auto_1, LOW);
    }
    else if (deviceId == " ") // Device ID of 3 device
    {
    Serial.print("Turn off Device ID: ");
    Serial.println(deviceId);
    digitalWrite (Auto_2, LOW);
    }
    else if (deviceId == " ") // Device ID of 4 device
    {
    Serial.print("Turn off Device ID: ");
    Serial.println(deviceId);
    digitalWrite (Auto_3, LOW);
    }
    else {
    Serial.print("Turn off for unknown device id: ");
    Serial.println(deviceId);
    }
    }

    void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
    switch(type) {
    case WStype_DISCONNECTED:
    isConnected = false;
    Serial.printf("[WSc] Webservice disconnected from sinric.com!\n");
    break;
    case WStype_CONNECTED: {
    isConnected = true;
    Serial.printf("[WSc] Service connected to sinric.com at url: %s\n", payload);
    Serial.printf("Waiting for commands from sinric.com ...\n");
    }
    break;
    case WStype_TEXT: {
    Serial.printf("[WSc] get text: %s\n", payload);
    // Example payloads

    // For Switch or Light device types
    // {"deviceId": xxxx, "action": "setPowerState", value: "ON"} // https://developer.amazon.com/docs/device-apis/alexa-powercontroller.html

    // For Light device type
    // Look at the light example in github
    #if ARDUINOJSON_VERSION_MAJOR == 5
    DynamicJsonBuffer jsonBuffer;
    JsonObject& json = jsonBuffer.parseObject((char*)payload);
    #endif
    #if ARDUINOJSON_VERSION_MAJOR == 6
    DynamicJsonDocument json(1024);
    deserializeJson(json, (char*) payload);
    #endif
    String deviceId = json ["deviceId"];
    String action = json ["action"];

    if(action == "setPowerState") { // Switch or Light
    String value = json ["value"];
    if(value == "ON") {
    turnOn(deviceId);
    } else {
    turnOff(deviceId);
    }
    }
    else if (action == "SetTargetTemperature") {
    String deviceId = json ["deviceId"];
    String action = json ["action"];
    String value = json ["value"];
    }
    else if (action == "test") {
    Serial.println("[WSc] received test command from sinric.com");
    }
    }
    break;
    case WStype_BIN:
    Serial.printf("[WSc] get binary length: %u\n", length);
    break;
    }
    }

    void setup() {
    Serial.begin(115200);
    pinMode (Auto_0, OUTPUT);
    pinMode (Auto_1, OUTPUT);
    pinMode (Auto_2, OUTPUT);
    pinMode (Auto_3, OUTPUT);

    WiFiMulti.addAP(MySSID, MyWifiPassword);
    Serial.println();
    Serial.print("Connecting to Wifi: ");
    Serial.println(MySSID);

    // Waiting for Wifi connect
    while(WiFiMulti.run() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    }
    if(WiFiMulti.run() == WL_CONNECTED) {
    Serial.println("");
    Serial.print("WiFi connected. ");
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
    }

    // server address, port and URL
    webSocket.begin("iot.sinric.com", 80, "/");

    // event handler
    webSocket.onEvent(webSocketEvent);
    webSocket.setAuthorization("apikey", MyApiKey);

    // try again every 5000ms if connection has failed
    webSocket.setReconnectInterval(5000); // If you see 'class WebSocketsClient' has no member named 'setReconnectInterval' error update arduinoWebSockets
    }

    void loop() {
    webSocket.loop();

    if(isConnected) {
    uint64_t now = millis();

    // Send heartbeat in order to avoid disconnections during ISP resetting IPs over night. Thanks @MacSass
    if((now - heartbeatTimestamp) > HEARTBEAT_INTERVAL) {
    heartbeatTimestamp = now;
    webSocket.sendTXT("H");
    }
    }
    }

    // If you want a push button: https://github.com/kakopappa/sinric/blob/master/arduino_examples/switch_with_push_button.ino

    [/code]

    0
    Jigneshk5
    Jigneshk5

    Reply 16 days ago

    What is the error?

    0
    joseismaelcastroruiz
    joseismaelcastroruiz

    3 months ago

    SIGAN LOS PASOS EXACTAMENTE Y LES VA A FUNCIONAR, DE PREFERENCIA CON ECHO DOT 3RA, GENERACION, PERO CREO QUE FUNCIONA CON CUALQUIER VERSION.
    SALUDOS AMIGOS!!

    0
    DilanM6
    DilanM6

    Question 1 year ago on Step 6

    Hey , this project i made , but i cant
    Configur my nodemcu with alexa android app , why?, this is only work amazon ecodot or other ?

    0
    joseismaelcastroruiz
    joseismaelcastroruiz

    Answer 3 months ago

    SI AMIGO, YO LO HICE CON ECHO DOT 3RA. GENERACION Y FUNCIONA PERFECTO. AGREGALE MQTT PARA CONTROLAR CON UNA APLICACION PROPIA Y SERA MAS PROFESIONAL.

    2
    demorgansucks
    demorgansucks

    10 months ago on Step 4

    Hi I'm confused with Step 5. Could you provide the link to all the codes?

    0
    joseismaelcastroruiz
    joseismaelcastroruiz

    Reply 3 months ago

    Descargue el código Sinric de ejemplo de Github por kakopappa

    AHI VIENE TODO AMIGO!!

    0
    Shankar005
    Shankar005

    Question 1 year ago on Step 6

    How to control ac circuits using alexa by two way switches as well as relay?I want control in both..plz help

    0
    joseismaelcastroruiz
    joseismaelcastroruiz

    Answer 3 months ago

    CREO QUE VA A ESTAR DIFICIL PARA TI SI NO SABES ALGO DE ELECTRONICA Y/O ELECTRICIDAD, INVESTIGA SOBRE EL CIRCUITO DE ESCALERA, CON ESO LO VAS A RESOLVER.

    0
    joseismaelcastroruiz
    joseismaelcastroruiz

    3 months ago

    ME FUNCIONO PERFECTAMENTE A LA PRIMERA, Y POR CIERTO LE AGREGUE A LA PROGRAMACION EL PROTOCOLO MQTT Y AHORA PUEDO ACCEDER A MIS DISPOSITIVOS Y CONTROLARLOS DESDE CUALQUIER PARTE Y CUANDO ESTOY EN MI CASA LOS CONTROLO CON ECHO DOT DE ALEXA

    0
    bhat2002.pratham
    bhat2002.pratham

    6 months ago

    Alexa is not able to find the device. Help!!