Advertisement
Guest User

S20control.php

a guest
Oct 5th, 2015
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.09 KB | None | 0 0
  1. <?php
  2. // definitions for S20 device
  3. $s20_ip = "udp://192.168.1.x";
  4. $s20_port = 10000;
  5. $s20_mac = "aabbccddeeff"; // MAC adress of S20
  6. $s20_macrev = "ffeeddccbbaa"; //Reverse MAC adress of S20
  7.  
  8. if(isset($_GET["schalter"]))
  9. {
  10.         if(strcmp($_GET["schalter"], "ein") == 0)
  11.         {
  12.                         //string for switch on
  13.                         $fp = @fsockopen($s20_ip, $s20_port, $errno, $errstr);
  14.                         if (!$fp)
  15.                                 {
  16.                                 echo "<h1>ERROR:Socket connot be opened: $errstr ($errno)</h1>";
  17.                                 exit(0);
  18.                                 }
  19.                         $bytemac = hex_str2byte($s20_mac);
  20.                         fwrite($fp, "\x68\x64\x00\x17\x64\x63$bytemac\x20\x20\x20\x20\x20\x20\x00\x00\x00\x00\x01");
  21.                         fclose($fp);
  22.                 }
  23.                 else if(strcmp($_GET["schalter"], "aus") == 0)
  24.         {
  25.                         //String for switch off
  26.                         $fp = @fsockopen($s20_ip, $s20_port, $errno, $errstr);
  27.                         if (!$fp)
  28.                                 {
  29.                                 echo "<h1>ERROR:Socket connot be opened: $errstr ($errno)</h1>";
  30.                                 exit(0);
  31.                                 }
  32.                         $bytemac = hex_str2byte($s20_mac);
  33.                         fwrite($fp, "\x68\x64\x00\x17\x64\x63$bytemac\x20\x20\x20\x20\x20\x20\x00\x00\x00\x00\x00");
  34.                         fclose($fp);
  35.         }
  36.         else echo "ERROR: wrong switch setting (".htmlentities($_GET["schalter"]).") <br>";
  37. }
  38.         usleep(500000);
  39.         //open socket
  40.         $fp = @fsockopen($s20_ip, $s20_port, $errno, $errstr);
  41.         if (!$fp)
  42.         {
  43.                 echo "<h1>ERROR:SSocket connot be opened: $errstr ($errno)</h1>";
  44.                 exit(0);
  45.         }
  46.         //open listening socket
  47.         $socket = stream_socket_server("udp://0.0.0.0:10000", $errno, $errstr, STREAM_SERVER_BIND);
  48.         if (!$socket) {
  49.                 die("$errstr ($errno)");
  50.         }
  51.         //send registration
  52.         $bytemac = hex_str2byte($s20_mac);
  53.         $bytemacrev = hex_str2byte($s20_macrev);
  54.         fwrite($fp, "\x68\x64\x00\x1e\x63\x6c$bytemac\x20\x20\x20\x20\x20\x20$bytemacrev\x20\x20\x20\x20\x20\x20");
  55.         $status = 0;
  56.         $pkt = stream_socket_recvfrom($socket, 24, 0, $peer);
  57.         $status = ord(substr($pkt,23,1));
  58.  
  59. //output at browser
  60. echo "<h1>Orvibo S20 statuspage</h1>";
  61. echo "<table border=1>";
  62. echo "<tr>";
  63. echo "<td>Actual state of S20 with MAC: $s20_mac and IP: $s20_ip </td>";
  64. echo "<td>S20 state: $status </td>";
  65. if($status=="0")  echo "<td><a href=?schalter=ein><img src=schalter_aus.png border=0 height=30></a></td>";
  66. else echo "<td><a href=?schalter=aus><img src=schalter_an.png border=0 height=30></a></td>";
  67. echo "</tr>";
  68. echo "</table>";
  69.  
  70. function hex_str2byte($string)
  71. {
  72.                 $bytes = "";
  73.                 for($i = 0; $i < strlen($string); $i+=2) $bytes .= chr(hexdec(substr($string, $i, 2)));
  74.                 return $bytes;
  75. }
  76. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement