-
Notifications
You must be signed in to change notification settings - Fork 10
/
server.js
45 lines (35 loc) · 1013 Bytes
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
'use strict';
const express = require('express');
const rpio = require('rpio');
const app = express();
const PORT = 80;
// default: 13-close, 19-open, 11-relay
const openPin = process.env.OPEN_PIN || 19;
const closePin = process.env.CLOSE_PIN || 13;
const relayPin = process.env.RELAY_PIN || 11;
rpio.open(openPin, rpio.INPUT, rpio.PULL_UP);
rpio.open(closePin, rpio.INPUT, rpio.PULL_UP);
rpio.open(relayPin, rpio.OUTPUT, rpio.HIGH);
function getState() {
return {
open: !rpio.read(openPin),
close: !rpio.read(closePin)
}
}
app.get('/', function(req, res) {
res.render('index.ejs');
});
app.get('/status', function(req, res) {
res.send(JSON.stringify(getState()));
});
app.get('/relay', function(req, res) {
// Simulate a button press
rpio.write(relayPin, rpio.LOW);
setTimeout(function() {
rpio.write(relayPin, rpio.HIGH);
res.send('done');
}, 1000);
});
app.listen(PORT);
app.use('/assets', express.static('assets'))
console.log('Running on http://localhost:' + PORT);