-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmqtt.js
More file actions
45 lines (40 loc) · 1.34 KB
/
Copy pathmqtt.js
File metadata and controls
45 lines (40 loc) · 1.34 KB
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
var mqtt = require('mqtt');
var Sequelize = require('sequelize');
var sequelize = new Sequelize('ss12test', 'root', 'hasitha', {
host: 'localhost',
dialect: 'mysql'
});
var options = {
clientId: 'abcd1234'
};
// client = mqtt.connect('mqtt://broker.hivemq.com:1883', options);
client = mqtt.connect('mqtt://localhost:1883', options);
client.on('connect', function () {
if (client.connected) {
console.log('mqtt device connected');
}
});
client.subscribe("ss12-tech");
client.on('close', function () {
console.log('mqtt device closed');
});
client.on('message', function (topic, message) {
console.log(message.toString());
var jsonData = JSON.parse(message);
var timeStamp = Date.now();
var temp = jsonData.temp;
var humid = jsonData.humid;
var foodStatus = jsonData.foodStatus;
var compartment_id = jsonData.compartment_id;
sequelize.query("INSERT INTO deviceData (time_stamp,temp,humid,food_status,compartment_id) VALUES (" + timeStamp + ", " + temp + ", " + humid + "," + foodStatus + ",\"" + compartment_id + "\" )", function (err) {
if (err) {
console.log("error");
} else {
console.log('succes');
// res.json(201, {response: {code: 201, message: 'Video has been added'}});
}
});
});
this.getClient = function () {
return client;
};