This is for all the muggles who never got the chance to learn magic.
Since the release of the Amazon Echo, I've envisioned Harry Potter-style home automation where lights and locks are controlled by magical spells rather than with standard commands.
To make this work, I created a custom Alexa skill that processes different incantations and translates them into what Amazon calls ‘intents.’ The skill then communicates the user’s intent to an instance of Node-RED running on the IBM Cloud, which handles most of the application logic and also controls the LIFX bulb through its native HTTP API.
Ideally, Alexa would wake up and respond to the user saying an incantation out of the blue, to make it feel like a true magical spell. Unfortunately, however, the Echo has to be triggered using one of a predetermined list of wake words (such as 'Alexa'), and the user must then provide the name of a skill to invoke. I chose to name the custom skill 'My Wand' so that the experience would still feel pretty authentic ("Alexa, use My Wand to cast Lumos").
Step 1: Set Up LIFX BulbYou’ll first need to install your LIFX bulb and connect it to WiFi. The easiest way to do this is by installing the LIFX app and following the prompts in the app to connect the device.
The LIFX Website also provides setup instructions if you need. Note: I did not pair the bulb with HomeKit.
When complete, you should be able to control the bulb from the app:
LIFX allows users to control their bulbs over the internet through the HTTP Remote Control API. HTTP commands are sent from the user to the LIFX Cloud Server, which can communicate with connected bulbs (we connected our bulb to the LIFX Cloud in Step 1). In order for the LIFX Cloud Server to access the bulbs in your account, we must generate a LIFX HTTP API Token and include it in our HTTP commands.
To obtain the token, log into the LIFX Cloud, then click your name in the top-right corner and select Personal Access Tokens. Follow the prompts to generate your Personal Access Token. You can also find instructions for this on the LIFX Community Website.
Once you’ve generated your token, make note of it and keep it somewhere safe. Someone with access to this token could control your LIFX bulb.
Step 3: Set Up Node-RED on IBM CloudWhen creating a custom Alexa skill, you must configure Alexa to send user requests over HTTPS to a web application that processes those requests, then returns a response. We will build that application in an instance of Node-RED hosted on the IBM Cloud server.
The IBM Developer site provides a great set of instructions you can use to set this up for free.
Once you've completed this step, launch Node-RED in your browser.
Step 4: Add Node LibrariesThe Node-RED flow we’ll uses requires nodes from these custom libraries:
- node-red-contrib-lifx-api
- node-red-node-random
To add them to your palette, click the Hamburger menu in the top-right and select Manage Palette. Under the Install tab, search for each library listed above and click the Install button.
Download the file Node-RED_Flow.json from my GitHub. In Node-RED, navigate to the Hamburger menu, then select Import to import the JSON flow.
You'll need to configure the six LIFX API nodes, shown highlighted in orange below:
Double-click each node and copy the Personal Access Token generated in Step 2 to the API Token field.
In the Selector field of each LIFX API node, identify which light(s) you would like the command to control. If your LIFX account is linked to a single bulb, you can enter all
into this field. Otherwise, use one of the selectors described in the Selectors page of the LIFX HTTP API Help Documentation.
Once the configuration is complete, click the Deploy button to save your changes and deploy them.
With the flow deployed, click the square icon on the TestLumos inject node to make sure the light turns on, then try turning it off using the TestNox inject node.
Use the Alexa phone or desktop app to set up the Amazon Echo and connect it to WiFi. The Amazon website provides instructions if you need.
If you would like Alexa t:o respond in a British accent when you cast spells, you'll have to change her locale in the settings. Because this is a general setting, it means that Alexa will keep using the accent with other skills, and some of your existing skills may no longer be supported if they were not developed to support the UK locale.
To update the locale, open your Alexa app or navigate to alexa.amazon.com. Go to Settings >> Device Settings and set the Language option to English (United Kingdom).
Step 7: Import and Configure the Custom Alexa SkillNow, we can start building our Alexa skill. Navigate to the Alexa Developer Console and log in. Click Create Skill to start a new skill.
Fill out the following options:
Skill Name: this can be anything you want; it won't affect the functionality of the skill
Default Language: if you chose to configure Alexa to use the British locale, select English (UK) here
Skill Model: Choose the Custom option
Backend Skill Method: Choose the Provision Your Own option
Template: Choose Start from Scratch
Now that you've created your skill, start customizing it by completing the four required checklist items:
InvocationName
This is the phrase that will trigger Alexa to use your custom skill. I used my wand as the invocation.
Intents, Samples, and Slots
Download the file alexa_skill.txt from my GitHub. In the menu on the left-hand side of the Developer Console, select JSON Editor and drag and drop the text file into the editor to load the intents and sample utterances. Each intent represents a command that Alexa will send to our Node-RED flow depending on what the user says.
BuildModel
Save and build the configured skill.
Endpoint
This is the location to which Alexa will post HTTP requests whenever a user invokes our custom skill. Under Service Endpoint Type, choose HTTPS. In the Default Region section, enter the URL of your cloud hosted Node-RED flow with /Alexa
appended to the end. For example:
https://<my-nodered-address>.mybluemix.net/alexa
Select the Wildcard Certificate option for the SSL Certificate Type.
Step 8: Test and Tune your SkillYou're now ready to test your skill! In the Developer Console, click the Test option in the main menu to use the Alexa Simulator. When you enter the command use my wand to cast lumos
, you should receive a response from Alexa and see your light turn on.
If this works, try using the skill directly with your Amazon Echo.
When you cast a spell, the Node-RED flow will receive the intent, manipulate the LIFX bulb, and then send a response back to Alexa. In each response, Alexa repeats the spell incantation and also plays a sound effect. Because the HTTP communication from Node-RED to Alexa and to the LIFX bulb is not deterministic, it can be difficult to get the sound effect to be reliably synchronized with the light effects. To improve the timing of the sound effect, test each spell and then tune the length of each purple Delay node in the flow. Don't forget to click the Deploy button after making any changes.
The project includes logic for the following spells:
Lumos: turns light on
Nox: turns light off if Lumos was cast
Expecto Patronum: brightening light to simulate patronus
Avada Kedavra: bright green flash
Incendio: red flickering light to simulate fire
Finite Incantatem: turns light off no matter which spell was cast
Wingardium Leviosa: does not affect lights; Alexa just makes fun of the user
The Alexa responses are provided using SSML, documented here on the Amazon Developer site.
The sound effects all come from the Alexa Skills Kit Sound Library
Comments