Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0203dcd

Browse files
Merge pull request #1088 from arduino/karlsoderby/iotc-nodejs-update
[IoTC] Update Node.js Connect Method
2 parents 9e13d45 + 9705b1e commit 0203dcd

File tree

1 file changed

+27
-30
lines changed

1 file changed

+27
-30
lines changed

‎content/arduino-cloud/01.getting-started/10.manual-device/manual-devices.md‎

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -220,57 +220,54 @@ Once you run the script, you will start the client and you will be able to inter
220220
The pre-requisities for connecting with Node.js is:
221221
- [Node.js](https://nodejs.org/en) installed on your machine (this is tested and confirmed to work with v20.2.0),
222222
- [arduino-iot-js](https://github.com/arduino/arduino-iot-js) installed,
223-
- a Thing created in the Arduino IoT Cloud.
223+
- a Thing created in the Arduino IoT Cloud,
224+
- a manual device created in the Arduino IoT Cloud, associated to your Thing.
224225

225-
Connection to the cloud via Node.js/Javascript requires you to first install the [arduino-iot-js](https://github.com/arduino/arduino-iot-js) package.
226+
Connection to the cloud via Node.js/Javascript requires you to first install the [arduino-iot-js](https://github.com/arduino/arduino-iot-js) package. You will also need to configure a manual device in the cloud, which will generate the **Device ID** and **Secret Key** needed to connect.
226227

227228
```sh
228229
npm install arduino-iot-js
229230
```
230231

231-
After installation, you can use the example below to connect and send variable updates to the cloud. Replace the following variables with your credentials:
232-
233-
- `THING_ID` - obtained from your Thing,
234-
- `NAME_OF_VARIABLE` - name of your variable, obtained from your Thing,
235-
- `CLIENT_ID` - obtained from [API key section](https://cloud.arduino.cc/home/api-keys),
236-
- `CLIENT_SECRET` - only obtainable during creation of your API key.
237-
232+
After installation, you can use the example below to connect and send variable updates to the cloud.
238233

239234
### JavaScript Example
240235

241-
This example connects to the cloud (MQTT broker), and sends a variable update with `sendProperty()`. The parameters of `sendProperty()` are `thingId`, `variableName` and `value`.
236+
This example connects to the cloud (MQTT broker), and sends a variable update with `sendProperty()`, and then listens for updates using the `onPropertyValue()` method.
242237

243-
***Please note: the variable name you enter in the script needs to match the variable name in the cloud.***
238+
***Please note: `cloudVar` needs to contain the variable name you create in the Arduino IoT Cloud. In this case, we are calling it `test_variable`***
244239

245240
```js
246241
const { ArduinoIoTCloud } = require('arduino-iot-js');
247-
const thingId = "THING_ID"
248-
const variableName = "NAME_OF_VARIABLE"
249-
250-
const options = {
251-
clientId: "CLIENT_ID",
252-
clientSecret: "CLIENT_SECRET",
253-
onDisconnect: message => {
254-
console.error(message);
255-
}
256-
}
257-
258-
ArduinoIoTCloud.connect(options).then(() => {
259-
console.log("Connected to Arduino IoT Cloud broker");
260-
ArduinoIoTCloud.sendProperty(thingId, variableName, 40).then(() => {
261-
console.log("Property value correctly sent: ", variableName, 40);
242+
243+
(async () => {
244+
const client = await ArduinoIoTCloud.connect({
245+
deviceId: 'YOUR_DEVICE_ID',
246+
secretKey: 'YOUR_SECRET_KEY',
247+
onDisconnect: (message) => console.error(message),
262248
});
263249

264-
});
250+
const value = 20;
251+
let cloudVar = "test_variable"
252+
253+
client.sendProperty(cloudVar, value);
254+
console.log(cloudVar, ":", value);
255+
256+
client.onPropertyValue(cloudVar, (value) => console.log(cloudVar, ":", value));
257+
})();
265258
```
266259

267-
On success, we should see the following:
260+
On successful connection, we should receive the following:
268261

269262
```sh
270-
Property value correctly sent: test_variable 40
263+
found association to thing: <thingid>
271264
```
272265

273-
This means you have successfully updated the `test_variable` with a value of `40`.
266+
And upon receiving an update (if you change the value in the dashboard), you should see:
267+
268+
```sh
269+
test_variable: <value>
270+
```
274271

275272
## Summary
276273

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /