From eedaaa0b512af05eaa7c1a51fbbdcd8a039053be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ey=C3=BCp=20S=C3=BCyr=C3=BCge?= Date: Sun, 12 Nov 2023 17:13:43 +0100 Subject: [PATCH] server modify and arduino sketch editting --- .vscode/settings.json | 1 + src/arduino/card_scanner.ino | 38 ++++++++++++++++- src/server/server.js | 79 +++++++++++++++++++++++++++++++----- 3 files changed, 106 insertions(+), 12 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/src/arduino/card_scanner.ino b/src/arduino/card_scanner.ino index 68d4473..751a92d 100644 --- a/src/arduino/card_scanner.ino +++ b/src/arduino/card_scanner.ino @@ -1,2 +1,36 @@ -void setup(){} -void loop(){} \ No newline at end of file +// imports +#include +#include + + +// Pin Setting +#define RST_PIN1 9 +#define SS_PIN1 10 + +#define RST_PIN2 5 +#define SS_PIN2 6 + +#define RST_PIN3 2 +#define SS_PIN3 3 + +// RFID-Module Instances +MFRC522 mfrc522_1(SS_PIN1, RST_PIN1); +MFRC522 mfrc522_2(SS_PIN2, RST_PIN2); +MFRC522 mfrc522_3(SS_PIN3, RST_PIN3); + +// Setup +void setup() { + // Serial Begin + Serial.begin(9600); + // SPI start + SPI.begin(); + + // Initialisation of Modules + mfrc522_1.PCD_Init(); + mfrc522_2.PCD_Init(); + mfrc522_3.PCD_Init(); +} + +void loop() { + // reading of triple signal +} diff --git a/src/server/server.js b/src/server/server.js index bae76b2..16b7681 100644 --- a/src/server/server.js +++ b/src/server/server.js @@ -3,22 +3,81 @@ const express = require('express'); const http = require('http'); const schedule = require('node-schedule'); const axios = require('axios'); +const { SerialPort } = require('serialport'); +const { ReadlineParser } = require('@serialport/parser-readline'); +const fs = require('fs'); +const cors = require('cors'); + + +// Start Express and define port const app = express(); +const serverPort = 3000; -// Shedule Format (* * * * *) (Sec(0-59), Min(0-59), Hours(0-23), Day (1-31), Month (0-11)) +// Set Cors Policy +app.use(cors()); +app.use(express.json()); +app.use(express.static('public')); +// Setting of Port of Arduino +const arduinoPort = new SerialPort({ + path: 'COM3', + baudRate: 9600 +}); -const job = schedule.scheduleJob('*/15 * * * *', () => { - const clientURL = 'http://localhost:80'; +// Server and Client information +const clientURL = 'http://localhost:80'; -axios.get(clientURL) - .then((response) => { - }) - .catch((error) => { - }); +// fetching data from Serialport: Getting Card Information which is in the middle +function waitForData() { + return new Promise((resolve) => { + const parser = arduinoPort.pipe(new ReadlineParser({ delimiter: '\r\n' })); + parser.once('data', (data) => { + resolve(data); + }); }); +} + +// fetching data from Serialport: Getting Hand Information of the Player +function waitForData(playerid) { + return "coming soon!" +} + +// Get Request for Playerhand +app.get('/cardinmyhand/:playerid', async (req, res) => { + try { + const playerid = req.params.playerid; + const data = await waitForData(playerid); // Warte auf Daten von der seriellen Schnittstelle + res.send(data); // Sende die Daten an den Client + } catch (error) { + res.status(500).send('Fehler beim Lesen der Daten'); + } +}); + +// Get Request for Card in the middle +app.get('/cardinthemiddle', async (req, res) => { + try { + const data = await waitForData(); // Warte auf Daten von der seriellen Schnittstelle + res.send(data); // Sende die Daten an den Client + } catch (error) { + res.status(500).send('Fehler beim Lesen der Daten'); + } +}); + + + + +// // Shedule Format (* * * * *) (Sec(0-59), Min(0-59), Hours(0-23), Day (1-31), Month (0-11)) +// const job = schedule.scheduleJob('*/15 * * * *', () => { + + +// axios.get(clientURL) +// .then((response) => { +// }) +// .catch((error) => { +// }); +// }); // Port Definition -app.listen(3000, () => { - console.log('Backend-Server running on port 3000...'); +app.listen(serverPort, () => { + console.log(`Server läuft auf http://localhost:${serverPort}`); });