Arduino Cloud Amazon Alexa Integration

Learn how to use Arduino Cloud and Amazon Alexa to interact with your sensors.

Components and Supplies

Apps and Online Services

About This Project

Intro: Getting Started

In this tutorial we are going to use Arduino Cloud, 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 your supported board, IoT Cloud things and properties and get you onboard.

Part 1: Arduino Cloud

From the main IoT 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 adding a new Device following the procedure that starts after clicking the ADD DEVICE button, under the Devices section.

Once done, we'll add three properties to our thing. The first two will represent the lamps, the last one the temperature.

The type ColoredLight lets us control an RGB Light, allowing us to control its brightness and colour. Set it as ReadandWrite because we'll need to control its behaviour using Alexa.

The DimmedLight type is in a way similar to the above, but only lets us control the brightness of a Light.

The last property to add is the temperature. Let's add this one as ReadOnly, since we only want to know its value, not set it. As a type we can use Temperature sensor (Celsius), but feel free to set it as Fº if it suits you better.

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

image 0xrarlfrro png HqyFyaXVAF

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

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.

Because we want to read some some environmental data (namely temperature, but feel free to add light, humidity and pressure if you please), we'll import the Arduino_MKRENV library at the top of our Sketch

1#include <Arduino_MKRENV.h>

Then we define some constants for the board pins that will be used for R,G,Bandwhitelight, this way the code is more readable:

1#define PIN_MEETING_ROOM 5
2#define PIN_LOUNGE_AREA_R 2
3#define PIN_LOUNGE_AREA_B 3
4#define PIN_LOUNGE_AREA_G 4

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

1if (!ENV.begin()) {
2 Serial.println("Failed to initialize MKR ENV shield!");
3 while (1);
4}

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

We do not need to specify that

pinMode
needs to be
OUTPUT
because we'll be using
analogWrite() 
to set the intensity of our white LED or the single colours of the RGB one.

In the loopfunction we are going to read the temperature every second:

1temperature = ENV.readTemperature();
2delay(1000);

Finally it's time to implement our callback functions: the ones that will be executed every time a change in a Property's value is pulled from IoT Cloud.

We have to implement

onMeetingRoomChange
and
onLoungeAreaChange
:

1void onMeetingRoomChange() {
2 uint8_t brightness = map(meetingRoom.getBrightness(), 0, 100, 0, 255);
3 if (meetingRoom.getSwitch()) {
4 Serial.println(brightness);
5 analogWrite(PIN_MEETING_ROOM, brightness);
6 }
7 else{
8 analogWrite(PIN_MEETING_ROOM, LOW);
9 }
10}

with the code above we first read the brightness value from the Cloud and map it to a usable value, then we verify if the light switch is on, if it is we can turn on the light, using the

brightness
red before. Otherwise we turn off the light

The working principle is the same for the other callback:

1void onLoungeAreaChange() {
2 uint8_t r, g, b;
3 loungeArea.getValue().getRGB(r, g, b);
4 if (loungeArea.getSwitch()) {
5 Serial.println("R:"+String(r)+" G:"+String(g)+ " B:"+String(b));
6 analogWrite(PIN_LOUNGE_AREA_R, r);
7 analogWrite(PIN_LOUNGE_AREA_B, b);
8 analogWrite(PIN_LOUNGE_AREA_G, g);
9 }
10 else{
11 Serial.println("Lamp Off");
12 analogWrite(PIN_LOUNGE_AREA_R, 0);
13 analogWrite(PIN_LOUNGE_AREA_B, 0);
14 analogWrite(PIN_LOUNGE_AREA_G, 0);
15 }
16}

The only notable difference is the fact that instead of the just brightness, we have three different components: they are the representation of the RGB colour of the light. 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.

Tap choose device.
Tap choose device.

After tapping on Choose Device you will be presented with the setup page listing all the available devices (They will be named according to how we named our properties in Arduino Cloud).

Device setup.
Device setup.

Let's choose one device and tap 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.

Tap continue.
Tap continue.

Repeat the Setup process for every device you want to control.

Finally the device view should look like this:

Office light.
Office light.

We can finally start asking things like "Alexa, what's the temperature in the office?" or "Alexa, turn on the light in the meeting room."

Current temperature.
Current temperature.

Have fun playing with Alexa and IoT Cloud.

The Arduino Team

Complete Sketch

Schematics

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.