Using Amazon Echo (Alexa) as an Interface for the IoT

The Amazon Echo device has just been made available for pre-orders in the US. For those not familiar with it, Echo is a device that interacts with users verbally and can “provide information, answer questions, play music, read the news, check sports scores or the weather, and more..”.

While its primary purpose it to assist (or promote) interaction with Amazon services (music, orders, etc.) it can also be used as an interface for controlling IoT devices. By default it can control your Philips Hue lights and Belkin Wemo switches. Probably integration with more home-automation devices is on the way.

However, since there is a SDK available for developer’s it’s quite possible to use Alexa (the voice and ‘AI’ of Echo) to develop your own apps that communicate with your devices. There are quite a few examples online that show people using Alexa to control for example some SmartThings devices.

I started experimenting with the SDK recently and I am sharing here an example on how to use Alexa to communicate with a Particle device (ex Spark core).

Objective: Use Alexa to ask about sensor information collected from a Particle / ask Alexa to deliver an actuation command on a Particle (e.g., turn off a switch)

concept

How developing for Amazon Echo works: Though it’s been referred to as Echo SDK, it’s more of an API, since your application needs to be hosted on the Internet and accessed through HTTPS as a web service. Your app is registered through the Amazon Developer portal as an Echo app where you indicate the app’s name, the Spoken Name (the name user’s will use to launch your app by saying e.g., ‘Alexa open -app name-‘), the URL of your application endpoint (HTTPS or Amazon Lambda endpoint) and the Interaction Model. The Interaction model basically indicates how your app interacts with Alexa: – the Schema that contains Intents (think of them as tasks for Alexa) and the input parameters your users will say to Alexa during app execution and – the Sample Utterances, which contain phrases a user might say that correspond to the various Intents (e.g., turn off the switch, turn the switch off, set temperature to {high|temperature}, set temperature to {low|temperature}, etc.)

Here are the steps to create an Amazon Echo app (update: Looks like Amazon Echo apps are now called Alexa Skills) that communicates with a Particle device:

a) First you need to create the endpoint of your app. It can be hosted under your custom server environment (but remember you need to enable HTTPS – even a self-signed certificate can do for testing your app) or you can create an Amazon Lambda function and host it under AWS. Currently you can create Echo apps using either Java or Node.JS. For simplicity I chose to create the endpoint using an Amazon Lambda function.

Choosing ‘Alexa Appkit  Colors (Preview)’ from Code template gives you with a code template to work on that features already the main functions Alexa needs to interact with your app. Remember to select ‘Basic Execution Role’ under Role options.

Screen Shot 2015-06-25 at 2.34.24 PM

Before editing your Lambda function and adding your code, remember to add Event Source: Click the Actions button -> ‘Add event source’ and from the ‘Event Source type’ select ‘Alexa AppKit (Preview). This step is crucial, without it your Echo device won’t be able to execute your app.

Your Amazon Lambda function is ready to be used and has its own endpoint, for example:

arn:aws:lambda:us-east-1:51XYXYXYXY50:function:AmazonEchoParticle

Click on your function’s name to Edit it. Have a look through the code template. What happens here is that when user invokes your app by saying ‘Alexa open appname’, Echo executes the ‘onLaunch’ function. When user asks something from Alexa or gives a command, that corresponds to an Intent. Based on phrases that a user might say (and that you have predefined when registering your Echo app – step b), the functio 

function onIntent(intentRequest, session, callback) 

is invoked with the requested Intent detected as first input parameter.

To have Alexa say something to the user you use the function: 

function buildSpeechletResponse(title, output, repromptText, shouldEndSession) 

where you also specify whether the session (app) should be ended, or Alexa should wait for some new input from the user.

In the case of the Particle device, I have created two Intents for use: one for asking the temperature from an attached temperature analog sensor, and one for controlling a digital pin.

The communication with the Particle is done through the Particle Cloud. The Particle Cloud exposes an API you can use for controlling or reading data from your device anywhere. You can use it invoke functions on the Particle or read a variable name (e.g., the temperature calculated in Celcius from an analog temperature sensor) or you can directly read data from and set the state of (digital) pins.

You can find the complete code for the Echo Lambda function here.

b) Register your Amazon Echo to the Amazon Developer’s portal. Under APPS & SERVICES -> Alexa, you add a new App (now called Skill). Give it a name, and an invocation name. I used ‘particle’ for this example. Add the endpoint (Lambda ID) and click Next to define the Intent Schema and the Sample Utterances.

Since the app has two Intents (‘AskTempIntent’ for getting the temperature and ‘ControlPinIntent’ for setting the digital pin to 0/1) you need an Internet Schema like the following:


{
"intents": [
{
"intent": "ControlPinIntent",
"slots": [
{
"name": "Pin",
"type": "LITERAL"
}
]
},
{
"intent": "AskTempIntent",
"slots": []
}
]
}

Note that the ControlPinIntent has an input slot (variable) named ‘Pin’.

For the Sample Utterances you need to define possible phrases the user will use to interact with Alexa for this app:


ControlPinIntent set pin to {zero|Pin}
ControlPinIntent set pin to {one|Pin}
ControlPinIntent set the pin to {zero|Pin}
ControlPinIntent set the pin to {one|Pin}
ControlPinIntent put the pin to {zero|Pin}
ControlPinIntent put the pin to {one|Pin}
AskTempIntent what is the temperature
AskTempIntent tell me the temperature
AskTempIntent temperature
AskTempIntent temperature sensor

c) Last but not least, upload the code to your Particle that will expose the temperature reading though the Cloud API and also let you control a digital pin remotely. Here you need two things mainly, a) register inside setup() the temperature variable so it can be exposed through the API:

Spark.variable(“temperature”, &temperature, INT);

b) register the function that controls the digital pin over the API:

Spark.function(“relay”, pinControl);

and the code of the pinControl function looks like:

int pinControl(String command{

// Get state

int state = command.toInt();

digitalWrite(relay_pin,state);

return 1;

}

You will need your DeviceID and your Access Token in order to be able to access the Particle Cloud API.

Once having setup everything and flashed your Particle, you should be able to use the app on Alexa and interact with the Particle: Invoke the app by saying ‘Alex open particle‘. After the welcome message, you can ask: ‘Alexa, what is the temperature?‘ or ‘Alexa, set pin to 1‘ to see the blue LED on the Particle light up 🙂

 

Here is a video demonstrating the project, by telling Alexa to turn on and off (set pin to 1 and then to 0) the on-board LED of the particle device

5 thoughts on “Using Amazon Echo (Alexa) as an Interface for the IoT

  1. Excellent article! I am trying to do the same by connecting my Netatmo weatherstation to a Lambda function and then Alexa to that lambda. Unfortunately, still having some problems on the Ajax async request…

  2. Hi – I noticed you open particle, but then are able to ask Alexa particle-specific utterances. Are you able to do that indefinitely after opening the particle? Or does it time out / work for just a few utterances?

    I’m looking to enable home automation without continually using the phrase, “Alexa, ask particle to …. “

    • Hi,

      As fas I as know, you always need to interact with a skill asking Alexa to do so (“Alexa, open particle”, etc..), and once you open it there is a timeout indeed. Maybe there is an option to set the timeout, but I am sure there is a limit.

    • Hi,

      The interaction is done via the particle cloud over https…the cloud then communicates with the particle device for changing the status of the pin.

Leave a Reply

Your email address will not be published. Required fields are marked *

*