/* -----------------------------

TEXTING DOORMAT
Justin Smith - justin@isamaker.com
@justinIsAMaker

Listen for a pressure sensor to register over a certain number, then text me via Twilio
------------------------------ */

// Require Node libraries
var mraa = require('mraa');
var request = require('request');

//TWILIO
var twilio = require('twilio');
var client = new twilio.RestClient(
	'[ INSERT TWILIO PUBLIC KEY ]',
	'[ INSERT TWILIO PRIVATE KEY ]'
);

// Declare the sensor
var pressureSensor = new mraa.Aio(0);

// Declare the buzzer
var buzzer = new mraa.Gpio(6);
buzzer.dir(mraa.DIR_OUT);

console.log('MRAA Version: ' + mraa.getVersion());

function init(){
	checkForGuest();
}

function checkForGuest(){
	var guestPresent = pressureSensor.read();
	console.log(guestPresent);
	if(guestPresent >= 400){
		buzzer.write(1);
		console.log('PRESENT: ' + guestPresent);

		client.sms.messages.create({
			to:'[ INSERT TARGET PHONE NUMBER ]',
			from:'+1[ INSERT TWILIO PHONE NUMBER ]',
			body: 'Head\'s up, someone is outside'
		}, function(error, message){
			if(!error){
				console.log(message);
			} else {
				console.log(error);
			}
		});
		setTimeout(function(){
			buzzer.write(0);
			setTimeout(checkForGuest, 100);
		}, 5000);
	} else {
	setTimeout(checkForGuest, 100);
	}
} // END CHECK BUTTON PRESSED

init();

@@ -309,16 +309,17 @@
 t');%0A%0A//
+ 
 TWILIO%0Av
@@ -596,16 +596,58 @@
 _OUT);%0A%0A
+// Log the MRAA Version as a sanity check%0A
 console.
@@ -690,16 +690,35 @@
 on());%0A%0A
+// Set up the init%0A
 function
@@ -773,16 +773,45 @@
 uest()%7B%0A
+%09// Read the pressure sensor%0A
 %09var gue
@@ -849,80 +849,311 @@
 ();%0A
-%09console.log(guestPresent);%0A%09if(guestPresent %3E= 400)%7B%0A%09%09buzzer.write(1);
+%0A%09// DEBUG SENSOR VALUE - UNCOMMENT THIS IF YOU NEED TO%0A%09// FIGURE OUT YOUR SENSOR RANGE%0A%09// console.log(guestPresent);%0A%0A%09// Check if the value is higher than the threshold to trigger an input%0A%09if(guestPresent %3E= 400)%7B%0A%09%09// Sound the buzzer%0A%09%09buzzer.write(1);%0A%09%09// Log the level that triggered an action
 %0A%09%09c
@@ -1193,16 +1193,43 @@
 sent);%0A%0A
+%09%09// Send the text message%0A
 %09%09client
@@ -1495,16 +1495,115 @@
 %09%7D%0A%09%09%7D);
+%0A%0A%09%09// Turn the buzzer off%0A%09%09buzzer.write(0);%0A%0A%09%09// Wait for 10 seconds, then start listening again
 %0A%09%09setTi
@@ -1624,28 +1624,8 @@
 ()%7B%0A
-%09%09%09buzzer.write(0);%0A
 %09%09%09s
@@ -1664,9 +1664,10 @@
 %09%7D, 
-5
+10
 000)
@@ -1745,16 +1745,33 @@
 RESSED%0A%0A
+// Init the code%0A
 init();%0A

Add comments to the code