This project does the same work as the project https://forum.phpoc.com/blogs/khanh-...ect-with-phpoc. But there is only one difference, the one project uses MQTT and the other uses HTTP.
It’s not difficult but quite long. I try describing clearly step by step.
Demonstration
Things Used In This Project
- 1 x Amazon Alexa Echo Dot
- 1 x PHPoC Blue
- 1 x PHPoC 4-Port Relay Expansion Board (T-type) http://www.phpoc.com/smart_expansion_board.php
- 1 x Light Bulb
The big picture of this article
- I. System Architecture
- System diagram
- Functionalities of each part and interaction among the system parts.
- Which parts in system are changeable. If possible, which are the alternative
- II. Amazon Echo Dot – PHPoC
- Step 1: Setting up an Amazon Echo or Echo Dot
- Step 2: Creating and configuring Alexa Skill (on Alexa Service)
- Step 3: Writing source code to handle requests Sent by Alexa (on AWS Lambda)
- Step 4: Writing source code on PHPoC to handle commands and control the devices
I. System Architecture
1. System diagram
In the article https://forum.phpoc.com/blogs/khanh-...ect-with-phpoc, I explained how I designed the system architecture using “Custom Skill” of Alexa Service. In that article, I used MQTT protocol. In this article, I am going to use HTTP protocol instead of MQTT. By using HTTP protocol, system architecture is simpler since it does not require any Device Cloud. However, it will be less simple to manage if we want to control a lot of IoT devices.
2. Functionalities of each part and interaction among the system parts
- Amazon echo:
- (1) - getting your voice command, stream it to Alexa service.
- (5a) - receiving response from Alexa Service.
- Alexa service: this is where you create the Alexa Skill.
- (2) - It converts you voice to text, process it, create “intent” data and send it to your service. You need to provide the endpoint of your service (URL or Amazon Resource Names (ARNs)) when you create the skill.
- (4a) - receiving response from your service and send it to Amazon echo
- Your service: (3) this is where you write your code to handle command from Alexa. Your code will perform two main tasks: send necessary command to IoT Device via HTTP and send the response to Alexa service.
- IoT device: get command and take action (e.g. turn on/off light bulb) according to the command.
3. Which parts in system can be changed. If possible, which are the alternative
- Amazon Echo: you can use Amazon Echo, Amazon Tap, Echo Dot or Echo Look.
- Your service: Amazon provides two ways to build your custom skill:
- You can build a web service for your skill and host it with any cloud provider. (This is a hard work)
- You can host your service in AWS Lambda (This is a simpler work)
- IoT device: Any kind of IoT hardware platform which has a web server such as PHPoC
II. Amazon Echo Dot – PHPoC
I am going to show a very simple example of voice-controlling on/off a light bulb. Another example which is quite complicated will be present in the next article.
As mentioned, Amazon echo, the service, device cloud and IoT device are changeable parts. In this project, I use:
- Amazon Echo: Amazon Echo Dot.
- The service: AWS Lambda.
- IoT device: PHPoC and a light bulb. I use PHPoC for this project because PHPoC is an IoT hardware platform which has a webserver and a variant of PHP interpreter. From PHP script, we can interact with any kind of sensors or actuators.
As I described before, the system includes four parts. The following is five steps to do on four parts, respectively.
Pre-require
You need to register Amazon accounts:
- Amazon developer account at https://developer.amazon.com/
- AWS Account at https://aws.amazon.com/
Step 1: Setting up an Amazon Echo Dot
Refer to https://www.amazon.com/gp/help/custo...deId=201994280
Step 2: Creating and configuring Alexa Skill (on Alexa Service)
- Visit https://developer.amazon.com/home.html and sign in
- Navigate to “Alexa” tab, Click “Alexa Skills Kit”
- Click “Add a New Skill” button, fill some information as below, click “save” and then “next” button.
- In “Skill Information”:
Note that: invocation name should be written with space “P H P o C” for correct the recognition. And you will interact with Amazon Echo Dot with structure:- Alexa, tell P H P o C turn on/off the light
- Alexa, tell P H P o C turn the light on/off
You can change vocation name to “robot” and can say: Alexa, tell robot turn on/off the light. - In “Interaction Model”:
- Intent Schema
Code:{ "intents": [ { "slots": [ { "name": "LightState", "type": "LIGHT_STATE" } ], "intent": "ControlLightBulb" } ] }
- Custom Slot Types
- Enter Type: LIGHT_STATE
- Enter Value: on and off
- => Click “Add Slot Type” button
For "LightState" and "ControlLightBulb", you can search them on index.js to see their meaning. - Sample Utterances
Code:ControlLightBulb Turn {LightState} the light ControlLightBulb Turn the light {LightState} ControlLightBulb {LightState} the light ControlLightBulb the light {LightState}
- Intent Schema
- Click “Next”
- Do step 3 to get AWS Lambda ARN and then come back here, put ARN as below image
- Click “Next” button.
If you finished step 3, you can test this skill by using service simulator
Step 3: Writing source code to handle data from Alexa Skill (on AWS Lambda)
We need to create a lambda function and write source code for it. This function is fired when there is an incoming request from Alexa. The function will:
- Process the request
- Make a HTTP request to PHPoC and get response
- Send the response back to Alexa
Create a Lambda function:
- Go to https://aws.amazon.com/ , Click “Sign in to The Console” button at the top-left.
- Search “Lambda” on the search bar and click on “Lambda” result
- Click “Create a Lambda function” button
- Select runtime Node.js 6.10
- You will see a template function “alexa-skills-kit-color-expert”, click download icon to get the sample code. We will modify and compare this code later. Then, close download window and click “Blank Function”. (Amazon may change the template code overtime).
- Choose “Allexa Skill Kit” and click “next”
- Configure function:
- Name: any name, for example: myLightBulb
- Runtime: Node.js 6.10
- Handler: index.handler (by default)
- Role: Firstly, select “Create a custom role”, it will redirect to create a custom role. (refer to Appendix 1 to create a role). Secondly select “choose an existing role”.
- Existing role: choose the custom role you have just created.
- Click “next” button
- Click “Create Function” it will redirect to function management page. You will upload code at this page. Please pay attention to the upper right corner of the ARN string, this is the endpoint of this Lambda function which we will put it in Alexa Skill configuration in step 2.
You can code in Node.js (JavaScript), Java, Python, or C#. In this project, I code in Node.js - Write main code (index.js)
we downloaded “alexa-skills-kit-color-expert” sample code. Unzip this code and see the index.js file. I modified it to control the light bulb (see index.js in source code section). You can compare my index.js code and index.js code from template to see the difference (I recommend to use the WinMerge tool for compare two file http://winmerge.org/ ). When you compare two file, it’s easier to understand the source code and how it works.
<index.js>
Code:'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 http = require('http'); // --------------- 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 httpPromise = new Promise( function(resolve,reject){ http.get({ host: 'your_ip_or_domainame', path: '/index.php?state=' + lightState, port: '80' }, function(response) { // Continuously update stream with data var body = ''; response.on('data', function(d) { body += d; }); response.on('end', function() { // Data reception is done, do whatever with it! console.log(body); resolve('Done Sending'); }); }); }); httpPromise.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.se ssion.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); } };
Note that: you need to change IP address or domain name of your PHPoC board in this source code. In case you use the private IP address, you need to set port forwarding on your router or access point. - Since this source code does not use any the external library, we can paste this code directly to online editor.
Step 4: Writing source code on PHPoC to handle commands and control the devices (task0.php)
In this code, we just need to write a PHP script to handle HTTP request. When receiving a HTTP request, this code turn on or off the light bulb according to value in HTTP request and send response to AWS Lambda.
<index.php>
PHP Code:
<?php
include_once "/lib/sd_spc.php";
$state = _GET("state");
//spc_reset();
spc_sync_baud(115200);
if($state == "on")
spc_request(14, 4, "set 0 output high");
else
spc_request(14, 4, "set 0 output low");
echo $state;
?>
Appendix 1: Creating a Role
However, I would be very grateful to you if you would help me with a complete list of compatible products as I have to convince my parents of the purchase.
Thanks
There are a lot of devices that can works with Amazon Alexa. It's difficult to find the a complete list of Alexa-enabled products. You can summarize it from various sources. There is a good source http://smarthome.reviewed.com/features/everything-that-works-with-amazon-echo-alexa .
And also you can make any kind of devices that you can imagine. It's possible that someday you can produce and sell it in over the world. If you have any difficulty in realizing your idea, feel free to ask here.