Alexa & Arduino IoT Cloud Integration

Learn how to build a smart lamp by integrating the Arduino IoT Cloud and Alexa.

Introduction

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.

Goals

The goals of this project are:

  • Set up the Arduino IoT Cloud.
  • Create a program that controls the RGB pixels.
  • Install the Arduino Skill on the Alexa app.
  • Sync the Arduino IoT Cloud with Alexa.

Hardware & Software needed

Circuit

Simply mount the MKR RGB Shield on top of the MKR WiFi 1010.

Mounting the shield.
Mounting the shield.

Step 1: Setting up the Arduino IoT Cloud

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.

Configure a new device

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.

Adding a new device.
Adding a new device.

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.

Selecting the type of device.
Selecting the type of device.

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.

Device found.
Device found.

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.

Naming the device.
Naming the device.

After clicking on next, the board will start to configure. This process may take a few minutes.

The configuration process.
The configuration process.

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!

Configuration complete!
Configuration complete!

Creating a Thing and linking your device

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.

Creating a thing.
Creating a thing.

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.

Overview of a Thing.
Overview of a Thing.

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.

Associating the device.
Associating the device.

Creating the RGB variable

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.

The "Add Variable" button.
The "Add Variable" button.

Name the variable LoungeArea, and for variable type, select the Colored Light. When done, click on "Add Variable".

Adding a variable.
Adding a variable.

Adding your network details

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.

Entering the network details.
Entering the network details.

Note: You can't enter any network details until you have added a variable.

Creating the program

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.

Click on the "Sketch" tab to edit the sketch.
Click on the "Sketch" tab to edit the sketch.

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 found
9 delay(1500);
10
11 MATRIX.begin();
12 MATRIX.brightness(10);
13
14 while(!Serial);
15
16 // Defined in thingProperties.h
17 initProperties();
18
19 // Connect to Arduino IoT Cloud
20 ArduinoCloud.begin(ArduinoIoTPreferredConnection);
21
22 /*
23 The following function allows you to obtain more information
24 related to the state of network and IoT Cloud connection and errors
25 the higher number the more granular information you’ll get.
26 The default is 0 (only errors).
27 Maximum is 4
28 */
29 setDebugMessageLevel(2);
30 ArduinoCloud.printDebugInfo();
31}
32
33void loop() {
34 ArduinoCloud.update();
35 // Your code here
36}
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 values
44 MATRIX.beginDraw(); //starts a new "drawing" on the RGB shield's pixels
45 MATRIX.clear(); //clears the RGB shield's pixels
46 MATRIX.noStroke();
47 MATRIX.fill(r, g, b); //the r, g, b values are fed into the shield's pixels
48 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 off
55 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}

Uploading the program

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:

Information regarding connection to network & cloud.
Information regarding connection to network & cloud.

We can now move on to the next step: setting up Alexa.

Step 2: Setting up Alexa

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.

Installing the Arduino Alexa Skill

Let's go through the steps necessary to install the Arduino Alexa Skill.

Installing the Alexa Skill.
Installing the Alexa Skill.

  1. Once in the Amazon Alexa app, begin by tapping on "Devices".
  2. Tap on the "Enable Smart home skills" button.
  3. We will now see a list of featured ones, where we need to search for the right skill.
  4. If we search for "Arduino" we will see a skill that has that exact name.

Installing the Alexa skill part 2.
Installing the Alexa skill part 2.

  1. Once we have selected it, we'll need to enable it.
  2. We now need to sync it with our Arduino Cloud, by logging into it.
  3. Our Alexa and Arduino IoT Cloud is now synced!
  4. We can now start discovering our devices!

Looking for devices.
Looking for devices.

Amazon Alexa devices setup

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.

Light found and connected.
Light found and connected.

Congratulations! You should now be able to control the lights through your Alexa app.

Step 3: Controlling the lights

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.

Controlling the lights through the Alexa app.
Controlling the lights through the Alexa app.

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.

Troubleshooting

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.

Conclusion

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.

The Alexa and Arduino IoT Cloud integration.
The Alexa and Arduino IoT Cloud integration.

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!

More tutorials

You can find more tutorials in the Arduino IoT Cloud documentation page.

Contribute to Arduino

Join the community and suggest improvements to this article via GitHub. Make sure to read out contribution policy before making your pull request.

Missing something?

Check out our store and get what you need to follow this tutorial.

Suggest Changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. You can read more on how to contribute in the contribution policy.