-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
53 lines (42 loc) · 1.05 KB
/
index.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
46
47
48
49
50
51
52
53
var express = require('express')
var app = express()
var bodyParser = require('body-parser')
//I|O GPIO
//var gpio = require('rpi-gpio');
var helper = require('./src/helper')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({
extended: true
}))
app.use(express.static('public'))
var server = app.listen(8000, function(){
console.log('Server started on port 8000')
})
app.post('/', function (req, res) {
var action = req.body.action
var pin = req.body.pin
var value = 0
//Control variables
var setup = false
var prevPin = 0
//If pin has been already set up we mustn't do that again.
if(prevPin != pin) {
setup = false
prevPin = pin
}
if(req.body.value === 'true' || req.body.value === 'on') value = 1
if(action === 'read') {
if(!setup) {
gpio.setup(pin, gpio.DIR_IN, helper.readInput.bind(this, pin));
setup = true
}
else helper.readInput(pin)
}
if(action === 'write') {
if(!setup) {
gpio.setup(pin, gpio.DIR_OUT, helper.write.bind(this, pin, value));
setup = true
}
else helper.write(pin, value)
}
})