Skip to content

arduino/iot-client-js

Repository files navigation

Arduino iot-api Javascript client

Installation

npm i @arduino/arduino-iot-client

For browser

The library also works in the browser environment via npm and browserify. After following the above steps with Node.js and installing browserify with npm install -g browserify, perform the following (assuming main.js is your entry file):

browserify main.js > bundle.js

Then include bundle.js in the HTML pages.

Webpack Configuration

Using Webpack you may encounter the following error: "Module not found: Error: Cannot resolve module", most certainly you should disable AMD loader. Add/merge the following section to your webpack config:

module: {
  rules: [
    {
      parser: {
        amd: false
      }
    }
  ]
}

Getting Started

Please follow the installation instruction and execute the following JS code:

var ArduinoIotClient = require('@arduino/arduino-iot-client');
var client = ArduinoIotClient.ApiClient.instance;
// Configure OAuth2 access token for authorization: oauth2
var oauth2 = client.authentications['oauth2'];
oauth2.accessToken = await getToken();
    
var api = new ArduinoIotClient.DevicesV2Api(client)
/* if required, organization_id can be configured as follow and the, pass opts var to function.
let opts = {
  'xOrganization': "<org_id>"
};
*/
api.devicesV2List().then(devices => {
    console.log(devices);
}, error => {
    console.log(error)
});

For a working example, see the example folder in this repo.

Authentication

The client requires a valid OAuth2 access token, you can get one like this:

var rp = require('request-promise');

var options = {
    method: 'POST',
    url: 'https://api2.arduino.cc/iot/v1/clients/token',
    headers: { 'content-type': 'application/x-www-form-urlencoded' },
    json: true,
    form: {
        grant_type: 'client_credentials',
        client_id: 'YOUR_CLIENT_ID',
        client_secret: 'YOUR_CLIENT_SECRET',
        audience: 'https://api2.arduino.cc/iot'
        // If required, specify organization identifier adding "organization_id: '<org_id>'" in form.
    }
};

try {
    const response = await rp(options);
    console.log("Access token: " + response['access_token']);
}
catch (error) {
    console.error("Failed getting an access token: " + error)
}

How to get Arduino IoT Cloud Client Credentials

You can generate Arduino IoT Cloud Client Credentials in the ARDUINO API section in the IoT Cloud things section:

Step 1

IoT Cloud Site

Step 2

IoT Cloud Site

Step 3

IoT Cloud Site

About

Javascript client for Arduino IoT API

Resources

License

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 8

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy