From ca0d24248c0af97368900ba3fbc4e3bc8324d62e Mon Sep 17 00:00:00 2001 From: newtbot Date: Sun, 17 Dec 2023 04:05:37 +0800 Subject: [PATCH] database presetup --- Database/mySQL.js | 20 ++++++++++++++++++++ IoT-sensor/IoT-sensor.js | 21 +++++++++------------ IoT-sensor/functions/validateData.js | 9 ++++++++- IoT-sensor/modules/coap-client.js | 24 ++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 13 deletions(-) diff --git a/Database/mySQL.js b/Database/mySQL.js index 8b13789..083d5c2 100644 --- a/Database/mySQL.js +++ b/Database/mySQL.js @@ -1 +1,21 @@ +require("dotenv").config({ path: "../../.env" }); +const Sequelize = require("sequelize"); +const sequelize = new Sequelize( + "your_database_name", + process.env. + process.env., +{ + host: '', + dialect: 'mysql' + } +); + + +sequelize.authenticate().then(() => { + console.log('Connection has been established successfully.'); +}).catch((error) => { + console.error('Unable to connect to the database: ', error); +}); + +module.exports = { sequelize }; diff --git a/IoT-sensor/IoT-sensor.js b/IoT-sensor/IoT-sensor.js index d92ef20..0d99dd2 100644 --- a/IoT-sensor/IoT-sensor.js +++ b/IoT-sensor/IoT-sensor.js @@ -10,7 +10,7 @@ /* 1) generate random data for each sensor -2) validate the data +3) send the coap request to the server */ const { isNumber } = require("./functions/validateData"); @@ -38,16 +38,6 @@ function generateRandomData() { windspeed: windspeedData.toFixed(0) + "km/h", time: currentTime, }; - /* - console.log(json.psi); - console.log(json.humidity); - console.log(json.o3); - console.log(json.no2); - console.log(json.so2); - console.log(json.temperature); - console.log(json.windspeed); - console.log(json.time); - */ return json; } @@ -55,9 +45,16 @@ function getRandomValue(min, max) { return Math.random() * (max - min) + min; } + //5 minutes setInterval(() => { var json = generateRandomData(); - isNumber(json); + console.log(json); }, 300000); +/* +setInterval(() => { + var json = generateRandomData(); + console.log(json); +}, 600); +*/ diff --git a/IoT-sensor/functions/validateData.js b/IoT-sensor/functions/validateData.js index cac41dc..5ce8f06 100644 --- a/IoT-sensor/functions/validateData.js +++ b/IoT-sensor/functions/validateData.js @@ -1,7 +1,14 @@ var validator = require('validator'); function isNumber(data) { - console.log("isNumber"); + if (validator.isNumeric(data)) + { + console.log(data); + } + else + { + console.log("Invalid data"); + } } diff --git a/IoT-sensor/modules/coap-client.js b/IoT-sensor/modules/coap-client.js index e69de29..716bf2b 100644 --- a/IoT-sensor/modules/coap-client.js +++ b/IoT-sensor/modules/coap-client.js @@ -0,0 +1,24 @@ +const coap = require('coap'); + +const serverUri = 'coap://localhost:5683'; + +// Create a CoAP request +const req = coap.request({ + hostname: 'localhost', // Replace with your server's hostname + port: 5683, // Replace with your server's port + method: 'PUT', // Use the CoAP method you need (e.g., PUT, POST) + pathname: '/resource', // Replace with your server's resource path +}); + +// Set the payload (data to be sent to the server) +const payload = 'Hello, CoAP Server!'; // Replace with your data +req.write(payload); + +// Event handler for the response +req.on('response', (res) => { + console.log('CoAP server responded with:', res.payload.toString()); + req.end(); +}); + +// Send the CoAP request +req.end();