Project tutorial
smart home using Alexa

smart home using Alexa © GPL3+

Making automation to the next level. Alexa an Artificial intelligence will help the user make pretty simple home automation. let's see.

  • 1,406 views
  • 0 comments
  • 8 respects

Components and supplies

Apps and online services

About this project

Amazon Alexa is pretty cool artificial intelligence that helps user to create their own skill. Amazon provides Alexa voice service(AVS) to help developers to make install Alexa on their own devices. It will helps user to make it simple as much. Those people not having echo device or dot device and use this way to access Alexa. we have Alexa on our Raspberry pi3 board using amazon voice service(AVS). Let's make it...we made simple automation ie., controlling an LED light over voice. No worry about making our raspberry pi as Alexa device all details are given below.

we made simple automation ie., controlling some home appliances(LED's for prototype) over voice using Alexa. you can replace those LED's with relays and real appliances. No worry about making our raspberry pi as Alexa device all details are given below. So do it with full of enjoyment.

This is our skill that have been published.

Here is our skill published link: https://alexa.amazon.in/spa/index.html#skills/beta/amzn1.ask.skill.429aeb0d-5bfb-4b8e-9d07-52799ae75778

Our Skill ID: amzn1.ask.skill.429aeb0d-5bfb-4b8e-9d07-52799ae75778

DEMO VIDEO:

Demonstration of smart home using Alexa

Hope you guys seen our demo video and get inspired on it. Then why waiting full detailed instruction are provided here and let's start building our own.

STEP 1: Make your Raspberry pi as Alexa Client

Here is the video on making your raspberry pi as alexa device client. This video will help in clear to make it.

STEP 2: Configure Alexa on raspberry pi

you can download and install alexa app from the github repository.

Alexa Github link: https://github.com/alexa/alexa-avs-sample-app

then create an account on amazon developer console. link is here: https://developer.amazon.com/

Log in to developer console and the click on to Alexa Voice service. Follow the video in step 1, after downloading the alexa app from the above Guihub link. After that make sure that you have noted down your own app ID, client ID, client secret. Because it wil be needed at the time of running.

It is not such complicated one to run alexa its pretty simple. Just take few seconds. Just run the below command on your pi device.

$ cd Downloads/alexa-avs-sample-app-master/. automated_install.sh

provide the information what it ask like app ID such things it will automatically configure with your account.

STEP 3: Time to wake Alexa

$ cd Downloads/alexa-avs-sample-app-master/samples
#three tabs will be open#
#In Tab 1#
$ cd companionService/npm start
#In tab 2#
$ cd javaclient
$ mvn exec:exec
#In tab 3#
$ cd wakewordAgent/src/
$ ./wakewordAgent -e sensory

that's it now alexa is ready to use.!

Alexa will be wake up by saying a wake work "alexa" and then ask whatever you want to ask. Still sticky in the codes. Detailed process is shown in the step 1 video have a look you will get a clear view.

STEP 4: Creating new skill

1. Log in to amazon developer account. Not having account just sign up few steps.

developer account link: https://developer.amazon.com/home.html

2. click on tab Alexa and choose Alexa skill kit.

3. click add a new skill it take to skill page.

  • select skill type as custom interaction model
  • choose language and then provide names. keep rest as it is

4. move to interaction model

  • just copy and paste our intent provided below and the sample utterances and then click save and move to configure model.
  • choose HTTPS on configuration. we have used heroku as https client. It is easy in use and deployment. Link is provided below.

heroku link: https://www.heroku.com/

STEP 5: Creating a Heroku client

  • Log in to heroku account. if not having account just create it with few easier steps. link is above.
  • click on create new app and choose the programming language convenient to you. we have choose python language.
  • In deployment method, connect you heroku account with your github repository.

Github link: https://github.com/

if anyone feel messy in reading with the above wording and pictures can go with video on Heroku account creating and deployment: https://youtu.be/qZtsPJa2yzQ

STEP 6: Connect Github with heroku

  • Now log in to github account and then create a new repository.
  • And then upload the files to your new repository by clicking upload files inside the repository. Files are provided below you can directly upload to your repository.
  • save changes and the copy the URL of your selected repository.
  • then come back to heroku and the connect your github repository.
  • click on connect to github and then paste the copied URL of your github repository.

if sticky in getting the points in above steps just go for video. Each and every step is clearly given. here is the video link on Connecting Github repository with Heroku client: https://youtu.be/_YtZTXd_Cio

STEP 7: Connecting NodeMCU to Heroku

  • Log in to heroku and the go to your created app.
  • then go to setting -> Domains and certification and then copy the domain URL
  • come back to skill configuration choose HTTPS and the paste the copied URL in the below box.

Detailed process are shown in the video have a look on it: https://youtu.be/om5vBcZjDrY

STEP 8: Run Arduino code on NodeMCU

Do connection of LED's as shown in schematic below.

Final step, arduino code is provided with this. It need some libraries for the execution no worry. Libraries also provided in our Github repository just simply go with it.

All set just execute the Arduino code in NodeMCU board and our project is ready.

cool... our Alexa device is ready to assist us.

Code

intent schemaJSON
{
  "intents": [
    {
      "intent": "AMAZON.CancelIntent"
    },
    {
      "intent": "AMAZON.HelpIntent"
    },
    {
      "intent": "AMAZON.StopIntent"
    },
    {
      "slots": [
        {
          "name": "instance",
          "type": "list_instance"
        },
        {
          "name": "state",
          "type": "list_state"
        },
        {
          "name": "question",
          "type": "list_question"
        }
      ],
      "intent": "OprateGarageDoor"
    }
  ]
}
slotsPlain text
Type:

list_instance
Values:

bed 
kitchen
hall
bath
all

Type:

list_question

Values:

is 
was
what

Type:

list_state

Values:

on
off
sample utterancesPlain text
OprateGarageDoor turn {state} {instance} room light
OprateGarageDoor turn {instance} room light {state}
OprateGarageDoor {question} {instance} room light {state}
OprateGarageDoor {state} {instance} room light
Arduino codeArduino
Upload this code to your NodeMCU board execute the program.
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <WebSocketsClient.h>
#include <Hash.h>


const char* ssid     = "xxxxxxx"; //enter your ssid/ wi-fi(case sensitiv) router name - 2.4 Ghz only
const char* password = "xxxxxxxx";     // enter ssid password (case sensitiv)
char host[] = "alexatestpi.herokuapp.com"; //enter your Heroku domain name like "espiot.herokuapp.com" 
 // enter the distance in inches between you ultrasonic sensor and garage door

int duration, distance;
const int relayPin1 = 16;
const int relayPin2 = 5; 
const int relayPin3 = 4;
const int relayPin4 = 14;
int pingCount = 0;
int port = 80;
char path[] = "/ws"; 
ESP8266WiFiMulti WiFiMulti;
WebSocketsClient webSocket;

DynamicJsonBuffer jsonBuffer;
String currState, oldState, message;
void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) { 


    switch(type) {
        case WStype_DISCONNECTED:
            //USE_SERIAL.printf("[WSc] Disconnected!\n");
           Serial.println("Disconnected! ");
            break;
            
        case WStype_CONNECTED:
            {
             Serial.println("Connected! ");
          // send message to server when Connected
            webSocket.sendTXT("Connected");
            }
            break;
            
        case WStype_TEXT:
            Serial.println("Got data");
            //Serial.println("looping...");
            
            
            
            
            processWebScoketRequest((char*)payload);

            break;
            
        case WStype_BIN:

            hexdump(payload, length);
            Serial.print("Got bin");
            // send data to server
            // webSocket.sendBIN(payload, length);
            break;
    }

}

void setup() {
    Serial.begin(115200);
    Serial.setDebugOutput(true);
    
    pinMode(relayPin1, OUTPUT);
    pinMode(relayPin2, OUTPUT);
    pinMode(relayPin3, OUTPUT);
    pinMode(relayPin4, OUTPUT);

    
      for(uint8_t t = 4; t > 0; t--) {
          delay(1000);
      }
    Serial.println();
    Serial.println();
    Serial.print("Connecting to ");
    
    //Serial.println(ssid);
    WiFiMulti.addAP(ssid, password);

    //WiFi.disconnect();
    while(WiFiMulti.run() != WL_CONNECTED) {
      Serial.print(".");
        delay(1000);
    }
    Serial.println("Connected to wi-fi");
    webSocket.begin(host, port, path);
    webSocket.onEvent(webSocketEvent);

}

void loop() {

    webSocket.loop();
        delay(100);
  // make sure after every 40 seconds send a ping to Heroku
  //so it does not terminate the websocket connection
  //This is to keep the conncetion alive between ESP and Heroku
  if (pingCount > 20) {
    pingCount = 0;
    webSocket.sendTXT("\"heartbeat\":\"keepalive\"");
  }
  else {
    pingCount += 1;
  }
}


void processWebScoketRequest(String data){
            String jsonResponse = "{\"version\": \"1.0\",\"sessionAttributes\": {},\"response\": {\"outputSpeech\": {\"type\": \"PlainText\",\"text\": \"<text>\"},\"shouldEndSession\": true}}";
            JsonObject& req = jsonBuffer.parseObject(data);

            String instance = req["instance"];
            String state = req["state"];
            String query = req["query"];
            String message = "{\"event\": \"OK\"}";
            
            Serial.println("Data2-->"+data);
            Serial.println("State-->" + state);

            if(query == "?"){ //if command then execute
              Serial.println("Recieved query!");
                 if(currState=="open"){
                      message = "On";
                    }else{
                      message = "Off";
                    }
                   jsonResponse.replace("<text>", "Bed room light " + instance + " is " + message );
                   webSocket.sendTXT(jsonResponse);
                   
            }else if(query == "cmd"){ //if query check state
              Serial.println("Recieved command!");
            if(instance == "bed"){
                    if(state == "on"){
                            digitalWrite(relayPin1, HIGH);
                            message = "turning   on";
                            Serial.println(instance);
                          }else if(state == "off"){
                            message = "turning   off";
                            digitalWrite(relayPin1, LOW);
                            Serial.println(instance);
                        }
                        
            } 
           
            if(instance == "hall"){
              if(state == "on"){
                            digitalWrite(relayPin2, HIGH);
                            message = "turning   on";
                            Serial.println(instance);
                          }else if(state == "off"){
                            message = "turning   off";
                            digitalWrite(relayPin2, LOW);
                            Serial.println(instance);
                        }
            }
           
            if(instance == "kitchen"){
              if(state == "on"){
                            digitalWrite(relayPin3, HIGH);
                            message = "turning   on";
                            Serial.println(instance);
                          }else if(state == "off"){
                            message = "turning   off";
                            digitalWrite(relayPin3, LOW);
                            Serial.println(instance);
                        }
            }
         
            if(instance == "bath"){
              if(state == "on"){
                            digitalWrite(relayPin4, HIGH);
                            message = "turning   on";
                            Serial.println(instance);
                          }else if(state == "off"){
                            message = "turning   off";
                            digitalWrite(relayPin4, LOW);
                            Serial.println(instance);
                        }
            }
            if(instance == "all"){
              if(state == "on"){
                            digitalWrite(relayPin1, HIGH);
                            digitalWrite(relayPin2, HIGH);
                            digitalWrite(relayPin3, HIGH);
                            digitalWrite(relayPin4, HIGH);
                            message = "turning   on";
                            Serial.println(instance);
                          }else if(state == "off"){
                            message = "turning   off";
                            digitalWrite(relayPin4, LOW);
                            digitalWrite(relayPin4, LOW);
                            digitalWrite(relayPin4, LOW); 
                            digitalWrite(relayPin4, LOW);
                              
                            Serial.println(instance);
                        }
            }
            
                  jsonResponse.replace("<text>",  instance + " room light   is now " + message );
                  webSocket.sendTXT(jsonResponse);
                  }

            
            else{//can not recognized the command
                    Serial.println("Command is not recognized!");
                   jsonResponse.replace("<text>", "Command is not recognized by room light in Alexa skill");
                   webSocket.sendTXT(jsonResponse);
            }
            Serial.print("Sending response back");
            Serial.println(jsonResponse);
                  // send message to server
                  webSocket.sendTXT(jsonResponse);
}
Alexa codes
All the codes and libraries are having in the Github repository so no worry just proceed as said in the procedure.

Schematics

NodeMCU connection
simply make a connection of LED's as in schematic provided
alexa_node_EoRLt5EyUM.fzz

Comments

Similar projects you might like

Arduino Bluetooth Basic Tutorial

by Mayoogh Girish

  • 455,599 views
  • 42 comments
  • 240 respects

Home Automation Using Raspberry Pi 2 And Windows 10 IoT

Project tutorial by Anurag S. Vasanwala

  • 286,084 views
  • 95 comments
  • 672 respects

Security Access Using RFID Reader

by Aritro Mukherjee

  • 230,252 views
  • 38 comments
  • 239 respects

OpenCat

Project in progress by Team Petoi

  • 197,586 views
  • 154 comments
  • 1,373 respects
Add projectSign up / Login