Message:

Subscribe rss
Blog categories
All | Backendless features
Developing a custom Alexa skill to control an IoT device
January 2, 2018 by markpiller

If you have any smart IoT devices in your home or office and tried controlling them with Alexa, you might wonder how it actually works. In this guide you will learn about building a custom Alexa skill which will let you control a wi-fi enabled light bulb with Alexa. You will be able to turn the light on/off and change the light colors. You can see a demo of the completed project as well as an overview of its components in the video below:

The solution consists of the following components:

  • Amazon Alexa-enabled device (Amazon Echo)
  • A custom Alexa skill, which is an API service running in Backendless
  • An API service which contains the logic of controlling the light bulb
  • A wi-fi enabled light bulb, for this we used a LIFX bulb.

The diagram below illustrates all the components and how they relate to one another:

alexa to lifx with backendless - Developing a custom Alexa skill to control an IoT device

It is important to note that this solution does not require you to use AWS Lambda, it can be implemented entirely in Backendless. Even with the the free plan in Backendless, you can build and run a custom Alexa skill.

The process of developing this solution makes more sense to do in the reversed order, that is:

  1. Start with the LIFX API Service
  2. Implement the Alexa skill
  3. Register the skill with Amazon

The reason for that order is the plain inter-component dependency.

LIFX API Service

This is the code containing the logic for communicating with the light bulb. The code is published into Backendless, which automatically turns it into an API service. The service APIs provide the functionality for controlling the light bulb, such as turning it on or off and changing the light color. For detailed instructions for how to develop, test and deploy the service, see the following blog post:

Developing and API Service with JavaScript and additional NPM modules.

Implement Custom Alexa Skill

A custom skill consists of two parts: the code which handles requests sent by Alexa and the skill registration with Amazon. In this step you will focus on the implementation of the skill. A skill implementation in Backendless must be an API service. It can be done with any server-side language supported by our platform, which includes JavaScript, Java or Codeless. In this guide we will use Codeless, and we plan to update it with code listing for Java/JavaScript.

  1. If you do not have an account in Backendless, register and create an app in the Backendless Console.
  2. Click the Business Logic icon in the vertical column of icons on the left side of the interface:
    bizlogin icon - Developing a custom Alexa skill to control an IoT device
  3. Switch to the CODELESS section:
    codeless tab - Developing a custom Alexa skill to control an IoT device
  4. You will see the BROWSER panel on the right side of the interface. This is where you will be creating a new service for the skill as well as a few custom functions, which will be used by the service. In fact, you need to start with the custom functions first. Click the Add New link located under the Functions node. The main canvas area gets a placeholder for a function. Make sure to change the name and add arguments as shown below:
    send alexa response - Developing a custom Alexa skill to control an IoT deviceThe function name must be: sendAlexaResponse . Function argument names are whatToSay  and waitForResponse .
  5. Create the logic for the function body as shown below:
    sendAlexaResponse function - Developing a custom Alexa skill to control an IoT device
    Do not forget to click the SAVE button when you are done editing the function’s logic.
  6. Repeat the process for another function:
    Function name: getIntentName .
    Function argument: request .
    Function body:
    getintentname function - Developing a custom Alexa skill to control an IoT device
    Click the SAVE button when you are done editing the function’s logic.
  7. Finally, the last function to add is:
    Function name: getSlotValue .
    Function arguments: request , intentName , slotName .
    getslotvalue declaration - Developing a custom Alexa skill to control an IoT device
    Function body:
    getslotvalue logic - Developing a custom Alexa skill to control an IoT device
    Click the SAVE button when you are done editing the function’s logic.
  8. Now that the functions are ready, we can create the actual API service for the Alexa skill. Click the Add New link under API Services in the BROWSER panel. In the popup that shows, enter AlexaLightsService  and click SAVE.
  9. You will be prompted to create a method for the new service. Enter the information as shown below and click SAVE:
    backendless api service step2 - Developing a custom Alexa skill to control an IoT device
  10. Codeless creates a placeholder for the service method, which you can edit by adding method’s logic. The complete listing of the method’s logic is below. Notice that some blocks are the custom functions implemented above as well as the LifxService service methods – these methods became available in Codeless when the LIFX API Service was deployed in an earlier step.
    codeless lights service - Developing a custom Alexa skill to control an IoT device
  11.  Click the DEPLOY MODEL button when you are done editing the Codeless logic. This will deploy the service and make it available for invocations. The service can be tested directly from the Backendless Console. To do this click the API SERVICES tab as shown below:
    api services tab - Developing a custom Alexa skill to control an IoT device
  12. Make sure the AlexaLightsService is selected. The Backendless Console will let you invoke the service right from its user interface. To invoke the handleRequest method, you need to know the format of the request message. The request is in a special format as Alexa creates it. You can see a sample request below. Copy/paste the text below into the the req field and click the INVOKE button:

    If the invocation is successful, you should see the following:alexa request response - Developing a custom Alexa skill to control an IoT device
  13. Finally, click the show full URL button in the green bar which says POST /handleRequest. It will display the full URL of the API service operation. The URL will be used in the next section where you will register the custom Alexa skill.

Registering a Custom Alexa Skill with Amazon

The final part in creating this solution is registering the skill you “coded” in the previous section with Amazon. This will make the skill available on your Alexa-enabled devices.

  1. Login to Amazon Developer Portal and click the Get Started > button for the Alexa Skills Kit:
    alexa skills kit - Developing a custom Alexa skill to control an IoT device
  2. Click the Add a New Skill button.
  3. Give your skill a name and an invocation name. The importance of the latter is this is how you will be activating your skill when talking to Alexa. Here’s a configuration of the skill from the video above:
    magic light skill step1 - Developing a custom Alexa skill to control an IoT device
  4. Click the Save button, then Next. The next step is one of the most important, as it includes information about skill’s intents and slots. Enter the following text into the Intent Schema field:
  5. Configure the Custom Slot Types as shown below:
    custom slot types - Developing a custom Alexa skill to control an IoT device
  6. Finally, add the following text into the Sample Utterances field:

    Click Save, then Next.
  7. On the Configuration step you will point the skill to the Codeless API service developed in the previous section. You will need to use the URL copied in step 13 above.Click HTTPS in the Endpoint section and paste the API Service endpoint URL into the Default field as shown below:
    alexa skill configuration - Developing a custom Alexa skill to control an IoT device
    Click Save, then Next.
  8. Make the selection as shown below on the SSL Certification step:
    alexa skill ssl cert - Developing a custom Alexa skill to control an IoT device
    Click Save, then Next.
  9. At this point the skill is ready and you can test it either using the Test section in the Amazon Developer Portal or using an Alexa-enabled device.

Enjoy!

Share this post
Tweet about this on TwitterShare on FacebookGoogle+