var express = require('express'); var app = express(); var bodyParser = require('body-parser'); var jsonParser = bodyParser.json(); var urlencodedParser = bodyParser.urlencoded({ extended: false }); var gpio = require('rpi-gpio'); gpio.setup(12, gpio.DIR_OUT, function() {}); var accountSid = 'ACCOUNT SID'; var authToken = "AUTH TOKEN"; var client = require('twilio')(accountSid, authToken); function on() { gpio.write(12, true, function(err) { if (err) throw err; console.log('Written to pin'); }); } function off() { gpio.write(12, false, function(err) { if (err) throw err; console.log('Written to pin'); }); } app.get('/', function(req, res) { res.send('Hello World!'); }); app.post('/', urlencodedParser, function(req, res) { console.log(req.body); if (req.body.Body.toLowerCase() === 'on') { console.log('turning on led'); on(); client.messages.create({ to: req.body.From, from: req.body.To, body: 'Turning LED on.' }, function(err, responseData) { if (err) { console.log(responseData.from); console.log(responseData.body); } }); } else if (req.body.Body.toLowerCase() === 'off') { console.log('turning off led'); off(); client.messages.create({ to: req.body.From, // Any number Twilio can deliver to from: req.body.To, // A number you bought from Twilio and can use for outbound communication body: 'Turning LED off.' // body of the SMS message }, function(err, responseData) { if (!err) { console.log(responseData.from); console.log(responseData.body); } }); } var twiml = ''; res.send(twiml, { 'Content-Type': 'text/xml' }, 200); }); app.listen(3000, function() { console.log('Example app listening on port 3000!'); });