Skip to content

Instantly share code, notes, and snippets.

@bodokaiser
Last active Aug 29, 2015
Embed
What would you like to do?
var fs = require('fs');
var http = require('http');
var express = require('express');
var app = express();
app.use(express.static(__dirname + '/../srv'));
fs.writeFile('/sys/class/gpio/export', '34', function(err) {
if (err) throw err;
app.get('/light', function(req, res, next) {
fs.readFile('/sys/class/gpio/gpio34/value', function(err, result) {
if (err) return next(err);
res.status(200);
res.format({
json: function() {
res.json({ on: Boolean(result.toString()) });
}
});
});
});
app.put('/light', function(req, res) {
var state = (res.body.on) ? '1' : '0';
fs.writeFile('/sys/class/gpio/gpio34/value', state, function(err) {
if (err) return next(err);
res.status(200);
res.format({
json: function() {
res.json();
}
});
});
});
});
app.use(express.errorHandler());
http.createServer(app).listen(8080);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment