Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/php
- <?php
- # Eric Tsai
- # 2015-04-07
- # run php file like this:
- # php myscript.php
- #
- # run in background
- # nohup php polling.php >&/dev/null &
- # kill polling script if running in background ps aux | grep php //root 6343 0.1 0.6 17956 5424 ? S 12:54 0:00 php polling.php //root 6470 0.0 0.0
- # 3776 660 pts/2 S+ 12:55 0:00 grep --color=auto php kill -9 6343
- #
- # Assumes two reed switch items and a Lutron remote. Edit as needed.
- # Assumes wink hub at 192.168.1.112, and openhab at 192.168.1.103
- # edit sleep timers to your own priority
- date_default_timezone_set('CST');
- # REST API for OpenHAB. Post a change.
- # Modify IP address for OpenHAB as needed
- function doPostRequest($item, $data) {
- $url = "http://192.168.1.103:8080/rest/items/" . $item;
- $options = array(
- 'http' => array(
- 'header' => "Content-type: text/plain\r\n",
- 'method' => 'POST',
- 'content' => $data //http_build_query($data),
- ),
- );
- $context = stream_context_create($options);
- $result = file_get_contents($url, false, $context);
- return $result;
- }//end doPostRequest
- $count = 0; $count_reed_1; $count_reed_2;
- #while ($count < 25)
- while (true)
- {
- sleep(1);
- if ($count %1 == 0) //run every time
- #if ($count %1 == -1) //never run
- {
- #--- switch one ----
- # "m1" is device number for first switch, modify as needed. Also note IP address for wink hub.
- $cmd = "curl \"http://192.168.1.112/set_dev_value.php\" -d \"nodeId=a&attrId=aprontest -m1 -l;\" | grep ZoneStatus | awk '{ print $9 }' ";
- echo "$cmd\n";
- unset($out);
- exec($cmd,$out,$retval);
- #echo "$item[0]: $out[0]\n";
- # these values represent closed switch
- if(($out[0] == 48) OR ($out[0] == 52) OR ($out[0] == 56) OR($out[0] == 568)) {
- echo "Switch #1 is Closed!!!!";
- doPostRequest("itm_winkpoll_trigger1", "OFF");
- }
- elseif (($out[0] == 49) OR ($out[0] == 53) OR ($out[0] == 57) OR($out[0] == 569)) {
- echo "Switch #1 is Opened!!!!";
- doPostRequest("itm_winkpoll_trigger1", "ON");
- }
- }//end if count divisible
- sleep(1);
- #---- switch 2 ---------
- if ($count %1 == 0) //run every time
- #if ($count %2 == -1) //do not run
- {
- $cmd = "curl \"http://192.168.1.112/set_dev_value.php\" -d \"nodeId=a&attrId=aprontest -m6 -l;\" | grep ZoneStatus | awk '{ print $9 }' ";
- unset($out);
- exec($cmd,$out,$retval);
- if(($out[0] == 48) OR ($out[0] == 52) OR ($out[0] == 56) OR($out[0] == 568)) {
- echo "second is it is Closed!!!!";
- doPostRequest("itm_winkpoll_trigger2", "OFF");
- }
- elseif (($out[0] == 49) OR ($out[0] == 53) OR ($out[0] == 57) OR($out[0] == 569)) {
- echo "second is it is Opened!!!!";
- doPostRequest("itm_winkpoll_trigger2", "ON");
- }
- }//end if divisible
- sleep(1);
- #--- Lutron PICO remote
- if ($count %1 == 0)
- {
- $cmd = "curl \"http://192.168.1.112/set_dev_value.php\" -d \"nodeId=a&attrId=aprontest -m4 -l;\" | grep Level | awk '{ print $9 }' ";
- unset($out);
- exec($cmd,$out,$retval);
- echo "$.....\n";
- echo ".....dimmer is ";
- echo "$out[0]\n";
- if($out[0] > 0) {
- echo "remote is on";
- doPostRequest("itm_winkpoll_remote", "ON");
- }
- if($out[0] < .1) {
- echo "remote is on";
- doPostRequest("itm_winkpoll_remote", "OFF");
- }
- }//end if divisible
- //handle counter
- $count += 1;
- if ($count > 100)
- {
- $count = 1;
- }
- echo "count is ";
- echo "$count\n";
- }#//while loop end
- echo "done \n"; ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement