I. System Architecture
II. Amazon Echo Dot – PHPoC - Temperature and Humidity Sensor
- 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 HTTP request from Lambda Function, read temperature and humidity, and send back to Lambda function via HTTP response.
Now, let’s go into detail.
I. System ArchitectureYou can refer to this link https://www.hackster.io/44558/amazon-echo-control-iot-devices-via-http-154590 to see how I designed the system architecture
“Why uses PHPoC for this project?”. The answer is PHPoC is an IoT hardware platform which has a web server and a variant of PHP interpreter. From PHP script, we can read value from any kind of sensor and send the value in HTTP response.
II. Amazon Echo Dot – PHPoC - Temperature and Humidity SensorAs 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/customer/display.html?nodeId=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”:
You will interact with Amazon Echo Dot with structure: “Alexa, my weather, tell me current temperature and humidity”
- In “Interaction Model”:
- Intent Schema
{
"intents": [
{
"intent": "TemperatureHumidity"
}
]
}
- Custom Slot Types: since command from use does not contain any option parameters, we do not need to add custom slot types
- Sample Utterances
TemperatureHumidity tell me temperature and humidity
TemperatureHumidity tell me current temperature and humidity
- 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 which contain temperature and humidity.
- 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: readTemperatureHumidity
- 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 (see index.js in the code section)
we downloaded ““alexa-skills-kit-color-expert”” sample code. Unzip this code and see the index.js file. I modified it to read temperature and humidity (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.
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.
You also can compare with the source turning on/off light bulb here https://www.hackster.io/44558/amazon-echo-control-iot-devices-via-http-154590
- 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 HTTP request from Lambda Function (see index.php in the code section).
In this code, we just need to write a PHP script to handle HTTP request. When receiving a HTTP request, this code read temperature and humidity from sensor and send them to AWS Lambda function via HTTP response.
In this project, I used TH02 temperature and humidity sensor. The library for this sensor is available here http://www.phpoc.com/forum/viewtopic.php?f=42&t=185&p=215
Appendix 1: creating a role
Comments
Please log in or sign up to comment.