My first Spark Project -- Door buzzer

sweet! i like the android app move. that has me inspired to put an NFC tag on the front door (or maybe a bluetooth beacon in my mailbox)…so many ideas.

very excited that this little project inspired so many (twilio tweeted about this page!)

happy to open source it. i didnt write any custom firmware for the spark (since it was an extra early prototype, and the tinker firmware was sufficient)

all i did was create a twilio account and point SMS to this PHP script I whipped up (sorry if there are typos, i didnt make a separate copy of the source for my halloween costume, and didnt get to re-test).

here it is.

<?php


	// make an associative array of senders we know, indexed by phone number
	$people = array(
		"+XXXXXXXX"=>"Avidan",
		"+XXXXXXXX"=>"My Friend"
	);

	// if the sender is known, then greet them by name
	// otherwise, consider them just another guest
	if(!$name = $people[$_REQUEST['From']]) {
		$name = "Guest";
	}

	header("content-type: text/xml");
	echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
	
$key =   'XXXXXXXXXXXXXXXXXXXXXXX'; //put in your spark API key herer

if($_REQUEST['Body'] == "Open")
{
$pin = "D7";
openDoor($key, $pin); // open the door
}

function openDoor($key, $pin)
{
$result = hitTheSpark($key, $pin, "HIGH");
sleep(2);  //keep the buzzer pressed for 2 seconds
$result = hitTheSpark($key, $pin, "LOW");
}

function hitTheSpark($key, $pin, $pin_level)
{

$post_data = "access_token=$key&pin=$pin&level=$pin_level";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.sprk.io/v1/devices/avidan');  // make sure to change your URL to reflect your spark ID
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));   
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
//curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"); 
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//var_dump($ch);
$result = curl_exec($ch);
$response = curl_getinfo( $ch );
return $result;
}

?>
<Response>
	<Message>Thanks <?php echo "Welcome home " . $name . "!"; ?></Message>
</Response>
1 Like