Configuring an Arduino IoT Cloud Route
Notehub offers a full integration with the Arduino Cloud, Arduino’s platform for remotely monitoring and controlling IoT devices.
In this guide you'll learn how to create an Arduino Cloud route, how to write firmware that uses the integration, and how to build dashboards for your devices on Arduino Cloud.
Let's start get started by looking at the firmware your devices need to run.
Writing Firmware
In order to use the Arduino Cloud route your firmware must use the Arduino team's ArduinoIoTCloud library. This section covers how to install the library and how to configure it for a Notecard-based project.
-
If you haven't already, download and install Arduino IDE on your machine.
-
Within Arduino IDE, open the libraries tab, search for the ArduinoIoTCloud library, and click Install.
-
The ArduinoIoTCloud library has a sample Notecard sketch you can use to start your firmware. To use it, open your File --> Examples menu in Arduino IDE, and scroll to find and select the ArduinoIoTCloud --> ArduinoIoTCloud-Notecard example.
-
With the sketch open, select tab for the
thingProperties.h
file, locate theNOTECARD_PRODUCT_UID
constant, and update it with your Notehub project's ProductUID.#define NOTECARD_PRODUCT_UID "<your value goes here>"
-
Upload this starting sketch to your device.
-
After uploading, your device should immediately start uploading test data to Notehub. You confirm that your device is connected by checking your Notehub project's Devices screen.
Creating the Route
With your device connected, your next step is to create a Notehub route that connects your Notehub project to Arduino Cloud.
-
Open your Notehub project's Routes page, and click the Create Route button.
-
Scroll down and click the Arduino IoT Cloud's Select button.
-
Click the Create Route button to create the route.
Setting Up Arduino Cloud
To complete the integration, you next need to connect your devices in Arduino Cloud to the devices in your Notehub project.
-
Create an Arduino Cloud account (if you don't have one already).
-
Within Arduino Cloud, go to your Devices page, and click the + Device button to create a new one.
-
Select the Manual option.
-
Copy both your device's Device ID and Secret Key. (You need both in the next step.)
-
Back in your Notehub project, go to your Devices page, double click your device, and go to the Environment tab. Here, define two new environment variables for your device,
_sn
and_secret_key
, and set them as follows.-
Set
_sn
to your device's Device ID. -
Set
_secret_key
to your device's Secret Key.
-
The _sn
and _secret_key
environment variables are necessary for Notehub to be
able to send data to the correct devices in Arduino Cloud. If you wish to
use multiple devices, you need to repeat the preceding steps for each device you wish
to connect.
Creating a Dashboard
With your devices connected you're ready to start building dashboards using your data. Let's set one up to see how it works.
-
To create a dashboard in Arduino Cloud you must first create a Thing, or a virtual twin of your hardware/setup. To do so, open the Things menu in your Arduino Cloud dashboard, and click the + Thing button.
-
On the next screen do the following steps (see image below):
-
Give your new thing a name.
-
Associate your thing with your device.
-
Add the following cloud variable.
- Name:
led
- Type: Boolean
- Permission: Read & Write
- Update Policy: On change
- Name:
-
-
With your Thing in place, next head to the Dashboards section of Arduino Cloud, and click the + Dashboard button to create a new dashboard.
-
On the next screen take the following steps.
- Click the Add button.
- Click the THINGS tab.
- Selct your Thing from the list.
-
Finally, click the Create Widgets button, and Arduino will automatically generate a starting point for your new dashboard.
And at this point—you're all set! If all went well, toggling the switch in the Arduino Cloud dashboard should toggle the onboard LED on your host.
With the integration working, let's wrap up by discussing some things you may wish to configure.
Final Configuration
Updating Properties
The default ArduinoIoTCloud firmware defines three properties for all projects:
-
led
: A boolean that controls the host's built-in LED. -
potentiometer
: An integer value tied to a potentiometer. The sketch expects a potentiometer to be connected to the host'sA0
pin. -
seconds
: An integer that the sketch increments and publishes to Arduino Cloud every 5 minutes.
Most projects will want to comment out (or remove) the potentiometer
and seconds
properties, and add their own custom values. The following section shows how to
remove the unused properties, and add a new temp
one for demonstration.
-
In Arduino IDE, open your project's
thingProperties.h
file, find where the global variables below are declared, and make the following changes.bool led; // int potentiometer; // int seconds; float temp;
-
In the same file, scroll down to find the code that makes calls to
ArduinoCloud.addProperty()
and make the following changes. The last line tells Arduino Cloud that a newtemp
property should be available, should be read only (Permission::Read
), and should have a new value published every 5 minutespublishEvery(5 * MINUTES)
.ArduinoCloud.addProperty(led, Permission::ReadWrite).onUpdate(onLedChange); // ArduinoCloud.addProperty(potentiometer, Permission::Read).publishOnChange(10); // ArduinoCloud.addProperty(seconds, Permission::Read).publishEvery(5 * MINUTES); ArduinoCloud.addProperty(temp, Permission::Read).publishEvery(5 * MINUTES);
-
And finally, open your sketch's
.ino
file, find theloop()
function, and make the following changes. The code at the bottom uses the Notecard'scard.temp
request to take a temperature reading from the Notecard's onboard temperature sensor.void loop() { ArduinoCloud.update(); // potentiometer = analogRead(A0); // seconds = millis() / 1000; J *req = NoteNewRequest("card.temp"); if (J *rsp = NoteRequestResponse(req)) { temp = JGetNumber(rsp, "value"); NoteDeleteResponse(rsp); } }
-
Back in Arduino Cloud, return to the Thing you created earlier and add the following Cloud Variable to represent the
temp
property you just added to your firmware.- Name:
temp
- Type: Floating Point Number
- Permission: Read Only
- Update Policy: Periodically, every 300 seconds
- Name:
-
Finally, return to your Dashboard, and add widgets for your new
temp
cloud variable. If all went well, you should see the temperature of your device on your dashboard, and the value should update according to your provided interval.
Using Interrupts
If you’re using an interrupt-capable host (like the Blues Swan), you may wish to enable interrupts. If you utilize interrupts, the Notecard will signal each time new data arrives, so the host can process it without delay. Without the interrupt the host has to continuously poll the Notecard for new information.
To enable interrupts you must do two things.
Firmware
First, open your sketch's .ino
file and uncomment the #define ATTN_PIN 9
line. The value of the constant
needs to be set to an interrupt capable pin on your host.
#define ATTN_PIN 9
Wiring
Second, you must connect your Notecard's ATTN
pin to the pin on your host you
specified above.
For example, if you're using a Blues Swan and have #define ATTN_PIN 9
defined,
you need to connect the Swan's D9
pin to the Notecard's ATTN
pin to take advantage
of the interrupt.