commit 8103fa573d1b1bedc9bfc2637118a77e41ca1738 Author: Filip Borum Poulsen Date: Sat Dec 7 15:34:08 2024 +0100 Inital commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d5f19d8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +package-lock.json diff --git a/index.js b/index.js new file mode 100644 index 0000000..8b87116 --- /dev/null +++ b/index.js @@ -0,0 +1,69 @@ +const express = require('express'); +const app = express(); +const child_process =require('child_process'); + +app.use(express.json()); + +const port = 8080; + +app.listen(port, () => { + console.log(`Server is running at http://localhost:${port}`); +}); + +// Set the pins 5,6,13,19 as output +child_process.exec('pinctrl set 5 op'); +child_process.exec('pinctrl set 6 op'); +child_process.exec('pinctrl set 13 op'); +child_process.exec('pinctrl set 19 op'); + +app.post('/on/:gpio', function (req, res) { + + const gpio = req.params.gpio; + const command = `pinctrl set ${gpio} pn dh`; + + child_process.exec(command, function (error, stdout, stderr) { + if (error) { + console.log(error); + return res.status(500).send('Error'); + } else { + console.log('LED ON', gpio); + return res.status(200).send('LED ON'); + } + }); +}); + +app.post('/off/:gpio', function (req, res) { + + const gpio = req.params.gpio; + const command = `pinctrl set ${gpio} dl`; + + child_process.exec(command, function (error, stdout, stderr) { + if (error) { + console.log(error); + return res.status(500).send('Error'); + } else { + console.log('LED ON', gpio); + return res.status(200).send('LED ON'); + } + }); +}); + +app.get('/status/:gpio', function (req, res) { + + const gpio = req.params.gpio; + const command = `pinctrl get ${gpio}`; + const regex = /^.*\| (..) \/.*$/; + + child_process.exec(command, function (error, stdout, stderr) { + + return res.status(200).send(stdout); + + if (error) { + console.log(error); + return res.status(500).send('Error'); + } else { + console.log('Status', gpio, stdout); + return res.status(200).send(stdout); + } + }); +}); diff --git a/package.json b/package.json new file mode 100644 index 0000000..058c977 --- /dev/null +++ b/package.json @@ -0,0 +1,14 @@ +{ + "name": "remote-relay", + "version": "1.0.0", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "description": "", + "dependencies": { + "express": "^4.21.2" + } +}