-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjaw.js
42 lines (32 loc) · 1006 Bytes
/
jaw.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
/*
run this in the terminal before running this node script
sqlite3 jaw.db
CREATE TABLE datatable (
device TEXT,
reading INT,
recorded DATETIME DEFAULT CURRENT_TIMESTAMP
);
*/
const mqtt = require('mqtt');
const sqlite = require('sqlite');
const database = 'jaw.db';
const query = 'INSERT INTO datatable (device,reading) VALUES (?,?)';
const mqttConnectionString = "mqtts://nikhil:[email protected]";
const client = mqtt.connect(mqttConnectionString);
client.on('connect', function () {
client.subscribe('jawbone/+');
});
client.on('message', async function (topic, message) {
console.log(topic, message.toString());
const device = topic.split('/')[1];
try {
const db = await sqlite.open(database, { cached: true });
const result = await db.run(query, [device,message]);
console.log(result);
} catch(err) {
console.log(err.stack)
}
});
// Make sure to change strings to integers
// SELECT MAX(CAST(reading as INTEGER))
// FROM datatable;