How to Create an Alexa-Enabled Smart Home with Particle Photon — Part 1

Aaron Coox
7 min readJan 27, 2019

Leverage the powerful Alexa API to build custom smart-home devices through this simple and cost-effective solution.

It’s no secret that smart home assistants and devices have become extremely popular in the last few years. Alexa is one of the most popular assistants, with over 100 million Alexa-enabled devices sold and over 25,000 supported skills.

Building a smart home device with Alexa creates instant compatibility with hundreds of other products

It’s possible to control almost every part of your home through internet-enabled devices, but it can be difficult finding a purpose for this. Alexa’s powerful routines allow users to trigger actions based not only on commands but also schedules and sensor triggers. This allows any user to easily automate their smart home without even requiring the purchase of a voice assistant!

Call C++ functions on a Photon by making an HTTP request, or trigger webhooks from a sensor reading with a single line of code

Alexa-enabled devices must be able to communicate and be controlled over the cloud. For a smart home, these devices must also be small, cheap, and low-power. Enter Particle.io. Their micro-controllers are designed with IOT in mind through seamless connection between their cloud API and physical devices. Call C++ functions on a Photon by making an HTTP request, or trigger webhooks from a sensor reading with a single line of code.

What I’ll Teach You

In this tutorial, I will be showing you how to:

  • Connect a Particle Photon to Alexa
  • Create a custom Alexa skill with Node.js and AWS Lambda
  • Use Particle’s cloud to host users and product deployments
  • Control your device through the Alexa app and trigger sensors with C/C++

Prerequisites

  • A Photon or other Particle micro-controller
  • Accounts for Alexa and Amazon AWS
  • Basic understanding of JavaScript and C/C++

Quick Links

Part 2: Click here

Part 3: Click here

Final Code: Click here

Step 1: Creating an Alexa Skill

To begin with, we’re going to create a skill that links to our Particle account.

  • Start by heading over to the Alexa developer console.
  • Log in (note that this account is separate from your AWS account).
  • Click “Create Skill”
  • Enter your name and language. Select the Smart Home model, and the Self Hosted method.
  • The language chosen here also affects the developer region of the skill. This tutorial will be in the Australian mode, but ensure you select the correct region.
  • Click “Create Skill”
  • From the next page, take note of your skill ID, but don’t fill out anything else just yet.

Step 2: Creating a Back-end

Alexa smart-home skills use predefined templates for what your device can do, and what requests/responses will be expected. We’ll specify an AWS Lambda handler to receive these requests and take the necessary actions.

  • To begin, log into the AWS management console.
  • It’s important that you select the correct AWS region to match your skill area. According to Alexa’s documentation, this is:

N.Virginia for English (US) or English (CA) skills

EU (Ireland) region for English (UK), English (IN), German or French (FR) skills

US West (Oregon) for Japanese and English (AU) skills.

  • Since I chose English (AU) for my skill, I will be using Oregon as my region.
  • If you haven’t used Lambda before, you’ll need to create an account with the correct permissions in IAM. If you have, you can skip these steps.
  • Navigate to the IAM service. Select “Roles” and click “Create Role”.
  • Select AWS service and Lambda. Click Next.
  • Choose AWSLambdaBasicExecutionRole. This will give your Lambda function all the generic permissions it needs to run.
  • Press Next on the tags page.
  • Give your role a name and description, then finalise it with Create Role.
  • We’re ready to create a Lambda function now! Head to the Lambda console and select Create Function.
  • Select Author from scratch. There is an Alexa smart home template available, but we won’t use it in this tutorial since it adds a lot of unnecessary code.
  • Choose a name for your lambda function. Leave the runtime on Node.js, and choose the role you just created from Existing Role.
  • Click Create Function.
  • From here, you can choose what services are allowed to trigger your function. Select Alexa Smart Home from the trigger list. If you don’t see it listed, double check that your region is still one of N. Virginia, Ireland, or Oregon.
  • Under Configure triggers, enter the ID you got from the Alexa skills page earlier in the tutorial. Make sure Enable Trigger is also on.
  • Click add, then Save in the top right.
  • If you look at the code in the trigger (by selecting the Lambda name and scrolling down), you can see that by default it’s just logging any requests that come in:
  • This is fine for now, but later we’ll come back here and set up our application logic.
  • Take the Lambda ARN (top right in the Lambda console) and enter it into your Alexa skill endpoints. Choose the geographical region that matches your Lambda region and skill language, as explained before.
  • Click Save.

Step 3: Setting Up Account Linking

For smart home skills, it’s required to set up account linking to connect the skill with your user accounts. Using OAuth 2.0 authorization grants, Alexa will request access and refresh tokens from your service periodically and pass these into any requests it makes. For us, these tokens will come from Particle and give us permission to communicate with a user’s physical devices.

  • Go into Account Linking and find the Redirect URLs section. From these, take note of the one that corresponds to your region:
  • Start by going to the Particle console and creating an account if you haven’t already.
  • Go to Authentication and click New Client.
  • Choose Custom.
  • Enter a name for the client, choose web, and tick none for scopes. This will give full access rights to the token owner.
  • Under Redirect Url, enter the correct region URL from the Alexa console. Technically, Alexa requires the OAuth client to redirect dynamically to the correct URL out of the three based on the request, which is not supported by Particle. Thankfully, it will accept any of the URLs anyway.
  • Click Get Client ID and Secret
  • Copy the ID and secret somewhere, since you will not be able to see the secret again.
  • Return to the Account Linking section of the Alexa console and fill out the entries:
  • Leave the other fields blank and click Save

We’re getting to the finish line! All that’s left for now is to try enabling the skill from Alexa to see if everything works.

  • Open up your Alexa app or the web app and go to Skills, Your Skills, Dev Skills. You should see your new skill listed, click on it.
  • Click Enable and cross your fingers. This should redirect you to the Particle.io login page.
  • Log into the Particle console and accept the access permissions.
  • If all goes well, you should be redirected to this screen:

And that is the longest part of the process done! In the next part, we’ll get into writing some code and controlling the micro-controller through Alexa.

Click here for part 2.

Troubleshooting

It’s possible that right in the last step, Alexa will give a ‘failure to link’ error with no further error information. This is a difficult bug to diagnose since the issue can lie anywhere in the process. Start by double checking:

  • Your Alexa ID and AWS ARN are correct in the corresponding consoles.
  • Your AWS region corresponds to the correct language in Alexa, and the correct endpoint in Account Linking.
  • You have NOT enabled the “Send Alexa Events” permission. We’ll go through that later, but enabling it now will cause failure to link.
  • Inspect the URL when you are re-directed to Particle Login. Are all the query parameters correct?
  • Set up a separate server to accept HTTP POST requests. Change the redirect URL in Particle Authorization to this, and see what parameters Particle passes back to it. Do these match up with the OAuth specification and the Alexa request? Note that missing the scope parameter is OK.
  • Go through Alexa’s official guide for Account Linking debugging.

--

--