JavaScript / Node.js Library
The JavaScript Library allows you to connect to the Arduino Cloud using Node.js.
This library provides interaction with the Arduino Cloud MQTT broker and can be used both from the browser and Node.js.
Connection via this library is achieved by registering a manual device, i.e. a virtual device that is not associated with an Arduino hardware board. This virtual device can connect through a simple username/password (Device ID, Secret Key) which is generated in the Arduino Cloud when configuring a device.
This library requires a version of Node.js to be installed on your machine.
GitHub
To view the source code and report issues, follow the links below to the GitHub repository:
Requirements
Installation
You can install this library using either
npm
or yarn
.1$ npm install arduino-iot-js
1$ yarn add arduino-iot-js
Check out the JavaScript Setup guide for more information and a detailed step by step tutorial.
Connection Methods
There are three available methods for connection:
- Using device credentials (recommended method).
- Using an API key (generated and listed at Arduino Cloud API keys).
- Using a JWT token
Device Credentials
Device credentials is the easiest method. These credentials are generated when configuring a manual device in the Arduino Cloud, and works like a username/password. The example below uses device credentials:
1//JavaScript code2
3const { ArduinoIoTCloud } = require('arduino-iot-js');4
5(async () => {6 const client = await ArduinoIoTCloud.connect({7 deviceId: 'YOUR_DEVICE_ID',8 secretKey: 'YOUR_SECRET_KEY',9 onDisconnect: (message) => console.error(message),10 });11
12 const value = 20;13 let cloudVar = "test_value"14
15 client.sendProperty(cloudVar, value);16 console.log(cloudVar, ":", value);17
18 client.onPropertyValue(cloudVar, (value) => console.log(cloudVar, ":", value));19})();
API Key
You can also connect using an API key generated from the Arduino Cloud API Key Section, a method that is almost identical to Device Credentials, but where you also need to specify your Thing ID. This is available in the metadata section of your Thing.
1import { ArduinoIoTCloud } from 'arduino-iot-js';2
3(async () => {4 const client = await ArduinoIoTCloud.connect({5 clientId: 'YOUR_CLIENT_ID',6 clientSecret: 'YOUR_CLIENT_SECRET',7 onDisconnect: (message) => console.error(message),8 });9
10 // Send a value to a thing property11 const value = 'some value';12 client.sendProperty('YOUR_THING_ID', 'YOUR_VARIABLE_NAME', value);13
14 // Listen to a thing property's changes15 client.onPropertyValue('YOUR_THING_ID', 'ANOTHER_VARIABLE_NAME', (value) => console.log(value));16})();
JSON Web Token (JWT)
To connect using a JWT, you can use the script below:
1import { ArduinoIoTCloud } from 'arduino-iot-js';2
3async function retrieveUserToken() {4 // Retrieve JWT Token here5}6
7(async () => {8 const token = await retrieveUserToken();9
10 const client = await ArduinoIoTCloud.connect({11 token,12 onDisconnect: (message) => console.error(message),13 });14
15 // Send a value to a thing property16 const value = 'some value';17 client.sendProperty('YOUR_THING_ID', 'YOUR_VARIABLE_NAME', value);18
19 // Listen to a thing property's changes20 client.onPropertyValue('YOUR_THING_ID', 'ANOTHER_VARIABLE_NAME', (value) => console.log(value));21})();
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.