-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (52 loc) · 1.66 KB
/
index.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
'use strict';
var util = require('util');
var bunyan = require('bunyan');
//var api = require('./lib/apiHelper');
//var rMQTTM = require('./lib/rMQTT');
var mqtt = require('mqtt');
//var certificates = require('./lib/certificate');
var Relayr = require('relayr');
//var rMQTT = new rMQTTM();
var app_id = 'aafaa8a6-58b5-4f8a-9485-018846e695f3';
var token = 'jpWnlsJTYBMXmyTjJIq8b4zOS2vYrWw6';
// var raw_id = 'b0b4c71d-f2a9-48cf-aed0-41e43ddef222';
//var raw_id = 'e9b5d04d-4e71-41cb-aa5d-d71cdd56fbf7';
var channel_id = '73927f8d-5135-40d0-9aa9-3837cee5e1e3';
var log = bunyan.createLogger({
name: 'got-milk'
});
log.level('debug');
// var relayr = new Relayr(app_id);
// relayr.connect(token, '73927f8d-5135-40d0-9aa9-3837cee5e1e3');
// relayr.on('data', function (topic, data) {
// log.info(topic + ':' + util.inspect(data));
// });
// // Connected to the relayr broker
// relayr.on('connect', function () {
// log.info('Connected to Relayr');
// });
log.debug('connecting');
var client = mqtt.connect({
servers: [{
'host': 'mqtt.relayr.io',
'port': 8883
}],
username: 'd4b7cc40-e399-4367-8ab6-90f165a98315',
password: 'EP6d.Q9CD9h5',
clientId: 'GotMilk!',
protocol: 'mqtts',
certPath: __dirname + '/lib/relayr.crt',
rejectUnauthorized: false
});
client.on('connect', function() {
log.info('Connected to the MQTT broker');
client.subscribe('/v1/' + channel_id + '/#');
setInterval(function() {
client.publish('/v1/' + channel_id, JSON.stringify({
weight: Math.random() * 1000
}));
}, 3000);
});
client.on('message', function(topic, message) {
log.debug(topic + ':' + message);
});