-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
48 lines (40 loc) · 1023 Bytes
/
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
import express from "express"
import cors from "cors";
const app = express();
const port = 3001;
app.use(cors({
origin: 'http://localhost:5173',
credentials: true
}));
app.use(express.json());
let latestSensorData = {
SoilTemp: 25,
AirTemp: 22,
Humidity: 60,
SoilMoisture: 40,
IdealSoilTemp: 23,
IdealAirTemp: 21,
IdealHumidity: 65,
IdealSoilMoisture: 41
};
app.post('/api/sensor-data', (req, res) => {
const { SoilTemp, AirTemp, Humidity, SoilMoisture } = req.body;
latestSensorData = {
...latestSensorData,
SoilTemp,
AirTemp,
Humidity,
SoilMoisture
};
console.log('Received data from NodeMCU:', latestSensorData);
res.status(200).json({ message: 'Data received successfully' });
});
// Added 1 second delay to the GET endpoint
app.get('/api/sensor-data', (req, res) => {
setTimeout(() => {
res.json(latestSensorData);
}, 1000); // 1000 milliseconds = 1 second
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});