-
-
Save kwhinnery/9819860 to your computer and use it in GitHub Desktop.
Assumes process.env.TWILIO_AUTH_TOKEN
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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