This is a fairly quick project, with a bit of a longer story. If you're anxious to jump into building something, you can skip the story and jump down to the next section below.
Let's begin with a story...So there I was creating my 5th Photon controlled LED lighting project for my RV. This was getting really fun. I had several Photons controlling lights and fans, and an Alexa skill and iOS apps to control them. To add another device, all I needed to do was:
- Decide on a name for the new device
- Copy/Paste and modify a bunch of existing C++ code from my earlier 4 projects to the new Photon.
- Update my Alexa skill to recognize the new name
- Update my iOS control panel apps to add the new name
Being a professional programmer, it bothered me that every time I added another IoT project to my RV I had to repeat these same 4 steps, even if the new project was very similar to one of the others. So I started thinking about how to reorganize my Alexa skill, iOS apps, and Photon sketches to see if I could make this process quicker, easier, and less error prone. It turns out that it can indeed be a lot quicker and easier, as I'll show you in this article.
To start with, I took all the duplicated Photon code and put it into a library that can be used by each new project without any cutting and pasting each time. Then I published this library, named "IoTlib", using Particle's new v2 library format so that it can be used by anyone, using any of the available IDEs: Web, Atom, or CLI.
Next I thought about how to enable the Alexa skill and iOS apps to so that they could automatically detect new devices so that I wouldn’t have to keep updating them in order to support new devices. I realized that each device can expose its name, and the actions it supports, using a particle.io variable. So I updated the IoTlib code to do that, and updated the Alexa smart home skill to automatically "discover" those actions. So now when I add a new Photon device, all I have to do is program it using the features provided by the IoTlib, and within a few minutes I can start controlling it with my Alexa. I can also use the Alexa app tell it to "Discover new devices", and the new device will be discovered right away.
What you will needThis project will show you how you can easily control your Photon device with Alexa using the open source Patriot IoTlib library and the Patriot hobbyist smart home skill.
To begin, you will need a Particle.io Photon and a free Particle.io account. If not, follow the instructions that came with the Photon, also available on the Particle.io website, to claim it on your account. You can power up the Photon using just a USB cable, or other power supply as described on the Particle.io website.
You will also need an Alexa device (Echo, Tap, or Dot) and a free Amazon account to login to the Alexa app or Alexa website.
Building the simplest voice controlled Photon projectSo with Photon in hand and powered up, and an Alexa device nearby, let's begin.
Experienced user instructionsIf you are already familiar with how to program your Photon, and you know how to install an Alexa skill, here is all you need to know:
- Create a Photon app using the example "starter" code. You can git this from Github or copy it from the listing later in this article. Be sure to include the IoT library.
- Install the "Patriot hobbyist smart home skill" using the Alexa app, and link it to your Particle.io account when prompted.
- Say "Alexa, turn on Photon" and watch your Photon's blue LED turn come on.
- Say "Alexa, turn off Photon" and watch the blue LED turn off.
That's it. Feel free to explore the other examples in the Github repo, or be adventurous and explore the source. I welcome your feedback.
Full step-by-step instructionsSo with Photon in hand and powered up, and an Alexa device nearby, let's begin.
- Log into the Particle.io console and verify the name of your Photon device, and that it is online. This is indicated by a pulsing blue dot on the left. If it isn't pulsing blue, then it isn't powered on or isn't connected to the network.
- Switch to the Particle.io IDE by selecting the </> icon on the bottom left of the console.
- In case you have more than one device on your account, select Devices and verify that the desired device is selected.
- Next select the Code icon < > on the left and enter a name for your app and press Enter.
- Now add the IoTlib by selecting the Library icon on the left, entering IoT into the search field, and selecting "IoT" from the displayed results. Select the IOT, shown in the figure here at the bottom of the displayed list.
- When prompted, choose to include IoT in your project.
- Select the name of your project when prompted for "Which app?" and select Confirm.
- The following lines should now appear at the top of your project .ino file.
// This #include statement was automatically added by the Particle IDE.
#include <IoT.h>
- Now repeat the last 3 steps to include the PatriotLight library also. Patriot uses plugin libraries to add support for specific IoT devices to your project. In this example we will be controlling an LED light, so we need to add the PatriotLight library.
- Erase the other lines in the file, and copy the starter example code in its place. Leave the 2 lines shown above at the top of the file. Your file should look like this:
#include <IoT.h>
#include <PatriotLight.h>
IoT *iot;
void setup() {
iot = IoT::getInstance();
iot->begin();
// Create devices
//Note: D7 is not a PWM pin, so it can only turn on or off
// Alexa will respond to "Alexa, turn L E D on" or "Alexa, turn off L E D"
// You can change the name 'LED' to whatever you like, but it needs to be something
// that Alexa can recognize.
Light *light1 = new Light(D7, "LED");
// Tell IoT about the devices you defined above
iot->addDevice(light1);
// The "behavior" defines additional commands that control things.
// Note that the name of each device can be used to control it, so behaviors are
// not required unless you want to control more than one device with a commands.
// Alexa will respond to "Alexa, turn photon on" or "Alexa, turn off photon"
// You can change the word 'photon' to whatever you like, but it needs to be something
// that Alexa can recognize. For now, use a single word.
iot->addBehavior(new Behavior(light1, "photon", '>', 0, 100)); // On
iot->addBehavior(new Behavior(light1, "photon", '=', 0, 0)); // Off
}
void loop() {
iot->loop();
}
- All of the lines that begin will // are comments, and are there to help you understand what the code is doing. You can delete those comments once you understand what the code is doing, or choose to leave them there for future reference. They have no effect on the operation of the code.
- Now build and flash the code by selecting the lightning bolt at the top left. You should see the blue light on your photon blink different colors, returning to pulsing cyan when done.
- At this point we're done programming the Photon.
We're almost done. We just need to install the Alexa skill.
- Open the Alexa app on your phone or the Alexa webpage in your browser.
- Select Skills and enter "Patriot" in the search field.
- Locate the "Patriot hobbyist smart home skill" in the list, and select it.
- Then select to "Enable Skill"
- Once the skill is installed, you will be prompted to "Discover new devices". Select to do so. It may take a couple minutes to discover all your devices.
- Once discovery completes, the name of your device and action should appear in the list of installed devices.
At this point you can say "Alexa, turn on photon" (or whatever words you used for the device name and/or in the behavior in the sketch) and the blue LED will come on. Say "Alexa, turn off photon" and it will go out..
I encourage you to experiment. Take a look at the other examples on the Github repo. Breadboard additional LED to PWM pins like D0 through D3 and see how the LEDs are slowly turned on and off.
Note that the Alexa app was recently updated (October 2017), and now allows control of smart home devices. So you will have the option of controlling your IoT devices using your Voice with Alexa, or with your phone with the Alexa app, or with another IoT device using for example the PatriotSwitch library.
In future articles I will demonstrate how to use the IoT library to add additional LED lights, switches, temperature sensors, and more.
Note: this article has been updated to reflect the newer version (2.0) of the IoT and plugin libraries. Refer to the code and examples on Github for the latest version numbers and examples.
Comments