Advertisement
Guest User

Polling script for Wink Hub

a guest
May 4th, 2015
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.52 KB | None | 0 0
  1. #!/usr/bin/php
  2. <?php
  3. # Eric Tsai
  4. # 2015-04-07
  5. # run php file like this:
  6. # php myscript.php
  7. #
  8. # run in background
  9. # nohup php polling.php >&/dev/null &
  10. # 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
  11. # 3776 660 pts/2 S+ 12:55 0:00 grep --color=auto php kill -9 6343
  12. #
  13. # Assumes two reed switch items and a Lutron remote.  Edit as needed.
  14. # Assumes wink hub at 192.168.1.112, and openhab at 192.168.1.103
  15. # edit sleep timers to your own priority
  16.  
  17. date_default_timezone_set('CST');
  18.  
  19. # REST API for OpenHAB.  Post a change.
  20. # Modify IP address for OpenHAB as needed
  21. function doPostRequest($item, $data) {
  22.   $url = "http://192.168.1.103:8080/rest/items/" . $item;
  23.   $options = array(
  24.     'http' => array(
  25.         'header' => "Content-type: text/plain\r\n",
  26.         'method' => 'POST',
  27.         'content' => $data //http_build_query($data),
  28.     ),
  29.   );
  30.   $context = stream_context_create($options);
  31.   $result = file_get_contents($url, false, $context);
  32.   return $result;
  33. }//end doPostRequest
  34.  
  35.  
  36. $count = 0; $count_reed_1; $count_reed_2;
  37.  
  38. #while ($count < 25)
  39. while (true)
  40. {
  41.  
  42.   sleep(1);
  43.   if ($count %1 == 0) //run every time
  44.   #if ($count %1 == -1) //never run
  45.  {
  46.     #--- switch one ----
  47.     # "m1" is device number for first switch, modify as needed.  Also note IP address for wink hub.
  48.    $cmd = "curl \"http://192.168.1.112/set_dev_value.php\" -d \"nodeId=a&attrId=aprontest -m1 -l;\" | grep ZoneStatus | awk '{ print $9 }' ";
  49.     echo "$cmd\n";
  50.     unset($out);
  51.     exec($cmd,$out,$retval);
  52.     #echo "$item[0]: $out[0]\n";
  53.     # these values represent closed switch
  54.    if(($out[0] == 48) OR ($out[0] == 52) OR ($out[0] == 56) OR($out[0] == 568)) {
  55.       echo "Switch #1 is Closed!!!!";
  56.       doPostRequest("itm_winkpoll_trigger1", "OFF");
  57.     }
  58.     elseif (($out[0] == 49) OR ($out[0] == 53) OR ($out[0] == 57) OR($out[0] == 569)) {
  59.       echo "Switch #1 is Opened!!!!";
  60.       doPostRequest("itm_winkpoll_trigger1", "ON");
  61.     }
  62.   }//end if count divisible
  63.  
  64.  
  65.   sleep(1);
  66.  
  67.   #---- switch 2 ---------
  68.  
  69.   if ($count %1 == 0) //run every time
  70.   #if ($count %2 == -1) //do not run
  71.  {
  72.     $cmd = "curl \"http://192.168.1.112/set_dev_value.php\" -d \"nodeId=a&attrId=aprontest -m6 -l;\" | grep ZoneStatus | awk '{ print $9 }' ";
  73.     unset($out);
  74.     exec($cmd,$out,$retval);
  75.     if(($out[0] == 48) OR ($out[0] == 52) OR ($out[0] == 56) OR($out[0] == 568)) {
  76.       echo "second is it is Closed!!!!";
  77.       doPostRequest("itm_winkpoll_trigger2", "OFF");
  78.     }
  79.     elseif (($out[0] == 49) OR ($out[0] == 53) OR ($out[0] == 57) OR($out[0] == 569)) {
  80.       echo "second is it is Opened!!!!";
  81.       doPostRequest("itm_winkpoll_trigger2", "ON");
  82.     }
  83.   }//end if divisible
  84.  
  85.   sleep(1);
  86.  
  87.   #--- Lutron PICO remote
  88.  if ($count %1 == 0)
  89.   {
  90.     $cmd = "curl \"http://192.168.1.112/set_dev_value.php\" -d \"nodeId=a&attrId=aprontest -m4 -l;\" | grep Level | awk '{ print $9 }' ";
  91.     unset($out);
  92.     exec($cmd,$out,$retval);
  93.     echo "$.....\n";
  94.     echo ".....dimmer is ";
  95.     echo "$out[0]\n";
  96.     if($out[0] > 0) {
  97.       echo "remote is on";
  98.       doPostRequest("itm_winkpoll_remote", "ON");
  99.     }
  100.     if($out[0] < .1) {
  101.       echo "remote is on";
  102.       doPostRequest("itm_winkpoll_remote", "OFF");
  103.     }
  104.    
  105.   }//end if divisible
  106.  
  107.   //handle counter
  108.   $count += 1;
  109.   if ($count > 100)
  110.   {
  111.     $count = 1;
  112.   }
  113.   echo "count is ";
  114.   echo "$count\n";
  115.  
  116.  
  117.  
  118. }#//while loop end
  119. echo "done \n"; ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement