Skip to content

Instantly share code, notes, and snippets.

@kwhinnery
Forked from jonmarkgo/nodelock.js
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kwhinnery/9819860 to your computer and use it in GitHub Desktop.
Save kwhinnery/9819860 to your computer and use it in GitHub Desktop.
Assumes process.env.TWILIO_AUTH_TOKEN
var twilio = require('twilio'),
SerialPort = require("serialport").SerialPort,
express = require('express');
var app = express();
function sendMessage(res, message) {
var resp = new twilio.TwimlResponse();
resp.message(message);
res.type('text/xml');
res.send(resp.toString());
}
var serialPort = new SerialPort("/dev/tty.usbmodem1411", {
baudrate: 9600
});
app.use(express.bodyParser());
// Include twilio webhook middleware with host and protocol used for number config
app.post('/sms', twilio.webhook({
host: 'foobar.herokuapp.com',
protocol: 'https'
}), function(req, res){
if (req.body.From == "+12128675309") {
console.log("verified number!");
serialPort.once('data', function(data) {
if (data.toString().indexOf('U') > -1) { //check if the Arduino returned a U for unlocking
sendMessage(res, 'Unlocking!');
}
else if (data.toString().indexOf('L') > -1) {
sendMessage(res, 'Locking!');
}
else {
sendMessage(res, 'ERROR');
}
console.log('data received: ' + data);
});
serialPort.write("V", function(err, results) {
if (err) {
console.log('err ' + err);
}
console.log('results ' + results);
});
} else {
console.log("Wrong number!");
sendMessage(res, "Invalid number!");
}
});
serialPort.open( function () {
app.listen(3000);
console.log('Listening on port 3000');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment