-
Notifications
You must be signed in to change notification settings - Fork 6
/
mqtt2rest.js
48 lines (38 loc) · 1.16 KB
/
mqtt2rest.js
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
43
44
45
46
47
/* mqtt2rest.js */
/* mqtt init */
var mqtt = require('mqtt');
var mqtt_client = mqtt.connect('mqtt://croft.thethings.girovito.nl');
/* http init */
var https = require('https');
var options = {
host: 'api.scriptrapps.io',
path: '/ttn_test',
//port: '1337', // optional port
method: 'POST',
headers: {
Authorization: "bearer TTUyMDg1MjNCMDpzY3JpcHRyOjhCNDE0NTVERkYwNjhEMDQ2QkNBMDQwNUVGMjg3MTFG"
},
};
/* display data returned from REST service */
rest_callback = function(response) {
var str = ''
response.on('data', function (chunk) {
str += chunk;
});
response.on('end', function () {
console.log(str);
});
}
/* subscribe to some topics */
mqtt_client.on('connect', function () {
mqtt_client.subscribe('nodes/ABABCCED/packets');
mqtt_client.subscribe('gateways/008000000000ABFF/status');
});
/* forward MQTT packets to REST */
mqtt_client.on('message', function (topic, message) {
console.log("MQTT Received Packet from TTN: ",message.toString());
console.log("Pushing to REST API... ");
var req = https.request(options, rest_callback);
req.write(JSON.stringify({topic: topic, message: message.toString()}))
req.end();
});