Things used in this project

Hardware components:
Amazon echo dot
Amazon Alexa Echo Dot
×1
Phpoc blue per 100x1000 euk22wtv9i
PHPoC Blue
×1
Relay board s type htqfasikzh
PHPoC 4-Port Relay Expansion Board (S-type)
This board contain relay to turn on/off a the high-voltage light bulb
×1
Light Bulb
×1
Software apps and online services:
Dp image kit 02
Amazon Alexa Alexa Skills Kit
Screen%20shot%202015 07 20%20at%206.10.26%20pm
Amazon Web Services AWS Lambda
MQTT

Code

Lambda function (index.js)JavaScript
This code is fired when Alexa Service request. The code process the incoming data (intent), get command (on/off), send the commands to MQTT broker, and the send response to Alexa Service
'use strict';

/**
 * This sample demonstrates a simple skill built with the Amazon Alexa Skills Kit.
 * The Intent Schema, Custom Slots, and Sample Utterances for this skill, as well as
 * testing instructions are located at http://amzn.to/1LzFrj6
 *
 * For additional samples, visit the Alexa Skills Kit Getting Started guide at
 * http://amzn.to/1LGWsLG
 */

var mqtt = require('mqtt');

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

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

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


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

function getWelcomeResponse(callback) {
    // If we wanted to initialize the session to have some attributes we could add those here.
    const sessionAttributes = {};
    const cardTitle = 'Welcome';
    const speechOutput = "Welcome to P H P o C. How can I help you?"
    // If the user either does not reply to the welcome message or says something that is not
    // understood, they will be prompted again with this text.
    const repromptText = "How can I help you?";
    const shouldEndSession = false;

    callback(sessionAttributes,
        buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
}

function handleSessionEndRequest(callback) {
    const cardTitle = 'Session Ended';
    const speechOutput = 'Thank you for trying the Alexa Skills Kit sample. Have a nice day!';
    // Setting this to true ends the session and exits the skill.
    const shouldEndSession = true;

    callback({}, buildSpeechletResponse(cardTitle, speechOutput, null, shouldEndSession));
}

function createLightBulbAttributes(lightBulb) {
    return {
        lightBulb: lightBulb
    };
}

/**
 * Control Light Bulb in the session and prepares the speech to reply to the user.
 */
function controlLightBulbInSession(intent, session, callback) {
    const cardTitle = intent.name;
    const lightStateRequest = intent.slots.LightState;
    let repromptText = '';
    let sessionAttributes = {};
    const shouldEndSession = true;
    let speechOutput = '';

    if (lightStateRequest) {
		var lightState = lightStateRequest.value;
		//Update 
		var mqttpromise = new Promise( function(resolve,reject){
			var client = mqtt.connect({port:1883,host:'iot.eclipse.org'})
			
			client.on('connect', function() { // When connected
				// publish a message to any mqtt topic
				client.publish('alexa/phpoc/lightbulb', lightState)
				client.end()
				resolve('Done Sending');
			});
			
		});
		mqttpromise.then(
			function(data) {
				console.log('Function called succesfully:', data);
				sessionAttributes = createLightBulbAttributes(lightState);
				speechOutput = "Ok, turning the light " + lightState;
				repromptText = "Ok, turning the light " + lightState;
				callback(sessionAttributes,buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
			},
			function(err) {
				console.log('An error occurred:', err);
			}
		);
		 
	} else {
		speechOutput = "Please try again";
		repromptText = "Please try again";
		callback(sessionAttributes,buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
	}
}

// --------------- Events -----------------------

/**
 * 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}");

    const intent = intentRequest.intent;
    const intentName = intentRequest.intent.name;

    // Dispatch to your skill's intent handlers
    if (intentName === 'ControlLightBulb') {
        controlLightBulbInSession(intent, session, callback);
    } else if (intentName === 'AMAZON.HelpIntent') {
        getWelcomeResponse(callback);
    } else if (intentName === 'AMAZON.StopIntent' || intentName === 'AMAZON.CancelIntent') {
        handleSessionEndRequest(callback);
    } else {
        throw new Error('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
}


// --------------- Main handler -----------------------

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

        /**
         * Uncomment this if statement and populate with your skill's application ID to
         * prevent someone else from configuring a skill that sends requests to this function.
         */
        /*
        if (event.session.application.applicationId !== 'amzn1.echo-sdk-ams.app.[unique-value-here]') {
             context.fail("Invalid Application ID");
        }
        */

        if (event.session.new) {
            onSessionStarted({ requestId: event.request.requestId }, event.session);
        }

        if (event.request.type === 'LaunchRequest') {
            onLaunch(event.request,
                event.session,
                function callback(sessionAttributes, speechletResponse) {
					context.succeed(buildResponse(sessionAttributes, speechletResponse));
				});
        } else if (event.request.type === 'IntentRequest') {
            onIntent(event.request,
                event.session,
                function callback(sessionAttributes, speechletResponse) {
					context.succeed(buildResponse(sessionAttributes, speechletResponse));
				});
        } else if (event.request.type === 'SessionEndedRequest') {
            onSessionEnded(event.request, event.session);
            context.succeed();
        }
    } catch (e) {
        context.fail("Exception: " + e);
    }
};
PHPoC code (task0.php)PHP
This code is run on PHPoC- a IoT hardware platform. It subscribes and MQTT topic and turn on/off the light when receive MQTT message
<?php

if(_SERVER("REQUEST_METHOD"))
    exit; // avoid php execution via http request

include_once "/lib/sd_spc.php";
include_once "/lib/sn_dns.php";
include_once "/lib/vn_mqtt.php";

spc_reset();
spc_sync_baud(115200);

//$host_name = "test.mosquitto.org";
$host_name = "iot.eclipse.org";
//$host_name = "broker.hivemq.com";
//$host_name = "broker.mqttdashboard.com";
//$host_name = "[192.168.0.3]";
$port = 1883;

mqtt_setup(0, "Alexa PHPoC 1",  $host_name, $port); 

$will = "";
$username = "";
$password = "";

mqtt_connect(true, $will, $username, $password);

$out_topics = array(array("alexa/phpoc/lightbulb", 1));

if(mqtt_state() == MQTT_CONNECTED)
    mqtt_subscribe($out_topics);

$in_topic = "";
$in_content = "";
$is_retain = 0;

while(1)
{    
    if(mqtt_state() == MQTT_DISCONNECTED)
        while(mqtt_reconnect() == false);
    
    if(mqtt_loop($in_topic, $in_content, $is_retain))
    {
		if($in_content === "on")
		{
			//turn on the light bulb
			echo "turn on the light bulb\r\n";
			spc_request(14, 4, "set 0 output high");
		}
		else if($in_content === "off")
		{
			//turn off the light bulb
			echo "turn off the light bulb\r\n";
			spc_request(14, 4, "set 0 output low");
		}
		else
		{
			echo "unknown command\r\n";
		}
    }
}
   
mqtt_disconnect();

?>

Credits

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Similar projects you might like

Making Music Using a Step Motor
Intermediate
  • 171
  • 8

Full instructions

This project show ho to turn step motors into musical instrument.

Amazon Echo – Control IoT devices via HTTP
Intermediate
  • 270
  • 11

Full instructions

I am going to show how to use Amazon Echo to control IoT devices via HTTP protocol. Turning on/off a light bulb is an example.

Amazon Echo – Read Temperature and Humidity from Sensor
Intermediate
  • 329
  • 12

Full instructions

I am going to show how to use Amazon Echo to read temperature and humidity from sensor.

Open your garage with Alexa and a Raspberry Pi
Intermediate
  • 26
  • 1

Protip

Run commands on your Raspberry Pi with Alexa and TRIGGERcmd. Example phrase: "Alexa, tell TRIGGER C M D to run door on garage"

Drone Powered 360º Circular Smartphone Camera Rig
Intermediate
  • 448
  • 3

This Arduino & XBEE connected camera rig shoots flat 360º degree round video/footage using only one smartphone camera/recording device.

Full Color Alexa Controlled Lights - FastLED & Photon
Intermediate
  • 519
  • 9

Full instructions

Full color light controlled by Alexa. This is a step by step using a Particle Photon and configurable Python Script in Lambda.

ProjectsCommunitiesTopicsContestsLiveAppsBetaFree StoreBlogAdd projectSign up / Login
Feedback