Skip to content

Commit 5008336

Browse files
Daniel Ballayichoi
Daniel Balla
authored andcommitted
Add ARTIK Cloud Websocket and MQTT demos (#15)
IoT.js-Samples-DCO-1.0-Signed-off-by: Daniel Balla [email protected]
1 parent b377837 commit 5008336

File tree

4 files changed

+171
-0
lines changed

4 files changed

+171
-0
lines changed

artik-cloud-mqtt/app.js

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/* Copyright 2018-present Samsung Electronics Co., Ltd. and other contributors
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
var mqtt = require('mqtt');
17+
var tls = require('tls');
18+
19+
var device_id = 'DEVICE ID';
20+
var device_token = 'DEVICE TOKEN';
21+
22+
var clientOpts = {
23+
port: 8883,
24+
username: device_id,
25+
password: device_token,
26+
keepalive: 30,
27+
};
28+
29+
function getMsg() {
30+
var temperature = Math.round(Math.random() * 100);
31+
32+
return JSON.stringify({
33+
"sdid": device_id,
34+
"ts": Date.now(),
35+
"type": "message",
36+
"data": {
37+
"temp": temperature
38+
}
39+
});
40+
}
41+
42+
var publishCounter = 0;
43+
44+
var client = mqtt.connect('mqtts://api.artik.cloud', clientOpts);
45+
46+
function keepPublishing() {
47+
setInterval(function() {
48+
client.publish('/v1.1/messages/' + device_id, getMsg());
49+
publishCounter++;
50+
51+
if (publishCounter == 10) {
52+
clearInterval(this);
53+
client.end();
54+
}
55+
}, 500);
56+
}
57+
58+
client.on('connect', function() {
59+
// Subscribe to the actions channel
60+
client.subscribe('/v1.1/actions/' + device_id);
61+
keepPublishing();
62+
});

artik-cloud-mqtt/readme.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# IoT.js MQTT Artik cloud demo
2+
This short demo demonstrates a connection to the `api.artik.cloud` server through an SSL connection.
3+
4+
## Step 1 - Initial setup
5+
Register a new account on `https://developer.artik.cloud/` and sign in.
6+
Build IoT.js with MQTT and TLS enabled (`--cmake-param=-DENABLE_MODULE_MQTT=1 --cmake-param=-DENABLE_MODULE_TLS=1`).
7+
## Step 2 - Registering your device
8+
When signed in click on the `Devices` button. This will navigate you to the section where you can manage your devices.
9+
Add a new device clicking on `Add Another device...`
10+
Select a corresponding device, or if you don't find anything suitable you can create your own type of device, by clicking on the `Developers` button in the top right corner.
11+
Having clicked the button you will see a `Dashboard` where you can see `Device types` and you can click on `+ New` to create a new type of device.
12+
Select a display name and a unique name for your device. The unique name must be in the format `com.your_company.devicename`. Having that done, you can continue, and you will be redirected to the next page where you can set your device up. After selecting the correct device, you should give it a name and you are all set.
13+
## Step 3 - Filling in the credentials
14+
Click on the device, and a popup will show up. You should generate a device token as it is needed for the authentication. This will be your `password` property in the MQTT options object.
15+
Copy your `device ID` and `device token` to the corresponding values in the JS example file, generate a client certificate and a private key for example with `OpenSSL`, and also set it in the JS file.
16+
## Additional information
17+
According to the documentation the data you send must be `JSON.stringify`-d, and should be in the following format:
18+
```js
19+
var data = {
20+
"data": {
21+
"property_name": property_value,
22+
...
23+
}
24+
}
25+
```
26+
**Note**: For some reason the Artik cloud currently only receives empty data object `{}`.
27+
28+
Only one MQTT client can be connected with the same `device id` simultaneously.
29+
A client can `publish` and/or `subscribe` to a topic.

artik-cloud-websocket/app.js

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/* Copyright 2018-present Samsung Electronics Co., Ltd. and other contributors
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
var ws = require('websocket');
17+
18+
var websocket = new ws.Websocket();
19+
20+
var device_id = "DEVICE ID";
21+
var device_token = "DEVICE TOKEN";
22+
23+
function getTimeMillis(){
24+
return parseInt(Date.now().toString());
25+
}
26+
27+
function register() {
28+
var reg_object = {
29+
type: "register",
30+
sdid: device_id,
31+
Authorization: "bearer " + device_token,
32+
cid: getTimeMillis(),
33+
};
34+
console.log('Sending register message ' + JSON.stringify(reg_object));
35+
websocket.send(JSON.stringify(reg_object), {mask: true});
36+
}
37+
38+
function sendTemp(temp) {
39+
var data_object = {
40+
sdid: device_id,
41+
ts: getTimeMillis(),
42+
data: {
43+
temp: temp,
44+
},
45+
cid: getTimeMillis(),
46+
};
47+
console.log('Sending temperature data ' + JSON.stringify(data_object));
48+
websocket.send(JSON.stringify(data_object), {mask: true});
49+
}
50+
51+
websocket.on('message', function(msg) {
52+
console.log('Received message: ' + msg);
53+
var parsed_msg = JSON.parse(msg);
54+
});
55+
56+
websocket.on('connect', function() {
57+
console.log('Websocket connection is open ....');
58+
register();
59+
sendTemp(73);
60+
});
61+
62+
websocket.connect('wss://api.artik.cloud', 443, '/v1.1/websocket?ack=true');

artik-cloud-websocket/readme.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# IoT.js WebSocket ARTIK Cloud demo
2+
3+
## Step 1 - Initial setup
4+
Register a new account on `https://developer.artik.cloud/` and sign in.
5+
Build IoT.js with WebSocket and TLS enabled (`--cmake-param=-DENABLE_MODULE_WEBSOCKET=1 --cmake-param=-DENABLE_MODULE_TLS=1`).
6+
## Step 2 - Registering your device
7+
When signed in click on the `Devices` button. This will navigate you to the section where you can manage your devices.
8+
Add a new device clicking on `Add Another device...`
9+
Select a corresponding device, or if you don't find anything suitable you can create your own type of device, by clicking on the `Developers` button in the top right corner.
10+
Having clicked the button you will see a `Dashboard` where you can see `Device types` and you can click on `+ New` to create a new type of device.
11+
Select a display name and a unique name for your device. The unique name must be in the format `com.your_company.devicename`. Having that done, you can continue, and you will be redirected to the next page where you can set your device up. After selecting the correct device, you should give it a name and you are all set.
12+
## Step 2.1 (optional) - Setting up a manifest
13+
A manifest is needed if you have your own type of device set up. It basically handles the incoming data.
14+
When sending the `data` object, its fields must be the same as set up in the `manifest`.
15+
More info on: https://developer.artik.cloud/documentation/data-management/the-manifest.html
16+
## Step 3 - Filling in the credentials
17+
Click on the device, and a popup will show up. You should generate a device token as it is needed for the authentication.
18+
Copy your `device ID` and `device token` to the corresponding values in the JS example file.

0 commit comments

Comments
 (0)