In this tutorial you will learn how to integrate the Arduino IoT Cloud with the Amazon Alexa skill. At the end of this tutorial, we will be able to change the color of an RGB matrix, using only voice commands in the Alexa app.
This tutorial focuses on using the MKR RGB Shield but can easily be modified to use other matrices.
While this tutorial focuses on creating a smart lamp, it also shows the steps needed to integrate the two services, so you can essentially follow this tutorial to create other cool projects!
You can also find all variables that can be synchronized between Arduino Cloud and Alexa in the IoT Cloud Variables guide.
The goals of this project are:
Simply mount the MKR RGB Shield on top of the MKR WiFi 1010.
Let's start by navigating to the Arduino IoT Cloud.
Note: You will need a Arduino account to use the Arduino IoT Cloud. If you do not have one, you will be directed to the account registration.
Once we are in the Arduino IoT Cloud, we will need to click on the "Devices" tab. This will open a new page which will ask you to add a new device. Click on the "Add device" button.
You will now have an option of either configuring a new Arduino device, or a third party device. Select the "Set up an Arduino device option.
At this point, you will need to connect your cloud compatible board to your computer. You will also need to have installed the Arduino Create Agent. If if it is not installed, the set up wizard will ask you to install it. Your device should now show up, and you will need to click on the "Configure" button.
You will now be asked to name your device. In this case, a name was randomly generated, which is Phil. Click on "Next" to proceed.
After clicking on next, the board will start to configure. This process may take a few minutes.
Once it is done, we will be directed to the devices page, where we can see our device. Congratulations, you have just made your first device IoT ready!
After our device is configured, we can move on to the next step: creating our very first Thing. Click on the "Things" tab. You should now see a button that says "Create thing", which we will need to click.
We will now see an interface with multiple options. This is your Thing configuration overview. Here we can select what network we are connecting to, what device we are using and create variables that we want to sync.
Let's start by linking our freshly configured device, by clicking on the "Select Device" button to the right. This will open up a window, where we can "Associate" the board with this Thing.
After our device is configured, we need to create the variable that will store the R, G, B data that will be retrieved from Alexa. Click on the "Add Variable" button in the Thing overview.
Name the variable LoungeArea, and for variable type, select the Colored Light. When done, click on "Add Variable".
Now that we have created a variable, we can configure the network details. This is done by clicking on the "Configure" button in the "Network" section.
Note: You can't enter any network details until you have added a variable.
With all configurations done, we can move onto creating the program. By clicking on the "Sketch" tab, we can start editing the code right away.
First of all, we need to include two libraries called
ArduinoGraphics
and Arduino_MKRRGB
. These will help us control the MKR RGB Shield.We will only be adding a couple of lines inside the
setup()
, and then the functionality of the program will all be stored inside the onLoungeAreaChange()
function. The loop()
will remain empty.When we later on will be connecting the cloud to Alexa, the data will be received directly from the Alexa app. Whenever the data updates, the
onLoungeAreaChange()
will execute, where it will fetch the data from Alexa (the RGB values) and display them on the RGB matrix. You can find the full code in the snippet below.1#include <ArduinoGraphics.h>2#include <Arduino_MKRRGB.h>3#include "thingProperties.h"4
5void setup() {6 // Initialize serial and wait for port to open:7 Serial.begin(9600);8 // This delay gives the chance to wait for a Serial Monitor without blocking if none is found9 delay(1500);10
11 MATRIX.begin();12 MATRIX.brightness(10);13
14 while(!Serial);15
16 // Defined in thingProperties.h17 initProperties();18
19 // Connect to Arduino IoT Cloud20 ArduinoCloud.begin(ArduinoIoTPreferredConnection);21
22 /*23 The following function allows you to obtain more information24 related to the state of network and IoT Cloud connection and errors25 the higher number the more granular information you’ll get.26 The default is 0 (only errors).27 Maximum is 428 */29 setDebugMessageLevel(2);30 ArduinoCloud.printDebugInfo();31}32
33void loop() {34 ArduinoCloud.update();35 // Your code here36}37
38
39void onLoungeAreaChange() {40 uint8_t r, g, b;41 loungeArea.getValue().getRGB(r, g, b);42 if (loungeArea.getSwitch()) {43 Serial.println("R:"+String(r)+" G:"+String(g)+ " B:"+String(b)); //prints the current R, G, B values44 MATRIX.beginDraw(); //starts a new "drawing" on the RGB shield's pixels45 MATRIX.clear(); //clears the RGB shield's pixels46 MATRIX.noStroke();47 MATRIX.fill(r, g, b); //the r, g, b values are fed into the shield's pixels48 MATRIX.rect(0, 0, MATRIX.width(), MATRIX.height()); //creates a rectangle (this covers the entire matrix)49 MATRIX.endDraw(); // ends the draw, and displays the new "drawing"50
51 }52 else{53 Serial.println("Lamp Off");54 //the following code simply turns everything off55 MATRIX.beginDraw();56 MATRIX.clear();57 MATRIX.noStroke();58 MATRIX.fill(0, 0, 0);59 MATRIX.rect(0, 0, MATRIX.width(), MATRIX.height());60 MATRIX.endDraw();61
62 }63}
Upload the code in the snippet above to your MKR WiFi 1010 board. When it has successfully uploaded, go to the "Serial Monitor" tab to initialize the program. If the connection is successful, we should see the following:
We can now move on to the next step: setting up Alexa.
Did you know that the Arduino IoT Cloud supports over the air uploads? When you've uploaded a sketch to your board once, it will become available for you to upload a new sketch to the board without connecting it to your computer!
Over the Air uploads require an Entry plan to the Arduino IoT Cloud
To use this feature, make sure the board has power. If your board is already connected to the IoT Cloud, you will be able to upload to it over the air. Navigate to the Things sketch tab in the Arduino IoT Cloud interface, and you should see it being discovered just as if it was connected via USB.
We will now need the Amazon Alexa app which can be downloaded from the Apple App Store or the Google Play Store. Once installed, login with your existing account or create a new one.
Let's go through the steps necessary to install the Arduino Alexa Skill.
After waiting a little bit, we should see a new screen appear, where it says "1 light found and connected".
Click "Set Up Device". If you like you can also add it to a group (this way you can control and organize your devices more easily), otherwise skip this part.
Congratulations! You should now be able to control the lights through your Alexa app.
To control the lights on our setup, we will need to head over to the "Devices" tab in the Alexa app. Once in the Devices tab, click on the "Lights" button. We will now see the available lights. If you have other lights already connected, they will appear here as well. In our case, we only have one, which is LoungeArea, which is the same name as the variable we created earlier in the Arduino IoT Cloud. If we click on LoungeArea we will access the color / brightness control.
Any changes of color / brightness you make will be sent to our MKR WiFi 1010, which will change the pixels on the MKR RGB Shield accordingly.
One great way of knowing if data is coming through from the Alexa app is by checking the Serial Monitor. The sketch that we uploaded to the board includes a command that prints out the value of r, g and b whenever they receive new data. If the board is successfully connecting to the cloud, it is most likely a problem on setting up the Alexa device.
In this tutorial, we went through a few simple steps to integrate a MKR WiFi 1010 + MKR RGB Shield, the Arduino IoT Cloud and the Amazon Alexa app.
As a result, we now have a smart light that can be controlled directly through the Alexa app, and if you have a physical Alexa device, you can start playing around with different voice commands, such as changing the color and brightness of your lamp!
You can find more tutorials in the Arduino IoT Cloud documentation page.