Turn Your Light Off Via Text: Using Particle and Twilio For Home Automation

June 18, 2015
Written by

Particle

Spark_Core_and_Twilio_Demo
There’s a sinking feeling in your stomach when you’re on your way to work and you think “Oh man, did I leave those lights on?” Krishnaraj Varma has a solution for your forgetful self, and it’s not driving all the way home. Call Twilio, enter a PIN and listen to the sweet voice confirmation that your light is being turned off.

In this Particle and Twilio hack, Krishnaraj runs you through how to leverage the ol’ internet of things to control your lights via a Twilio Voice call. Read Krishnaraj’s original post here. But first, learn a little more about Karishnaraj and how he put the hack together in this interview we did with him.

Reason for the hack:

Most of the Home Automation solutions has a Mobile Application that has to be installed on a Smartphone. I was thinking about calling a mobile number, enter a PIN and control your devices. In this case the you call a Twilio number enter a pre-configured PIN and then you can control anything that is attached to the Particle Core. This can be anything like a light, Air Conditioner, etc.

Preferred Languages:

I have been coding for 17 years, started in C/C++. Then I moved to VC++, Java and Objective C, Android, iOS. I also code in Node.js, JavaScript and C#. I love C/C++/VC++.

Closing Pre-Tutorial Words

Immediately I wanted to add a Web Interface to configure the PIN number and interface to configure different devices. Now it has only 1 for turn on a light and 0 to turn off. After that I like to add a two-factor authentication along with the PIN.

Tutorial Time: How To  Use Particle and Twilio For Home Automation

First, grab yourself a Twilio number and one of these.

<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Gather action="/twilioaction.php" method="GET">
        <Say>
          Thank you for trying Spark Core and Twilio Demo, please enter your PIN number, 
          followed by the pound sign.
        </Say>
    </Gather>
    <Say>We didn't receive any input. Goodbye!</Say>
</Response>

Now for the PHP side of things.

<!--?php 
if($_REQUEST['Digits'] == '55231' || $_REQUEST['Digits'] == '55230'){
	$url = 'https://api.spark.io/v1/devices/<<codeid-->>/digitalWrite';

	if($_REQUEST['Digits'] == '55231'){
		$data = array(
			'access_token' => '<>', 
			'params' => 'D7,HIGH'
		);
	}
	else{
		$data = array(
			'access_token' => '<>', 
			'params' => 'D7,LOW'
		);
	}

	// use key 'http' even if you send the request to https://...
	$options = array(
		'http' => array(
			'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
			'method'  => 'POST',
			'content' => http_build_query($data),
		),
	);

	$context  = stream_context_create($options);
	$result = file_get_contents($url, false, $context);

	echo "<!--?xml version=\"1.0\" encoding=\"UTF-8\"?-->\n";
	if($_REQUEST['Digits'] == '55231')
		echo "Your light will be turned on, Goodbye.";
	else
		echo "Your light will be turned off, Goodbye.";
}else{
	echo "<!--?xml version=\"1.0\" encoding=\"UTF-8\"?-->\n";
	echo "You entered " . $_REQUEST['Digits'] . ", 
	      you are not authorized to use this demo, Goodbye.";
}
?>

Here’s what your Twilio UX will look like.

Particle+Twilio1

Here’s the magic in action:

Let us know what you think and if you need any help – @twilio.