In this tutorial I will show you how to use Amazon Alexa to turn an LED on and off via Blynk. Unlike IFTTT or other 3rd party services, this method allows you to use Amazon directly and get much more creative about the things you want your devices to do. Plus you will quickly learn how to build real Alexa skills.
First, shout out to Dave York for writing the original Alexa and Lambda code, which you can find on Github.
You can also find my fork with edits here.
Setting Up Arduino and Blynk
Now, let's take a look at our goal. In this photo I have an ESP32 connected to a red LED that is off (on the left side). The LED is connected to GPIO 5 of the ESP32 and is controlled by Virtual Pin 1 (V1) by Blynk.
Using the Blynk app, create a new project in order to get an Auth Token.
Also add a button in Blynk and tie it to V1 as a SWITCH.
Below is the code for Arduino that sets up the pin, connects to Blynk, and sets a virtual pin to V1.
Remember to change the Auth token, WiFi, and password in the sketch above.
You should now be able to connect your Arduino to Blynk. If you have an LED attached, make sure that you can switch it on and off from the app to ensure everything is connected correctly.
Setting Up the API
First, you need to understand a high level overview of how this works. Blynk has an API that allows you to control your devices from the internet. It'd be good for you to test this functionality out to see how it works.
Go here to view the documentation.
Notice the structure of the GET request.
If you are using the Blynk server (which is the default), what you need to do is replace the bold text with your values. In this case, you will replace auth_token with your auth token value, replace pin with V1, and replace value with 1 or 0 to turn on or off.
Copy and paste this into your browser and your LED should turn on. Change the value to 0 and it should turn off. If it doesn't, something is wrong and you need to fix it before moving on.
Setting Up Alexa & Lambda
Now that we have the API set up, we will now set up Alexa and Lambda.
If you have never heard of Lambda, it is a service offered by Amazon AWS that allows you to run functions without the use of a server (serverless). Without it, you would need to set up a server to make the GET request above. But instead, we will simply copy some code into Lambda that will execute the GET request once you give the proper command to Alexa.
Remember, when using Alexa, we are always using two different services. The Alexa service, which recognizes and interprets your voice, and Lambda which does the actual computing and makes things happen. These are completely different services, so don't get confused.
Let's set up Alexa first.
Log in with THE SAME ACCOUNT that your Alexa device is logged into. Chances are it's your regular Amazon account that you use every day.
This will ensure that your new app appears in your Alexa phone app.
Now go to your Alexa Skills Developer Console
Click on Create Skill
We will create a Custom skill and Start from scratch. Enter a name and click Create
There are two ways to create your skill. You can use the JSON editor to code everything, or use the menus to visually enter everything. Let's use the JSON editor to add the code and then visually edit anything you might want.
First locate the JSON editor on the left side and click on it.
Copy the following code and paste it into the JSON editor. Be sure to remove the code that was previously there.
Save everything by clicking on Save Model above. When you are ready to test, you can click on Build Model, which takes a minute to build everything. For now we will just save.
Let's set the invocation name that you would like to say. This is the name of your device that you want to call. For example, if you call it "Morning Rod", you will say "Alexa, tell morning rod to turn on". This will allow Alexa to know what device to look for.
Next, we have intents. These are the things that you want your skill to do. As you can see, there are about 5 default intents. You cannot remove these as they are required. We will only focus on the custom onOff intent we just added.
Click on the intent to open it up. This section allows you to add phrases that you want to say in order to start the intent. So after you say, "Alexa, tell Morning Rod"... you now have to add phrases. Add as many phrases as you can think of, just in case the user says it in a different way. In the phrase is a slot that we chose to put On/Off in.
If the user says on, it will trigger the On function, if they say Off, it will trigger the off function.
Do not change anything for this demo, but feel free to learn more about slots and intents by reading the documentation.
If we tell Alexa to do something, she should now understand what is being said, but nothing will happen. That's because we now need to set up Lambda in order to communicate with Blynk.
Create an AWS account and log in.
It is very important that you set your region in the top right to one of these as only these regions support Alexa
- Asia Pacific (Tokyo)
- EU (Ireland)
- US East (N. Virginia)
- US West (Oregon)
AWS is very strict on permissions in order to protect your account, so first we need to set some permissions.
Search for the IAM service and open it up
Click on 'Roles' and 'Create Role'
Next find Lambda, and go next
Next search for Lambda in the list and select AWSLamdaFullAccess
Click Next, do not add tag
Under Review, give it a name, and click Create Role.
Now we are ready to create our Lambda function
In services, open up Lambda.
On the main page, click Create Function
We will Author from scratch.
Give it a name
Select Node.js 8.10
Choose Existing Role
Now select the role you created earlier in IAM
Select the Alexa Skill Kit. Now scroll all the way down to the bottom of the page to configure it.
You should see this at the bottom. We now need to figure out our Skills ID.
Go back to your Alexa Skills Page
Click on Endpoint and you will see the Skill ID on the right side. Copy it and paste it into Lambda.
While we are here, notice in the section below the Skill ID is the Default Region, and it's required.
Go to the top of your Lambda page and find the region ARN. Copy this to the Alexa Default Region box
Next, go back to Lambda and click on the function in the middle of the page. After you click on it, scroll to the bottom
Delete what is in there, and copy the following code into it.
We need to make some changes to it.
First, let's try to understand what is going on here. Notice the word "On".
This code will run once Alexa detects the word On in your phrase.
The "Ok" is what she will respond with
And finally, Lambda will execute the Blynk API. You need to plug your auth token into this section. You can also change the pin if you'd like, but we are using V1 for this example.
Scroll down to the "Off" section and update your Auth Token there as well.
Go back to Alexa Developer Console and Build Model. It will take a minute.
Now test is out.
Click on Test.
In the input box, you can type or speak but clicking the microphone. I prefer to type. You can see that I types "alexa tell morning rod to turn on"
and in response, she said OK and executed the Lambda Function which turned the light on!
We're not done yet, but so close!!
We need to enable those skill in Alexa, so we can use it on all devices around our home. Only your account will have access to this unless you publish it, which we don't do.
Go to your Alexa app.
In the Menu click on Skill and Games
Click on Your Skills and scroll all the way to the right, for the Dev section
There's your skill! Now enable it
It should now FINALLY work!!