Created
August 9, 2012 12:16
-
-
Save mrichardson23/3303761 to your computer and use it in GitHub Desktop.
Internet Connected Lamp via iDigi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
$idigi_username = 'YOUR_IDIGI_USERNAME'; // enter your username here. | |
$idigi_password = 'YOUR_IDIGI_PASSWORD'; // enter your password here. Consider your options for securing this info. | |
$device_id = '00000000-00000000-XXXXXXX-XXXXXXXX'; // XIG device ID (replace the X's with the device ID from your XIG setup screen) | |
$hardware_address = '00:13:a2:00:XX:XX:XX:XX'; // XBee's Hardware address (replace the X's so that this value matches your hardware address) | |
$idigi_sci_url = 'http://developer.idigi.com/ws/sci'; // Keep this as is. | |
// Only take action if a valid set parameter comes through (i.e. http://www.example.com/lamp.php?set=on) | |
if (isset($_GET['set']) && (($_GET['set'] == 'on') || ($_GET['set'] == 'off'))){ | |
// If the "set" parameter is "on," the value is set to 5. (ATD0 5 to set the pin high) | |
// If the "set" paremeter is "off," the value is set to 4. (ATD0 4 to set the pin low) | |
if ($_GET['set'] == 'on') { | |
$val = 5; | |
} | |
if ($_GET['set'] == 'off') { | |
$val = 4; | |
} | |
// Here's the XML command that we're sending to iDigi: | |
$data = '<sci_request version="1.0"><send_message><targets><device id="' . $device_id . '" /></targets><rci_request version="1.1"><do_command target="xig"><at hw_address="' . $hardware_address . '!" command="D0" value="' . $val . '" apply="true" /></do_command></rci_request></send_message></sci_request>'; | |
// HTTP post code with basic auth below, thanks to Jonas John: http://www.jonasjohn.de/snippets/php/post-request.htm | |
// Set up basic auth string | |
$idigi_auth = base64_encode($idigi_username . ":" . $idigi_password); | |
substr_replace($idigi_auth,"",-1); | |
// parse the given URL | |
$url = parse_url($idigi_sci_url); | |
if ($url['scheme'] != 'http') { | |
die('Error: Only HTTP request are supported !'); | |
} | |
// extract host and path: | |
$host = $url['host']; | |
$path = $url['path']; | |
// open a socket connection on port 80 - timeout: 30 sec | |
$fp = fsockopen($host, 80, $errno, $errstr, 30); | |
if ($fp){ | |
// send the request headers: | |
fputs($fp, "POST $path HTTP/1.1\r\n"); | |
fputs($fp, "Host: $host\r\n"); | |
fputs($fp, "Authorization: Basic " . $idigi_auth); | |
fputs($fp, "Content-type: text/xml; charset=\"UTF-8\"\r\n"); | |
fputs($fp, "Content-length: ". strlen($data) ."\r\n"); | |
fputs($fp, "Connection: close\r\n\r\n"); | |
fputs($fp, $data); | |
$result = ''; | |
while(!feof($fp)) { | |
// receive the results of the request | |
$result .= fgets($fp, 128); | |
} | |
} | |
// close the socket connection: | |
fclose($fp); | |
// split the result header from the content | |
$result = explode("\r\n\r\n", $result, 2); | |
$header = isset($result[0]) ? $result[0] : ''; | |
$content = isset($result[1]) ? $result[1] : ''; | |
// Echo any error responses from iDigi: | |
echo $content; | |
} | |
else { | |
echo "Please use the set parameter in the URL for on or off."; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment