|
| 1 | + |
1 | 2 | [](https://www.gnu.org/licenses/gpl-3.0)
|
2 | 3 | [](https://badge.fury.io/js/arduino-iot-js)
|
3 | 4 |
|
4 | 5 | # arduino-iot-js
|
| 6 | +## Introduction |
| 7 | +This NPM module provides interaction with the Arduino IoT Cloud MQTT broker. It can be used both from the browser and node.js |
5 | 8 |
|
6 |
| -JS module providing interaction with Arduino Cloud |
| 9 | +The main features of this module are: |
| 10 | +- Connection/disconnection to Arduino IoT Cloud Broker using WebSocket |
| 11 | +- Send IoT Cloud *property* updates |
| 12 | +- Listen for IoT Cloud *property* updates made by other clients and/or devices |
7 | 13 |
|
8 |
| -## Installation |
| 14 | +If you are looking for a way to create, read, update, delete resources like |
| 15 | +- Devices |
| 16 | +- Things |
| 17 | +- Properties |
| 18 | +- Data Timeseries |
9 | 19 |
|
10 |
| -```bash |
11 |
| -$ npm install arduino-iot-js |
12 |
| -``` |
| 20 | +please check the official [Javascript Rest API client](https://www.npmjs.com/package/@arduino/arduino-iot-client). |
13 | 21 |
|
14 |
| -## How to use |
15 |
| -```javascript |
16 |
| -import ArduinoCloud from 'arduino-iot-js'; |
17 |
| - |
18 |
| -// connect establishes a connection with mqtt, using token as the password |
19 |
| -// options = { |
20 |
| -// host: 'BROKER_URL', // Default is wss.iot.arduino.cc |
21 |
| -// port: BROKER_PORT, // Default is 8443 |
22 |
| -// ssl: true/false, // Default is true |
23 |
| -// token: 'YOUR_BEARER_TOKEN' // Required! |
24 |
| -// apiUrl: 'AUTH SERVER URL', // Default is https://api2.arduino.cc |
25 |
| -// onDisconnect: message => { /* Disconnection callback */ }, |
26 |
| -// useCloudProtocolV2: true/false, // Default is false |
27 |
| -// } |
28 |
| -ArduinoCloud.connect(options).then(() => { |
29 |
| - // Connected |
30 |
| -}); |
| 22 | +If you want to learn more about Arduino IoT Cloud architecture, check the official [getting started documentation](https://www.arduino.cc/en/IoT/HomePage). |
31 | 23 |
|
32 |
| -ArduinoCloud.disconnect().then(() => { |
33 |
| - // Disconnected |
34 |
| -}); |
35 | 24 |
|
36 |
| -ArduinoCloud.subscribe(topic, cb).then(topic => { |
37 |
| - // Subscribed to topic, messaged fired in the cb |
38 |
| -}); |
39 | 25 |
|
40 |
| -ArduinoCloud.unsubscribe(topic).then(topic => { |
41 |
| - // Unsubscribed to topic |
42 |
| -}); |
| 26 | +## Installation |
43 | 27 |
|
44 |
| -ArduinoCloud.sendMessage(topic, message).then(() => { |
45 |
| - // Message sent |
46 |
| -}); |
| 28 | +```bash |
| 29 | +$ npm install arduino-iot-js |
| 30 | +``` |
47 | 31 |
|
48 |
| -ArduinoCloud.openCloudMonitor(deviceId, cb).then(topic => { |
49 |
| - // Cloud monitor messages fired to cb |
50 |
| -}); |
| 32 | +## How to use |
| 33 | +The MQTT connection over Websocket relies on Username / Password authentication. Under the hood, this module uses your user ID (plus a timestamp) as *Username* and needs a valid JWT Token as *Password*. You can use either a valid JWT token or just your API Credentials (*clientId* and *clientSecret*). |
51 | 34 |
|
52 |
| -ArduinoCloud.writeCloudMonitor(deviceId, message).then(() => { |
53 |
| - // Message sent to cloud monitor |
54 |
| -}); |
| 35 | +### How to import arduino-iot-js in your project |
| 36 | +Using a web application in the browser |
| 37 | +```javascript |
| 38 | +import { ArduinoIoTCloud } from 'arduino-iot-js' |
| 39 | +``` |
| 40 | +Using nodejs |
| 41 | +```javascript |
| 42 | +const { ArduinoIoTCloud } = require('arduino-iot-js'); |
| 43 | +``` |
55 | 44 |
|
56 |
| -ArduinoCloud.closeCloudMonitor(deviceId).then(topic => { |
57 |
| - // Close cloud monitor |
58 |
| -}); |
| 45 | +### How to connect to Arduino IoT Cloud broker using API Credentials |
| 46 | +```javascript |
| 47 | +const { ArduinoIoTCloud } = require('arduino-iot-js'); |
| 48 | + |
| 49 | +const options = { |
| 50 | + clientId: "YOUR_CLIENT_ID", |
| 51 | + clientSecret: "YOUR_CLIENT_SECRET", |
| 52 | + onDisconnect: message => { |
| 53 | + console.error(message); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +ArduinoIoTCloud.connect(options) |
| 58 | + .then(() => console.log("Connected to Arduino IoT Cloud broker")) |
| 59 | + .catch(error => console.error(error)); |
| 60 | +``` |
59 | 61 |
|
60 |
| -// Send a property value to a device |
61 |
| -// - value can be a string, a boolean or a number |
62 |
| -// - timestamp is a unix timestamp, not required |
63 |
| -ArduinoCloud.sendProperty(thingId, name, value, timestamp).then(() => { |
64 |
| - // Property value sent |
65 |
| -}); |
| 62 | +### How to listen for property value updates |
| 63 | +After a successful connection, you can listen for property updates. |
| 64 | +To do this you need: |
| 65 | +- The ID of the *Thing* the *property* belongs to. You can list all your things and properties using the [Javascript Rest API client](https://www.npmjs.com/package/@arduino/arduino-iot-client), calling the [GET Things endpoint](https://www.arduino.cc/reference/en/iot/api/index.html#api-ThingsV2-thingsV2List) |
| 66 | +- The *variable name* of the property you want to listen |
66 | 67 |
|
67 |
| -// Register a callback on a property value change |
68 |
| -// |
69 |
| -ArduinoCloud.onPropertyValue(thingId, propertyName, updateCb).then(() => { |
70 |
| - // updateCb(message) will be called every time a new value is available. Value can be string, number, or a boolean depending on the property type |
71 |
| -}); |
| 68 | +```javascript |
| 69 | +const { ArduinoIoTCloud } = require('arduino-iot-js'); |
| 70 | +const thingId = "THING_ID" |
| 71 | +const variableName = "PROPERTY_NAME" |
| 72 | + |
| 73 | +const options = { |
| 74 | + clientId: "YOUR_CLIENT_ID", |
| 75 | + clientSecret: "YOUR_CLIENT_SECRET", |
| 76 | + onDisconnect: message => { |
| 77 | + console.error(message); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +ArduinoIoTCloud.connect(options) |
| 82 | + .then(() => { |
| 83 | + console.log("Connected to Arduino IoT Cloud broker"); |
| 84 | + return ArduinoIoTCloud.onPropertyValue(thingId, variableName, showUpdates = value => console.log(value)); |
| 85 | + }) |
| 86 | + .then(() => console.log("Callback registered")) |
| 87 | + .catch(error => console.log(error)); |
| 88 | +``` |
| 89 | +Each time a new value is sent from the Device, the `counterUpdates` callback will be called. |
72 | 90 |
|
73 |
| -// Re-connect with a new authentication token, keeping the subscriptions |
74 |
| -// to the Things topics |
75 |
| -ArduinoCloud.updateToken(newToken).then(() => { |
76 |
| - // Successful reconnection with the provided new token |
| 91 | +### How to disconnect from Arduino IoT Cloud Broker |
| 92 | +```javascript |
| 93 | +ArduinoCloud.disconnect() |
| 94 | + .then(() => console.log("Successfully disconnected")); |
| 95 | +``` |
| 96 | +### How to send property values to the device |
| 97 | +To do this you need: |
| 98 | +- The ID of the *Thing* the *property* belongs to. You can list all your things and properties using the [Javascript Rest API client](https://www.npmjs.com/package/@arduino/arduino-iot-client), calling the [GET Things endpoint](https://www.arduino.cc/reference/en/iot/api/index.html#api-ThingsV2-thingsV2List) |
| 99 | +- The *variable name* of the property you want to set |
| 100 | +- Value can be either a string, a boolean or a number |
| 101 | +```javascript |
| 102 | +const { ArduinoIoTCloud } = require('arduino-iot-js'); |
| 103 | +const thingId = "THING_ID" |
| 104 | +const variableName = "PROPERTY_NAME" |
| 105 | + |
| 106 | +const options = { |
| 107 | + clientId: "YOUR_CLIENT_ID", |
| 108 | + clientSecret: "YOUR_CLIENT_SECRET", |
| 109 | + onDisconnect: message => { |
| 110 | + console.error(message); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +ArduinoIoTCloud.connect(options).then(() => { |
| 115 | + console.log("Connected to Arduino IoT Cloud broker"); |
| 116 | + ArduinoCloud.sendProperty(thingId, variableName, value).then(() => { |
| 117 | + console.log("Property value correctly sent"); |
| 118 | + }); |
77 | 119 | });
|
78 | 120 |
|
79 | 121 | ```
|
80 |
| - |
81 |
| -## Run tests |
82 |
| -First of all you need a valid Hydra Arduino token, you can get it from [Arduino Create IoT Cloud](https://create.arduino.cc/cloud/) |
83 |
| - |
84 |
| -Then you can use this token to run tests |
85 |
| - |
86 |
| -```bash |
87 |
| -$ TOKEN=YOUR_HYDRA_TOKEN_HERE npm run test |
| 122 | +### How to listen to every user properties updates |
| 123 | +```javascript |
| 124 | +const { ArduinoIoTCloud } = require('arduino-iot-js'); |
| 125 | +const ArduinoIoTApi = require('@arduino/arduino-iot-client'); |
| 126 | + |
| 127 | +const options = { |
| 128 | + clientId: "YOUR_CLIENT_ID", |
| 129 | + clientSecret: "YOUR_CLIENT_SECRET", |
| 130 | + onDisconnect: message => { |
| 131 | + console.error(message); |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +// Connect to Arduino IoT Cloud MQTT Broker |
| 136 | +ArduinoIoTCloud.connect(options) |
| 137 | + .then(() => { |
| 138 | + console.log("Connected to Arduino IoT Cloud MQTT broker"); |
| 139 | + |
| 140 | + // Init Arduino API Client |
| 141 | + const ArduinoIoTClient = ArduinoIoTApi.ApiClient.instance; |
| 142 | + ArduinoIoTClient.authentications['oauth2'].accessToken = ArduinoIoTCloud.getToken(); |
| 143 | + |
| 144 | + const thingsApi = new ArduinoIoTAPI.ThingsV2Api(ArduinoIoTClient); |
| 145 | + const propertiesAPI = new ArduinoIoTApi.PropertiesV2Api(ArduinoIoTClient); |
| 146 | + |
| 147 | + return thingsApi.thingsV2List() |
| 148 | + .then(things => { |
| 149 | + things.forEach(thing => { |
| 150 | + propertiesAPI.propertiesV2List(thing.id) |
| 151 | + .then(properties => { |
| 152 | + properties.forEach(property => { |
| 153 | + ArduinoIoTCloud.onPropertyValue(thing.id, property.variable_name, |
| 154 | + showUpdates = value => console.log(property.variable_name + ": " + value)) |
| 155 | + .then(() => console.log("Callback registered for " + property.variable_name)) |
| 156 | + .catch(error => console.error(error)); |
| 157 | + }); |
| 158 | + }) |
| 159 | + .catch(error => console.error(error)); |
| 160 | + }); |
| 161 | + }); |
| 162 | + }) |
| 163 | + .catch(error => console.error(error)); |
88 | 164 | ```
|
0 commit comments