-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
170 lines (149 loc) · 4.44 KB
/
server.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
const express = require('express');
let app = express();
let awsIot = require('aws-iot-device-sdk');
const fs = require("fs");
const https = require('https');
app.set('views', './views');
app.set("view engine","pug");
//serve public directory and setup for post requests
app.use(express.static('public'));
app.use(express.json());
app.get("/",function(req,res){
res.render("./home.pug",{result:"test"}); //serve home page
});
//endpoint that returns json data
app.get("/temperatureDataToday",function(req,res){
db.all(`SELECT temperature, date FROM temperature_humidity WHERE date(date) = date('now') ORDER BY date ASC;`, (err,row) => {
if (err) {
res.status(500).send(err.message);;
} else {
console.log(row);
res.send({result:row});
}
});
});
app.get("/humidityDataToday",function(req,res){
db.all(`SELECT humidity, date FROM temperature_humidity WHERE date(date) = date('now') ORDER BY date ASC;`, (err,row) => {
if (err) {
res.status(500).send(err.message);;
} else {
console.log(row);
res.send({result:row});
}
});
});
app.get("/temperatureDataYday", function(req, res) {
db.all(
`SELECT temperature, date FROM temperature_humidity WHERE date(date) = date('now', '-1 day') ORDER BY date ASC;`,
(err, row) => {
if (err) {
res.status(500).send(err.message);
} else {
console.log(row);
res.send({ result: row });
}
}
);
});
app.get("/humidityDataYday", function(req, res) {
db.all(
`SELECT humidity, date FROM temperature_humidity WHERE date(date) = date('now', '-1 day') ORDER BY date ASC;`,
(err, row) => {
if (err) {
res.status(500).send(err.message);
} else {
console.log(row);
res.send({ result: row });
}
}
);
});
app.get("/temperatureDataLweek", function(req, res) {
db.all(
`SELECT temperature, date FROM temperature_humidity WHERE date(date) BETWEEN date('now', '-7 days') AND date('now') ORDER BY date ASC;`,
(err, row) => {
if (err) {
res.status(500).send(err.message);
} else {
console.log(row);
res.send({ result: row });
}
}
);
});
app.get("/humidityDataLweek", function(req, res) {
db.all(
`SELECT humidity, date FROM temperature_humidity WHERE date(date) BETWEEN date('now', '-7 days') AND date('now') ORDER BY date ASC;`,
(err, row) => {
if (err) {
res.status(500).send(err.message);
} else {
console.log(row);
res.send({ result: row });
}
}
);
});
app.get("/statistics",function(req,res){
res.render("./stats.pug",{});
});
//temerature endpoint
app.get("/temperature",function(req,res){
//return latest temperature from database
db.all(`SELECT temperature FROM temperature_humidity ORDER BY date DESC LIMIT 1;`, (err,row) => {
if (err) {
res.status(500).send(err.message);;
} else {
res.send({result:row[0].temperature});
}
});
});
//humidity endpoint
app.get("/humidity",function(req,res){
//return latest humidity from database
db.all(`SELECT humidity FROM temperature_humidity ORDER BY date DESC LIMIT 1;`, (err,row) => {
if (err) {
res.status(500).send(err.message);;
} else {
res.send({result:row[0].humidity});
}
});
});
//start the server, connect the db
const sqlite3 = require('sqlite3').verbose();
let db = new sqlite3.Database('data.db', (err) => {
if (err) {
console.error(err.message);
}
app.listen(8080);
console.log("Server listening on port 8080");
console.log('Connected to the database.');
awsData();
});
async function awsData(){
let device = awsIot.device({
keyPath: <Your private key>,
certPath: <Your device certificate>,
caPath: <AWS access credentials>,
clientId: 'sdk-nodejs-59dada38-278b-46fe-9aa7-4a4aeebe6a02',
host: <Your AWS host IP>
});
device
.on('connect', function() {
console.log('Connected to AWS IOT Core');
device.subscribe('esp32/pub');
});
device
.on('message', function(topic, payload) {
//add to database
let message = JSON.parse(payload.toString())
//add the data to the database
db.run(`INSERT INTO temperature_humidity (temperature, humidity, date) VALUES (${message.temperature}, ${message.humidity}, datetime('now'))`, (err) => {
if (err) {
console.error(err.message);
} else {
console.log('Row inserted successfully!');
}
});
});
};