Skip to content

Commit 826d0ef

Browse files
committed
node-js javascript sample
1 parent b06552e commit 826d0ef

File tree

4 files changed

+131
-0
lines changed

4 files changed

+131
-0
lines changed

node-javascript/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/node_modules
2+
package-lock.json

node-javascript/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# AWS IoT Sample Code with Node.js and Javascript
2+
3+
This repository contains a single Javascript file that will connect to AWS IoT core using the MQTTS open protocol using certificates.
4+
5+
## Pre-requisites
6+
* npm (v9.8.1+)
7+
* Node.js (v18.17.0+)
8+
* [AWS Account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html?nc2=h_ct&src=header_signup)
9+
10+
## Getting Started
11+
1. [Create an AWS IoT Core instance](https://docs.aws.amazon.com/iot/latest/developerguide/iot-gs.html#aws-iot-get-started)
12+
2. [Retrieve certificates for connection](https://docs.aws.amazon.com/iot/latest/developerguide/device-certs-create.html) and put them into this folder
13+
3. Inside `index.js` replace line 5 to 8 with your own values
14+
4. Run `node index.js` to start publishing sample data!

node-javascript/index.js

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
const mqtt = require("mqtt");
2+
const fs = require("fs");
3+
4+
// REPLACE THE FOLLOWING WITH YOUR OWN VALUES
5+
const BROKER = "YOUR BROKER ENDPOINT"// should look similar to: example-ats.iot.us-east-1å.amazonaws.com;
6+
const THING_NAME = "YOUR THING NAME"
7+
const CERT_FILE_NAME = "YOUR CERTIFICATE FILE NAME (PEM)"
8+
const PRIVATE_KEY_FILE_NAME = "YOUR PRIVATE KEY FILE NAME (PEM"
9+
10+
11+
const options = {
12+
rejectUnauthorized: false,
13+
port: 8883,
14+
host: BROKER,
15+
clientId: THING_NAME,
16+
cert: fs.readFileSync(`./${CERT_FILE_NAME}`, "utf8"),
17+
key: fs.readFileSync(`./${PRIVATE_KEY_FILE_NAME}`, "utf8"),
18+
connectTimeout: 10_000,
19+
};
20+
const client = mqtt.connect(`mqtts://${BROKER}`, options);
21+
22+
client.on("connect", () => {
23+
console.log("connected");
24+
setInterval(function() {
25+
console.log(new Date().getSeconds())
26+
client.publish(THING_NAME, JSON.stringify([
27+
{
28+
"label": "Accelerometer",
29+
"display_type": "line_graph",
30+
"unit": "meters",
31+
"values": [
32+
{
33+
"value": Math.floor(Math.random() * 101),
34+
"label": "x"
35+
},
36+
{
37+
"value": Math.floor(Math.random() * 101),
38+
"label": "y"
39+
},
40+
{
41+
"value": Math.floor(Math.random() * 101),
42+
"label": "z"
43+
}
44+
]
45+
},
46+
{
47+
"label": "Gyroscope",
48+
"display_type": "line_graph",
49+
"unit": "meters",
50+
"values": [
51+
{
52+
"value": Math.floor(Math.random() * 101),
53+
"label": "x"
54+
},
55+
{
56+
"value": Math.floor(Math.random() * 101),
57+
"label": "y"
58+
},
59+
{
60+
"value": Math.floor(Math.random() * 101),
61+
"label": "z"
62+
}
63+
]
64+
},
65+
{
66+
"label": "Battery",
67+
"display_type": "donut_graph",
68+
"unit": "%",
69+
"values": [
70+
{
71+
"value": 72,
72+
"label": "Charged"
73+
}
74+
]
75+
},
76+
{
77+
"label": "GPS",
78+
"display_type": "map",
79+
"unit": "",
80+
"values": [
81+
{
82+
"value": -122.33328,
83+
"label": "Longitude"
84+
},
85+
{
86+
"value": 47.61544,
87+
"label": "Latitude"
88+
},
89+
{
90+
"value": 118732,
91+
"label": "Accuracy (m)"
92+
}
93+
]
94+
}
95+
]));
96+
}, 1000);
97+
});
98+
99+
client.on("reconnect", () => {
100+
console.warn('reconnecting')
101+
})

node-javascript/package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "aws-iot-node-js-sample",
3+
"version": "1.0.0",
4+
"description": "Sample code for AWS IoT services with NodeJs and Javascript",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"license": "",
11+
"dependencies": {
12+
"mqtt": "^5.3.1"
13+
}
14+
}

0 commit comments

Comments
 (0)