-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodelock.js
101 lines (77 loc) · 2.39 KB
/
nodelock.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
var twilio = require('twilio'),
SerialPort = new require("serialport").SerialPort,
express = require('express');
var app = express();
// Twilio Credentials
var accountSid = 'ACCOUNTSID';
var authToken = 'AUTHTOKEN';
//require the Twilio module and create a REST client
var client = require('twilio')(accountSid, authToken);
function sendMessage(res, message) {
var resp = new twilio.TwimlResponse();
resp.message(message);
res.type('text/xml');
res.send(resp.toString());
}
var serialPort = new SerialPort("COM3");
app.use(express.bodyParser());
app.post('/sms', twilio.webhook('AUTHTOKEN', { host:'fooo.ngrok.com', protocol:'http' }), function(req, res){
if (req.body.From == 'PHONENUMBER' && req.body.Body == "Unlock") {
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, 'The door is already open');
}
else {
sendMessage(res, 'ERROR');
}
});
serialPort.write("V", function(err, results) {
if (err) {
console.log('err ' + err);
}
console.log('results ' + results);
});
} else if (req.body.From == '+PHONENUMBER' && req.body.Body == "Lock"){
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, 'Door is already locked');
}
else if (data.toString().indexOf('L') > -1) {
sendMessage(res, 'Locking');
}
else {
sendMessage(res, 'ERROR');
}
});
serialPort.write("V", function(err, results) {
if (err) {
console.log('err ' + err);
}
console.log('results ' + results);
});
}
else {
console.log("Access Denied");
}
});
serialPort.on('open', function(){
// serialPort.open( function () {
app.listen(3000);
console.log('Listening on port 3000');
serialPort.on('data', function(data){
if (data.toString().indexOf('G')> -1) {
console.log("Someone at the door");
client.messages.create({
to: "PHONENUMBER",
from: "NUMBER",
body: "Someone is at the door",
}, function(err, message) {
});
}
});
});