Alexa, Light My Mood

Learn how to control your MKR RGB Shield using Arduino Cloud and Amazon Alexa.

Components and Supplies

Apps and Online Services

About This Project

Intro: Arduino Cloud

Arduino Cloud is a platform that enables anyone to build IoT connected objects with ease. In order to control our MKR RGB Shield using Alexa, we'll also be using the Official Arduino Alexa Skill.

If you are new to Arduino Cloud, we advise to first take a look at this introductory project, which will give you an overview and guide you through the process of setting up one of the supported boards, IoT Cloud Things and Properties and get you onboard.

Part 1: Arduino Cloud

From the main Cloud page, we'll create a new thing and assign it a meaningful name. We'll then select the board we are going to use. If you can't see your board you might have skipped the getting started procedure: look at the section above.

Once done, we'll add a property to our thing that will represent the RGB shield. As a type remember to choose ColoredLight which lets us control an RGB Light, allowing us to change its brightness and colour. Set it as Read and Write because we'll need to control its behaviour through Alexa.

Here it is what the Dashboard view of your Thing should look like at this point:

screenshot from 2019 12 23 15 37 38 lFNTKH9oSn

When adding properties make sure you only use types listed under the Smart Home section, otherwise they will not work with Alexa.

The Smart Home Section
The Smart Home Section

Now go to the Web Editor by clicking the Edit Sketch button in your Thing's edit view.

Part 2: Arduino Web Editor

In the Web Editor we need to add some code to the Sketch automatically generated for us.

The first thing to include are the libraries which will help us to control the MKR RGB Shield:

1#include <ArduinoGraphics.h>
2#include <Arduino_MKRRGB.h>

We have to include both libraries because Arduino_MKRRGB depends on ArduinoGraphics.

Now in the setup function we can initialize the Arduino MKR RGB Shield with:

1if (!MATRIX.begin()) {
2 Serial.println("Failed to initialize MKR RGB shield!");
3 while (1);
4}
5MATRIX.brightness(10);

This way if the shield is not installed Sketch execution will be blocked.

The brightness is set to 10 for development purposes (no external power supply needed). You can use larger values (supported range is from 0 to 255) if you plan to use a 4 Amps 5v power supply as stated in the getting started guide.

Now it's time to implement our callback function: the one responsible of setting the colour/state of the LEDs. This function will be executed every time a change in a Property's value is pulled from IoT Cloud.

Let's implement

onMoodLightChange
:

1void onMoodLightChange() {
2 uint8_t r, g, b;
3 moodLight.getValue().getRGB(r, g, b);
4 MATRIX.beginDraw();
5 if (moodLight.getSwitch()) {
6 Serial.println("R:"+String(r)+" G:"+String(g)+ " B:"+String(b));
7 MATRIX.fill(r, g, b);
8 MATRIX.rect(0, 0, MATRIX.width(), MATRIX.height());
9 }
10 else{
11 MATRIX.clear();
12 }
13 MATRIX.endDraw();
14}

With the code above we first read the rgb value from the Cloud and save the representation of the RGB colour in some variables, we then verify if the light switch is on; if it is we can turn on the light, using the colour values read before. Otherwise we turn off the light.

The

beginDraw
and
endDraw
functions are used to alter the state of the matrix,
fill
is used to select the colour to use, rect is a function used to specify the region to fill.

Of course we can define custom colours by name in the Alexa app so we won't have to manually tell which amounts of Red, Green or Blue we want to set.

Part 3: Amazon Alexa Skill Setup

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. We'll follow the sequence of images below to see all the required steps.

1/9: Tap on "Devices"
1/9: Tap on "Devices"
2/9: Let's now enable our Smart Home Skills
2/9: Let's now enable our Smart Home Skills
3/9: We'll be presented with the featured ones. Time to search for what we need :)
3/9: We'll be presented with the featured ones. Time to search for what we need :)
4/9: Typing "Arduino" and tapping "search" we'll obtain a list. That's the one we need!
4/9: Typing "Arduino" and tapping "search" we'll obtain a list. That's the one we need!
5/9: Once selected, we'll have to enable it
5/9: Once selected, we'll have to enable it
6/9: Let's login with our Arduino Create account credentials
6/9: Let's login with our Arduino Create account credentials
7/9: Our Alexa and Arduino Cloud can now talk to each other :)
7/9: Our Alexa and Arduino Cloud can now talk to each other :)
8/9: Time to discover our devices
8/9: Time to discover our devices
9/9: A little bit of patience won't hurt
9/9: A little bit of patience won't hurt

Part 4: Amazon Alexa Devices Setup

After waiting a little bit, you should see that some devices have been found.

Follow the sequence of images below to setup the device:

 1/5: Now you should see that some devices have been found, click on "Set Up Device"
1/5: Now you should see that some devices have been found, click on "Set Up Device"
2/5: If you like you can also add it to a group to keep things organized
2/5: If you like you can also add it to a group to keep things organized
3/5: Choose the group you prefer and click on "Next"
3/5: Choose the group you prefer and click on "Next"
4/5: Finished! :)
4/5: Finished! :)
5/5: We can finally control our light's colour through Alexa!
5/5: We can finally control our light's colour through Alexa!

Note: The devices will be named according to how we named our properties in Arduino Cloud. If you have trouble finding the devices, you might have not selected your property from the "Smart Home" section.

We're done, it's now time to voice control our MKR RGB Shield asking things like

  • ”Alexa, turn on the light in the office”
  • ”Alexa, change the colour of the mood light to red”
  • ”Alexa, set the brightness of the mood light to 50%”

Have fun playing with Alexa and IoT Cloud. If you have questions and/or build this project let us know in the comments below.

Thank you, the Arduino Team

Complete Sketch

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.