Skip to content

Instantly share code, notes, and snippets.

@joeheyming
Created June 16, 2018 19:29
Show Gist options
  • Save joeheyming/2848b2f55de1fc94a6f040d404f9a79d to your computer and use it in GitHub Desktop.
Save joeheyming/2848b2f55de1fc94a6f040d404f9a79d to your computer and use it in GitHub Desktop.
garage door app
//Requires
const express = require('express');
const app = express();
const path = require('path');
const chalk = require('chalk');
const { exec } = require('child_process');
const morgan = require('morgan');
//Static Routes
app.use('/dist', express.static(path.join(__dirname, 'dist')));
app.use(morgan('dev')) // logging
//Main App Route
app.get('/', (req, res, next) => res.sendFile(path.join(__dirname, 'index.html')));
const port = 80;
//Run Server
app.listen(process.env.PORT || port, () => console.log(chalk.blue(`Listening intently on port ${port}`)));
const lookup = [7, 0];
const gpio = '/usr/local/bin/gpio';
function press(door) {
console.log('door: ', door);
const io = lookup[door];
console.log('io = ', io);
exec(`${gpio} write ${io} 1`, (err, stdout, stderr) => {
setTimeout(() => exec(`${gpio} write ${io} 0`), 100);
});
}
app.get('/open/:door', (req, res) => res.send('OK') && press(req.params.door));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment