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
  • Backendless SDK for Alexa, which provides functionality greatly reducing the amount of work you need to do when working with Alexa requests.
  • 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. Make sure to install the Backendless SDK for Alexa.
  3. 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
  4. Switch to the CODELESS section:
    codeless tab - Developing a custom Alexa skill to control an IoT device
  5. Click the Add New link under API Services in the BROWSER panel. In the popup that shows, enter AlexaLightsService  and click SAVE.
  6. 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
  7. 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.
    alexa lightbulb codeless - Developing a custom Alexa skill to control an IoT device
  8.  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
  9. 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
  10. 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 select Alexa and create a new skill.
  2. Use magic light for the invocation name.
  3. Select JSON Editor in the menu:
    json editor alexa 615x1024 - Developing a custom Alexa skill to control an IoT device
  4. Paste the following schema definition into the editor area on the page:
  5. Click the Save Model button and then Build Model.
  6. Click the Endpoint menu and select the HTTPS option.
  7. Paste the Backendless API service endpoint into the Default Region inout field. For the “Select SSL certificate type” dropdown select “My development endpoint is a subdomain of a domain that has a wildcard certificate from a certificate authority”. Click the Save Endpoints button.
  8. 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+