Created
March 4, 2012 13:51
-
-
Save mmorey/1973091 to your computer and use it in GitHub Desktop.
Auto update @MattsOffice with current environmental conditions (temperature, luminosity)
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
<?php | |
include_once('class.twitter.php');// http://code.google.com/p/php-twitter/ | |
$ioBridgeJSONFeedURL = 'http://www.iobridge.com/api/feed/key=XXXXXXXXXXXXXXXXXXXXXX'; | |
$buzzerOnURL = 'http://yourserver.com/iobridge-proxy.php?widget=buzzeron'; | |
$buzzerOffURL = 'http://yourserver.com/iobridge-proxy.php?widget=buzzeroff'; | |
$t = new twitter; | |
$t->username = 'twitter_username'; | |
$t->password = 'twitter_password'; | |
// Read the ID of the last reply | |
$myFile = "lastreply.txt"; | |
$fileHandle = fopen($myFile, 'r'); | |
$lastReplyID = fgets($fileHandle); | |
fclose($fileHandle); | |
// Get the latest unanwsered replies | |
$data = $t->getReplies('','',$lastReplyID); | |
$counter = 1; | |
if(!empty($data)){ | |
foreach($data as $tweet) { | |
if (!empty($tweet->user->id)){ | |
// Check for valid command | |
if(stripos($tweet->text, "light"){ | |
// retrieve data from ioBridge.com | |
$curl_handle=curl_init(); | |
curl_setopt($curl_handle,CURLOPT_URL,$ioBridgeJSONFeedURL); | |
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2); | |
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1); | |
$ioBridgeResultsObject = curl_exec($curl_handle); | |
$ioBridgeResultsObject = json_decode($ioBridgeResultsObject); | |
curl_close($curl_handle); | |
$lightResults = $ioBridgeResultsObject->{'module'}->{'channels'}[2]->{'AnalogInput'}; | |
$updateText = ' @' . $tweet->user->screen_name . ' Ambient Light = ' . $lightResults; | |
} | |
// If no valid command was found reply with what commands are valid | |
else{ | |
$messageText = ' Valid commands are: light, bright, dark, temp, hot, cold, lcd, and/or view'; | |
$updateText = ' @' . $tweet->user->screen_name . $messageText; | |
} | |
// Send update | |
$updateData = $t->update($updateText, $tweet->id); | |
// Update text file with ID of last reply (first reply of loop) | |
if($counter === 1){ | |
$myFile = "lastreply.txt"; | |
$fileHandle = fopen($myFile, 'w') or die("can't open file"); | |
fwrite($fileHandle, $tweet->id); | |
fclose($fileHandle); | |
} | |
$counter++; | |
// Sound Buzzer (serves as an audible alert when someone pings @MattsOffice) | |
$curl_handle=curl_init(); | |
curl_setopt($curl_handle,CURLOPT_URL,$buzzerOnURL); | |
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,5); | |
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1); | |
$results = curl_exec($curl_handle); | |
curl_setopt($curl_handle,CURLOPT_URL,$buzzerOffURL); | |
$results = curl_exec($curl_handle); | |
curl_close($curl_handle); | |
}// if (!empty($tweet->user->id)){ | |
}// foreach($data as $tweet) { | |
}// if(!empty($data)){ | |
else { | |
echo 'All replies have been responded to.'; | |
} | |
print '<pre>Twitter says(list of @mentions):'; print_r($data); print '</pre>'; | |
print '<pre>Twitter says(response to status update):'; print_r($updateData); print '</pre>'; | |
?> |
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
<?php | |
include_once('class.twitter.php');//http://code.google.com/p/php-twitter/ | |
$ioBridgeJSONFeedURL = 'http://www.iobridge.com/api/feed/key=XXXXXXXXXXXXXXXXXX'; | |
$t = new twitter; | |
$t->username = 'twitter_username'; | |
$t->password = 'twitter_pwd'; | |
$curl_handle=curl_init(); | |
curl_setopt($curl_handle,CURLOPT_URL,$ioBridgeJSONFeedURL); | |
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2); | |
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1); | |
$ioBridgeResultsObject = curl_exec($curl_handle); | |
$ioBridgeResultsObject = json_decode($ioBridgeResultsObject); | |
curl_close($curl_handle); | |
if (empty($ioBridgeResultsObject)){ | |
print "Sorry, something went wrong."; | |
} | |
else{ | |
$data = $t->update('Temperature = '. $ioBridgeResultsObject->{'module'}->{'channels'}[0]->{'AnalogInput'} . '°F / Ambient Light = ' . $ioBridgeResultsObject->{'module'}->{'channels'}[2]->{'AnalogInput'}); | |
print '<pre>Twitter says: '; print_r($data); print '</pre>'; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment