Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Suresh
Published © Apache-2.0

Smart Alexa

SmartAlexa can connect with Amazon Alexa devices as a Smart Home Skill to give you command of your home through voice controls.

AdvancedProtip1,614
Smart Alexa

Things used in this project

Story

Read more

Code

Lambda Function

JavaScript
var awsIot = require('aws-iot-device-sdk');
var deviceName = "voice-chair";   //thing name from AWS IoT
var host = "a3jra11pv5kiyg.iot.us-east-1.amazonaws.com"; //host address
var app_id = "amzn1.ask.skill.2b38790d-905a-4005-ad73-908f63e1622f" //app id from Alexa skill
/*
 thing shadow requires the private key, certificate and rooCA file you 
 downloaded at the time of certificate creation of AWS IoT
*/

var thingShadows = awsIot.thingShadow({
   keyPath: './private.pem.key',
  certPath: './certificate.pem.crt',
    caPath: './rootCA.pem',
  clientId: deviceName,
    region: "us-east-1",
});

var ctx = null;

// Route the incoming request based on type (LaunchRequest, IntentRequest, etc.) The JSON body of the request is provided in the event parameter.
exports.handler = function (event, context) {
    try {
        console.log("event.session.application.applicationId=" + event.session.application.applicationId);
        ctx = context;

        if (event.session.application.applicationId !== app_id) {
             ctx.fail("Invalid Application ID");
         }
 
        thingShadows.on('connect', function() {
			thingShadows.register('voice-chair');
 
		});
		
		thingShadows.on('message', function(topic, payload) {
            console.log('message', topic, payload.toString());
        });
     
        if (event.session.new) {
            onSessionStarted({requestId: event.request.requestId}, event.session);
        }
        if (event.request.type === "LaunchRequest") {
            onLaunch(event.request, event.session);
        }  else if (event.request.type === "IntentRequest") {
            onIntent(event.request, event.session);
        } else if (event.request.type === "SessionEndedRequest") {
            onSessionEnded(event.request, event.session);
            ctx.succeed();
        }
    } catch (e) {
        console.log("EXCEPTION in handler:  " + e);
        ctx.fail("Exception: " + e);
    }
};

/**
 * Called when the session starts.
 */
function onSessionStarted(sessionStartedRequest, session) {
    console.log("onSessionStarted requestId=" + sessionStartedRequest.requestId + ", sessionId=" + session.sessionId);
}


/**
 * Called when the user launches the skill without specifying what they want.
 */
function onLaunch(launchRequest, session, callback) {
    console.log("onLaunch requestId=" + launchRequest.requestId + ", sessionId=" + session.sessionId);

    // Dispatch to your skill's launch.
    getWelcomeResponse(callback);
}

/**
 * Called when the user specifies an intent for this skill.
 */
function onIntent(intentRequest, session ) {                  //, callback) {
    console.log("onIntent requestId=" + intentRequest.requestId + ", sessionId=" + session.sessionId);

    var intent = intentRequest.intent,
    intentName = intentRequest.intent.name;

    console.log("REQUEST to string =" + JSON.stringify(intentRequest));

    var callback = null;
    // Dispatch to your skill's intent handlers
    if ("BedroomLightOn" === intentName) {
		thingShadows.publish('taifur/test/pi/voice', 'Bedlighton', function(){
			var cardTitle = "Bedroom Lamp on";
			var repromptText = "";
			var sessionAttributes = {};
			var shouldEndSession = false;
			var speechOutput = "Your bedroom light is on. ";
			repromptText = "Your bedroom light is on. ";
			var shouldEndSession = false;
			ctx.succeed(buildResponse(sessionAttributes, buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession)));
		});
	
    }else if ("BedroomLightOff" === intentName) {
		thingShadows.publish('taifur/test/pi/voice', 'Bedlightoff', function(){
			var cardTitle = "Lamp on";
			var repromptText = "";
			var sessionAttributes = {};
			var shouldEndSession = false;
			var speechOutput = "Your bedroom light is off. ";
			repromptText = "Your bedroom light is off. ";
			var shouldEndSession = false;
			ctx.succeed(buildResponse(sessionAttributes, buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession)));
		});
	
    }else if ("KitchenLightOn" === intentName) {
		thingShadows.publish('taifur/test/pi/voice', 'Kitchenlighton', function(){
			var cardTitle = "Lamp on";
			var repromptText = "";
			var sessionAttributes = {};
			var shouldEndSession = false;
			var speechOutput = "Your kitchen light is on. ";
			repromptText = "Your kitchen light is on. ";
			var shouldEndSession = false;
			ctx.succeed(buildResponse(sessionAttributes, buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession)));
		});
	
    }else if ("KitchenLightOff" === intentName) {
		thingShadows.publish('taifur/test/pi/voice', 'Kitchenlightoff', function(){
			var cardTitle = "Lamp on";
			var repromptText = "";
			var sessionAttributes = {};
			var shouldEndSession = false;
			var speechOutput = "Your kitchen light is off. ";
			repromptText = "Your kitchen light is off. ";
			var shouldEndSession = false;
			ctx.succeed(buildResponse(sessionAttributes, buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession)));
		});
	
    }else if ("BathroomLightOn" === intentName) {
		thingShadows.publish('taifur/test/pi/voice', 'Bathroomlighton', function(){
			var cardTitle = "Lamp on";
			var repromptText = "";
			var sessionAttributes = {};
			var shouldEndSession = false;
			var speechOutput = "Your bathroom light is on. ";
			repromptText = "Your bathroom light is on. ";
			var shouldEndSession = false;
			ctx.succeed(buildResponse(sessionAttributes, buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession)));
		});
	
    }else if ("BathroomLightOff" === intentName) {
        thingShadows.publish('taifur/test/pi/voice', 'Bathroomlightoff', function(){
			var cardTitle = "Lamp off";
			var repromptText = "";
			var sessionAttributes = {};
			var shouldEndSession = false;
			var speechOutput = "Your bathroom light is off. ";
			repromptText = "Your bathroom light is off. ";
			var shouldEndSession = false;
			ctx.succeed(buildResponse(sessionAttributes, buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession)));
		});
		
    }else if ("BedroomFanOn" === intentName) {
		thingShadows.publish('taifur/test/pi/voice', 'Bedroomfanon', function(){
			var cardTitle = "Lamp on";
			var repromptText = "";
			var sessionAttributes = {};
			var shouldEndSession = false;
			var speechOutput = "Your bedroom fan is on. ";
			repromptText = "Your bedroom fan is on. ";
			var shouldEndSession = false;
			ctx.succeed(buildResponse(sessionAttributes, buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession)));
		});
	
    }else if ("BedroomFanOff" === intentName) {
        thingShadows.publish('taifur/test/pi/voice', 'Bedroomfanoff', function(){
			var cardTitle = "Lamp off";
			var repromptText = "";
			var sessionAttributes = {};
			var shouldEndSession = false;
			var speechOutput = "Your bedroom fan is off. ";
			repromptText = "Your bedroom fan is off. ";
			var shouldEndSession = false;
			ctx.succeed(buildResponse(sessionAttributes, buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession)));
		});
		
    }else if ("IncreaseSpeed" === intentName) {
		  var speedLevelSlotInc = intent.slots.Speed;
		  var speedLevelInc = speedLevelSlotInc.value;
	      thingShadows.publish('taifur/test/pi/voice', 'Inc' +speedLevelInc, function(){
		    var cardTitle = "Fan Speed";
		    var repromptText = "";
		    var speechOutput = "Fan speed is adjusted to " + speedLevelInc + " percent. ";
			var sessionAttributes = {};
			var shouldEndSession = false;
			ctx.succeed(buildResponse(sessionAttributes, buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession)));			
	    });
	
	}else if ("DecreaseSpeed" === intentName) {
          var speedLevelSlot = intent.slots.Speed;
		  var speedLevel = speedLevelSlot.value;
	      thingShadows.publish('taifur/test/pi/voice', 'Dec' +speedLevel, function(){
		    var cardTitle = "Fan Speed";
		    var repromptText = "";
		    var speechOutput = "Fan speed is adjusted to " + speedLevel + " percent. ";
			var sessionAttributes = {};
			var shouldEndSession = false;
			ctx.succeed(buildResponse(sessionAttributes, buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession)));			
	    });
		
    }else if ("AMAZON.HelpIntent" === intentName) {
        getHelp(callback);
    }else if ("AMAZON.StopIntent" === intentName || "AMAZON.CancelIntent" === intentName) {
        handleSessionEndRequest(callback);
    }else {
        throw "Invalid intent";
    }

}

/**
 * Called when the user ends the session.
 * Is not called when the skill returns shouldEndSession=true.
 */
function onSessionEnded(sessionEndedRequest, session) {
    console.log("onSessionEnded requestId=" + sessionEndedRequest.requestId + ", sessionId=" + session.sessionId);
    // Add cleanup logic here
}

// --------------- Functions that control the skill's behavior -----------------------

function getWelcomeResponse() {
    // If we wanted to initialize the session to have some attributes we could add those here.
    var sessionAttributes = {};
    var cardTitle = "Welcome";
    var speechOutput = "Welcome to Pi Home Master . I can control your electrical appliances. Tell me what can I do for you. ";

    var repromptText = "I am ready for command.";
    var shouldEndSession = false;

    ctx.succeed(buildResponse(sessionAttributes, buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession)));

}

function getHelp() {
	var sessionAttributes = {};
    var cardTitle = "Help";
    var speechOutput = "Welcome to pi home master, I can control your light and fan. " + 
    "You can ask me by saying, turn on my bedroom light or turn off my bedroom fan.";
    var repromptText = "Would you like to control your light or fan?";
    var shouldEndSession = false;

    ctx.succeed(buildResponse(sessionAttributes, buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession)));
}

function handleSessionEndRequest() {
	var sessionAttributes = {};
    var cardTitle = "Session Ended";
    var speechOutput = "Thank you for using pi home master, Have a nice day!";
    var shouldEndSession = true;
	var repromptText = "";
    ctx.succeed(buildResponse(sessionAttributes, buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession)));
}


// --------------- Helpers that build all of the responses -----------------------

function buildSpeechletResponse(title, output, repromptText, shouldEndSession) {
    return {
        outputSpeech: {
            type: "PlainText",
            text: output
        },
        card: {
            type: "Simple",
            title: title,
            content: output
        },
        reprompt: {
            outputSpeech: {
                type: "PlainText",
                text: repromptText
            }
        },
        shouldEndSession: shouldEndSession
    }
}

function buildResponse(sessionAttributes, speechletResponse) {
    return {
        version: "1.0",
        sessionAttributes: sessionAttributes,
        response: speechletResponse
    }
}

Rassberry Pi code

C/C++
import paho.mqtt.client as mqtt
import ssl
import json
import RPi.GPIO as GPIO
import time

# make sure python gpio library is installed
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
kitchenLightPin = 23 # Broadcom pin 23 (P1 header pin 16)
bedLightPin = 10     # Broadcom pin 17 (P1 header pin 19)
bedFanPin = 11       # Broadcom pin 27 (P1 header pin 23)
bathLightPin = 22    # Broadcom pin 22 (P1 header pin 15)
GPIO.setup(kitchenLightPin, GPIO.OUT) # pin set as output
GPIO.setup(bedLightPin, GPIO.OUT)     # pin set as output
GPIO.setup(bedFanPin, GPIO.OUT)       # pin set as output
GPIO.setup(bathLightPin, GPIO.OUT)    # pin set as output
GPIO.output(kitchenLightPin, GPIO.LOW)
GPIO.output(bedLightPin, GPIO.LOW)
GPIO.output(bedFanPin, GPIO.LOW)
GPIO.output(bathLightPin, GPIO.LOW)

client = mqtt.Client()

def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))
    client.subscribe("taifur/test/pi/voice")

def on_message(client, userdata, msg):
    
     if str(msg.payload) == 'Kitchenlighton':
        print("Kitchen light on command received ")
        print(str(msg.payload))
        GPIO.output(kitchenLightPin, GPIO.HIGH)  # gpio pin high
        
     if str(msg.payload) == 'Kitchenlightoff':
        print("Kitchen light off command received ")
        print(str(msg.payload))
        GPIO.output(kitchenLightPin, GPIO.LOW)

     if str(msg.payload) == 'Bedlighton':
        print("Bedroom light on command received ")
        print(str(msg.payload))
        GPIO.output(bedLightPin, GPIO.HIGH)  # gpio pin high
        
     if str(msg.payload) == 'Bedlightoff':
        print("Bedroom light off command received ")
        print(str(msg.payload))
        GPIO.output(bedLightPin, GPIO.LOW)

     if str(msg.payload) == 'Bedroomfanon':
        print("Bedroom fan on command received ")
        print(str(msg.payload))
        GPIO.output(bedFanPin, GPIO.HIGH)  # gpio pin high
        
     if str(msg.payload) == 'Bedroomfanoff':
        print("Bedroom fan off command received ")
        print(str(msg.payload))
        GPIO.output(bedFanPin, GPIO.LOW)

     if str(msg.payload) == 'Bathroomlighton':
        print("Bathroom light on command received ")
        print(str(msg.payload))
        GPIO.output(bathLightPin, GPIO.HIGH)  # gpio pin high
        
     if str(msg.payload) == 'Bathroomlightoff':
        print("Bathroom light off command received ")
        print(str(msg.payload))
        GPIO.output(bathLightPin, GPIO.LOW)
       
        

client.on_connect = on_connect
client.on_message = on_message

client.tls_set(ca_certs='rootCA.pem', certfile='certificate.pem.crt', keyfile='private.pem.key', cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_SSLv23, ciphers=None)
client.connect("a3jra11pv5kiyg.iot.us-east-1.amazonaws.com", 8883, keepalive=60)
client.loop_forever()

Intent Schema

JSON
{
  "intents": [
    {
      "intent": "BedroomLightOn"
    },
    {
      "intent": "BedroomLightOff"
    },
    {
      "intent": "KitchenLightOn"
    },
    {
      "intent": "KitchenLightOff"
    },
    {
      "intent": "BathroomLightOn"
    },
    {
      "intent": "BathroomLightOff"
    },
    {
      "intent": "BedroomFanOn"
    },
    {
      "intent": "BedroomFanOff"
    },
    {
      "intent": "AMAZON.HelpIntent"
    },
    {
      "intent": "AMAZON.StopIntent"
    },
    {
      "intent": "AMAZON.CancelIntent"
    }
  ]
 }
CREDITS

Sample Utterence

Plain text
BedroomFanOn switch on the bedroom fan
BedroomFanOn switch on my bedroom fan
BedroomFanOn switch on my bedroom fan
BedroomFanOn switch on bedroom fan
BedroomFanOn switch on bedroom fa

Arduio LED

C/C++
int led = 13;

void setup() {
  // put your setup code here, to run once:
  pinMode(led, OUTPUT);
    Serial.begin(9600);

  
}

void loop() {
  if (Serial.read() == 'N') {
     digitalWrite(led, HIGH);
  }else if(Serial.read() == 'F') {
     digitalWrite(led, LOW);
  }

  
 }

Credits

Suresh

Suresh

18 projects • 20 followers
IoT Developer

Comments