-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
42 lines (35 loc) · 980 Bytes
/
index.js
File metadata and controls
42 lines (35 loc) · 980 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
var mqtt = require('mqtt')
/*
* Thingsup MQTT Service is Available on below Server
* Host: mqtt.thingsup.io
* Port: 1883(Secure MQTT)/ 8083(Secure MQTT over Websockets)
*/
var client = mqtt.connect('mqtts://mqtt.thingsup.io:1883',
{
username:"<AccountID>:<Name you specified>", //e.g. 3p309xhfhfhfhf:TestDevice1
password:"<Your MQTT Password>" //e.g. Test12345
})
/*
* All Topics Must Start with /<AccountID>/
*/
client.on('connect', function () {
console.log("Connected")
client.subscribe('/<AccountID>/Test', function (err) {
if (!err) {
client.publish('/<AccountID>/Test', 'Hello mqtt')
}
})
})
client.on('message', function (topic, message) {
// message is Buffer
console.log(message.toString())
client.end()
})
client.on('error', function (error) {
// message is Buffer
console.log(error.toString());
})
client.on('close', function (error) {
// message is Buffer
console.log("Connection Closed");
})