JavaScript

Learn how to connect to the Arduino Cloud via JavaScript (node.js).

The arduino-iot-js library allows you to interact with the Arduino Cloud via MQTT. It supports basic authentication using the device ID as well as secret key that is obtained from the Arduino Cloud when configuring a manual device.

Overview

In this guide we will:

  • Configure a manual device in the Arduino Cloud,
  • install the arduino-iot-js library,
  • create a JavaScript example code that connects to the Arduino Cloud.

Requirements

To follow this guide, make sure to have:

Cloud Setup

To begin with, we need to create a manual device, and create a new Thing. Navigate to the Arduino Cloud and to the "Things" tab.

Thing & Device Configuration

  1. Create a new Thing, by clicking on the "Create Thing" button.
  2. Click on the "Select Device" in the "Associated Devices" section of your Thing.
  3. Click on "Set Up New Device", and select the bottom category ("Manual Device"). Click continue in the next window, and choose a name for your device.
  4. Finally, you will see a new Device ID and a Secret Key generate. You can download them as a PDF. Make sure to save it as you cannot access your Secret Key again.
Device credentials.
Device credentials.

Create Variable

Next step is to create a Cloud variable, which we will later interact with in our JavaScript code.

  1. While in Thing configuration, click on "Add Variable" which will open a new window.
  2. Name your variable
    test_value
    and select it to be of an
    int
    type.
  3. Click on "Add Variable" at the bottom of the window.

You should now have a variable named

test_value
. It is important that it is named exactly like this, as we will be using it in the example script of this guide.

Complete Thing.
Complete Thing.

Variables that we create here can also be synchronized with variables running on any other device in the platform. This makes it possible to link an Arduino boards with a Python or JavaScript project without writing any connection code!

JavaScript Setup

Before continuing, make sure you have a version of node.js installed. You can check that it is installed by opening a terminal and running:

1node -v

Install NPM Package

The arduino-iot-js library can be installed by running the following command:

1npm install arduino-iot-js

This will install the library in your current directory. You can check that the library is installed by running

npm list
.

Example Script

Create a file with a

.js
extension, and call it something appropriate such as
cloud_first_test.js
. Note that it needs to be in the same directory as you installed the library.

Below is a script that connects to the Cloud using the device ID and secret key that we obtained in the cloud setup section. Copy the contents into your

.js
file.

1const { ArduinoIoTCloud } = require('arduino-iot-js');
2
3(async () => {
4 const client = await ArduinoIoTCloud.connect({
5 deviceId: 'YOUR_DEVICE_ID',
6 secretKey: 'YOUR_SECRET_KEY',
7 onDisconnect: (message) => console.error(message),
8 });
9
10 const value = 20;
11 let cloudVar = "test_value"
12
13 client.sendProperty(cloudVar, value);
14 console.log(cloudVar, ":", value);
15
16 client.onPropertyValue(cloudVar, (value) => console.log(cloudVar, ":", value));
17})();
  • sendProperty(cloudVar, value)
    - update a variable in the Cloud,
  • onPropertyValue(cloudVar, (value))
    - when receiving an update from the Arduino Cloud, updates the
    value
    variable.

This example simply sends an update to the Arduino Cloud (updating the value of

test_value
to
20
), and awaits any changes from the Cloud. You can ensure it is working by checking your Thing interface as well:

Value in the Thing interface
Value in the Thing interface

You can test out the

test_value
variable by creating a dashboard in the Arduino Cloud with a value widget linked to the variable.

When entering a new number, you should see the following command being printed in the terminal:

1test_value : <value>

For creating dashboards and linking variables, check out the Dashboard & Widgets documentation.

Troubleshooting

  • Error: Cannot find module
    - the package is not installed/accessible from your current directory. Double check that your directory is correct and that you have successfully installed the package.
  • Error: connection failed: client not connected
    - likely due to wrong credentials (device ID / secret key). Double check these parameters.

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.