-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
150 lines (131 loc) · 4.57 KB
/
app.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
const express = require("express");
const SerialPort = require("serialport").SerialPort;
const Readline = require("@serialport/parser-readline").ReadlineParser;
const sqlite3 = require("sqlite3").verbose();
require("dotenv").config();
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const twilioFromNumber = process.env.TWILIO_FROM_NUMBER;
const twilioToNumber = process.env.TWILIO_TO_NUMBER;
const client = require("twilio")(accountSid, authToken);
const app = express();
const serverPort = 3000;
app.use(express.static("public"));
const portName = "/dev/cu.usbmodem1101"; // Change this to your Arduino port
const portOptions = {
path: portName,
baudRate: 9600,
};
const serialPort = new SerialPort(portOptions);
const parser = serialPort.pipe(new Readline({ delimiter: "\n" }));
const db = new sqlite3.Database("data.db");
db.serialize(() => {
db.run(
"CREATE TABLE IF NOT EXISTS sensor_data (id INTEGER PRIMARY KEY AUTOINCREMENT, timestamp TEXT, value REAL, voltage REAL)"
);
});
parser.on("data", (data) => {
const [value, voltage] = data.split(",").map(parseFloat);
const currentDate = new Date();
const year = currentDate.getFullYear();
const month = String(currentDate.getMonth() + 1).padStart(2, "0");
const day = String(currentDate.getDate()).padStart(2, "0");
const hours = String(currentDate.getHours()).padStart(2, "0");
const minutes = String(currentDate.getMinutes()).padStart(2, "0");
const seconds = String(currentDate.getSeconds()).padStart(2, "0");
const timestamp = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
db.run(
"INSERT INTO sensor_data (timestamp, value, voltage) VALUES (?, ?, ?)",
[timestamp, value, voltage],
(err) => {
if (err) {
console.error("Error inserting data into database:", err);
} else {
handleGasSensorData(value);
console.log("Data inserted into database:", {
timestamp,
value,
voltage,
});
}
}
);
});
app.get("/", (req, res) => {
res.sendFile(__dirname + "/public/index.html");
});
app.get("/data", (req, res) => {
db.get(
"SELECT * FROM sensor_data ORDER BY timestamp DESC LIMIT 1",
(err, row) => {
if (err) {
console.error("Error querying database:", err);
res.status(500).json({ error: "Internal server error" });
} else {
res.json(row);
}
}
);
});
app.get("/highestdata", (req, res) => {
db.get(
"Select max(value) as maxvalue, max(voltage) as maxvoltage, min(value) as minvalue, min(voltage) as minvoltage from sensor_data",
(err, row) => {
if (err) {
console.error("Error querying database:", err);
res.status(500).json({ error: "Internal server error" });
} else {
res.json(row);
}
}
);
});
app.get("/valueData", (req, res) => {
db.all(
"SELECT timestamp, value FROM sensor_data ORDER BY timestamp",
(err, rows) => {
if (err) {
console.error("Error querying database:", err);
res.status(500).json({ error: "Internal server error" });
} else {
res.json(rows);
}
}
);
});
app.get("/voltageData", (req, res) => {
db.all(
"SELECT timestamp, voltage FROM sensor_data ORDER BY timestamp",
(err, rows) => {
if (err) {
console.error("Error querying database:", err);
res.status(500).json({ error: "Internal server error" });
} else {
res.json(rows);
}
}
);
});
app.listen(serverPort, () => {
console.log(`Server running at http://localhost:${serverPort}`);
});
serialPort.on("error", (err) => {
console.error("Serial port error:", err);
});
function sendSMS(message) {
client.messages
.create({
body: message,
from: twilioFromNumber, // Twilio phone number
to: twilioToNumber, // mobile phone number
})
.then((message) => console.log(`SMS sent: ${message.sid}`))
.catch((error) => console.error("Error sending SMS:", error));
}
// Function to handle gas sensor data
function handleGasSensorData(data) {
const thresholdValue = 300;
if (data > thresholdValue) {
sendSMS(`Gas sensor value (${data}) exceeds threshold!`);
}
}