Skip to content

Instantly share code, notes, and snippets.

@chadwallacehart
Created November 5, 2014 03:45
Show Gist options
  • Save chadwallacehart/84dd9b99188ed2daa414 to your computer and use it in GitHub Desktop.
Save chadwallacehart/84dd9b99188ed2daa414 to your computer and use it in GitHub Desktop.
/**
* Basic REST API for toggling a Tessel Relay module
* Created by chad on 8/30/2014.
*/
var router = require('tiny-router'),
relaylib = require('relay-mono'),
tessel = require('tessel');
var relay = relaylib.use(tessel.port['C']);
var lights = {
green: tessel.led[0],
blue: tessel.led[1],
red: tessel.led[2],
amber: tessel.led[3]
};
router
.get('/', function(req, res) {
res.send('Simple tessel rest API');
console.log("home");
})
.get('/relay1/{t}', function(req, res){
var t = parseInt(req.body.t);
res.send("Toggling relay 1 for " + t);
console.log("Toggling relay 1 on for " + t + " seconds");
relay.turnOn(1);
lights.green.write(1);
setTimeout(function(){
relay.turnOff(1);
lights.green.write(0);
}
, t * 1000);
})
.get('/relay2/{t}', function(req, res){
var t = parseInt(req.body.t);
res.send("Toggling relay 2 for " + t);
console.log("Toggling relay 2 on for " + t + " seconds");
relay.turnOn(2);
lights.green.write(1);
setTimeout(function(){
relay.turnOff(2);
lights.green.write(0);
}
, t * 1000);
});
//only start listening if the relay is ready
relay.on('ready', function relayReady () {
setTimeout(function(){
router.listen(8080);
console.log("listening on port 8080");
lights.blue.write(1);
},10000);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment