You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.6 KiB
JavaScript

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);
}
});
});