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


Things Used In This Project
  • 1 x Amazon Alexa Echo Dot
  • 1 x PHPoC Blue
  • 1 x PHPoC Grove Expansion Board (Optional)
  • 1 x Grove - Temperature&Humidity Sensor

The big picture of this article
  • 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 Architecture

You can refer to this link to see how I designed the system architecture

Click image for larger version  Name:	1.png Views:	1 Size:	105.4 KB ID:	661

“Why uses PHPoC for this project?”. The answer is PHPoC is an IoT hardware platform which has a webserver 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 Sensor

As I described before, the system includes four parts. The following is five steps to do on four parts, respectively.


You need to register Amazon accounts:

Step 1: Setting up an Amazon Echo Dot

Refer to

Step 2: Creating and configuring Alexa Skill (on Alexa Service)
  • Visit and sign in
  • Navigate to “Alexa” tab, Click “Alexa Skills Kit”

    Click image for larger version  Name:	2.png Views:	1 Size:	150.8 KB ID:	662
  • Click “Add a New Skill” button, fill some information as below, click “save” and then “next” button.
  • In “Skill Information”:

    Click image for larger version  Name:	3.png Views:	1 Size:	110.3 KB ID:	663

    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 image for larger version  Name:	4.png Views:	1 Size:	187.5 KB ID:	664
  • Click “Next” button.
    If you finished step 3, you can test this skill by using service simulator
    Click image for larger version  Name:	5.png Views:	1 Size:	97.2 KB ID:	665

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 , 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

    Click image for larger version  Name:	6.png Views:	1 Size:	37.0 KB ID:	666
  • Select runtime Node.js 6.10

    Click image for larger version  Name:	7.png Views:	3 Size:	22.2 KB ID:	667
  • 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).

    Click image for larger version  Name:	8.png Views:	1 Size:	170.9 KB ID:	668
  • Choose “Allexa Skill Kit” and click “next”

    Click image for larger version  Name:	9.png Views:	2 Size:	34.3 KB ID:	669
  • 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 (index.js)
    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. 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 ). When you compare two file, it’s easier to understand the source code and how it works.


    	'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
    	* For additional samples, visit the Alexa Skills Kit Getting Started guide at
    	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',
    	      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;
    	      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 createTemperatureAttributes(temperature) {
    	  return {
    	      temperature: temperature
    	* Read temperature in the session and prepares the speech to reply to the user.
    	function readTemperatureInSession(intent, session, callback) {
    	  const cardTitle =;
    	  let repromptText = '';
    	  let sessionAttributes = {};
    	  const shouldEndSession = true;
    	  let speechOutput = '';
    	var body = '';
    	var httpPromise = new Promise( function(resolve,reject){
    	 host: '',
    	 path: '/temp_humi.php',
    	 port: '1470'
    	}, function(response) {
    	 // Continuously update stream with data
    	 response.on('data', function(d) {
    	  body += d;
    	 response.on('end', function() {
    	  // Data reception is done, do whatever with it!
    	  resolve('Done Sending');
    	function(data) {
    	 var info = JSON.parse(body);
    	 console.log('Function called succesfully:', data);
    	 sessionAttributes = createTemperatureAttributes(info.temperature);
    	 speechOutput = "Temperature is " + info.temperature + " degree Celsius. Humidity is " + info.humidity + " percent";
    	 repromptText = "Temperature is " + info.temperature + " degree Celsius. Humidity is " + info.humidity + " percent";
    	 callback(sessionAttributes,buildSpeechletResponse(  cardTitle, speechOutput, repromptText, shouldEndSession));
    	function(err) {
    	 console.log('An error occurred:', err);
    	// --------------- 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.
    	* 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 =;
    	  // Dispatch to your skill's intent handlers
    	  if (intentName === 'TemperatureHumidity') {
    	      readTemperatureInSession(intent, session, callback);
    	  } else if (intentName === 'AMAZON.HelpIntent') {
    	  } else if (intentName === 'AMAZON.StopIntent' || intentName === 'AMAZON.CancelIntent') {
    	  } 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=${  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 !== '[unique-value-here]') {
    	 "Invalid Application ID");
    	      if ( {
    	          onSessionStarted({ requestId: event.request.requestId }, event.session);
    	      if (event.request.type === 'LaunchRequest') {
    	              function callback(sessionAttributes, speechletResponse) {
    	   context.succeed(buildResponse(sessionAttributes, speechletResponse));
    	      } else if (event.request.type === 'IntentRequest') {
    	              function callback(sessionAttributes, speechletResponse) {
    	   context.succeed(buildResponse(sessionAttributes, speechletResponse));
    	      } else if (event.request.type === 'SessionEndedRequest') {
    	          onSessionEnded(event.request, event.session);
    	  } catch (e) {
 "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.
    You also can compare with the source code of turning on/off light bulb here Amazon Echo - Control IoT devices via HTTP
  • Since this source code does not use any the external library, we can paste this code directly to online editor.

    Click image for larger version  Name:	10.png Views:	1 Size:	167.1 KB ID:	670

Step 4: Source Code on PHPoC to Handle HTTP request from Lambda Function.

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 in this project


PHP Code:
include_once "/lib/vd_th02.php";

$temp TH02_read_temperature();            
$humi TH02_read_humidity();
'{"temperature":"' "$temp'","humidity":"' "$humi'"}';

Appendix 1: Creating a Role

Click image for larger version  Name:	11.png Views:	3 Size:	48.1 KB ID:	671