Skip to content

Instantly share code, notes, and snippets.

@sidwarkd sidwarkd/www
Last active Aug 29, 2015

Embed
What would you like to do?
A Physical Yo Button
#!/usr/bin/env node
var app = require('../app');
var io = null;
app.set('port', process.env.PORT || 3000);
app.get('/new', function(req, res){
io.emit('yoReceived', req.query.username);
res.json(req.query);
});
app.post('/new', function(req, res){
io.emit('yoReceived', req.query.username);
res.json(req.query);
});
/// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
/// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
var server = app.listen(app.get('port'), function() {
debug('Express server listening on port ' + server.address().port);
});
io = require('socket.io').listen(server);
var socket = require("socket.io-client")('[url to your websocket]');
var gpio = require("gpio");
var SPI = require('node-spi');
var requestify = require('requestify');
var spi = new SPI.Spi('/dev/spidev0.0');
spi.open();
var yoCount = 0;
var connected = false;
process.on('SIGINT', function(){
console.log('Cleaning up IO pins');
spi.close();
gpio18.unexport(function(){
process.exit();
});
});
function updateDisplay(text){
var t = new Buffer(text);
var r = new Buffer(text);
var clearMsg = new Buffer([0x76]);
var clearRx = new Buffer([0x00]);
spi.transfer(clearMsg, clearRx, function(dev, buf){});
spi.transfer(t, r, function(dev, buf){});
}
var gpio18 = gpio.export(24, {direction: 'in', ready: function(){}});
// Listen for button presses
gpio18.on('change', function(value){
if (value === 1){
console.log("You pressed the button");
requestify.post('http://api.justyo.co/yoall/', {
api_token: "[your auth token]"
})
.then(function(response){
console.log("Yo sent to subscribers!");
});
}
});
socket.on('connect', function(){
console.log("Connected to server");
if(! connected){
connected = true;
socket.on('yoReceived', function(username){
// Increment the yo count and update display
console.log(username);
yoCount++;
updateDisplay(yoCount + "");
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.