-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
37 lines (33 loc) · 1019 Bytes
/
index.js
File metadata and controls
37 lines (33 loc) · 1019 Bytes
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
const express = require('express');
const mongoose = require('mongoose');
const app = express();
const port = 3000;
mongoose.connect("mongodb+srv://AnikaSterilis:[email protected]/Sensordb", {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
console.log('Connected to MongoDB');
});
const sensorSchema = {
sensorValue: String,
}
const sensor = mongoose.model('sensor', sensorSchema);
app.post('/data', (req, res) => {
let newData = new sensor ({
sensorValue: req.query.value
});
newData.save();
});
app.get('/data', (req, res) => {
const sensorValue = req.query.value;
console.log(sensorValue);
console.log(`Received sensor data: ${sensorValue}`);
// Process the sensor data as needed
res.sendStatus(200); // Send a response back to the Arduino
});
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});