Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // definitions for S20 device
- $s20_ip = "udp://192.168.1.x";
- $s20_port = 10000;
- $s20_mac = "aabbccddeeff"; // MAC adress of S20
- $s20_macrev = "ffeeddccbbaa"; //Reverse MAC adress of S20
- if(isset($_GET["schalter"]))
- {
- if(strcmp($_GET["schalter"], "ein") == 0)
- {
- //string for switch on
- $fp = @fsockopen($s20_ip, $s20_port, $errno, $errstr);
- if (!$fp)
- {
- echo "<h1>ERROR:Socket connot be opened: $errstr ($errno)</h1>";
- exit(0);
- }
- $bytemac = hex_str2byte($s20_mac);
- fwrite($fp, "\x68\x64\x00\x17\x64\x63$bytemac\x20\x20\x20\x20\x20\x20\x00\x00\x00\x00\x01");
- fclose($fp);
- }
- else if(strcmp($_GET["schalter"], "aus") == 0)
- {
- //String for switch off
- $fp = @fsockopen($s20_ip, $s20_port, $errno, $errstr);
- if (!$fp)
- {
- echo "<h1>ERROR:Socket connot be opened: $errstr ($errno)</h1>";
- exit(0);
- }
- $bytemac = hex_str2byte($s20_mac);
- fwrite($fp, "\x68\x64\x00\x17\x64\x63$bytemac\x20\x20\x20\x20\x20\x20\x00\x00\x00\x00\x00");
- fclose($fp);
- }
- else echo "ERROR: wrong switch setting (".htmlentities($_GET["schalter"]).") <br>";
- }
- usleep(500000);
- //open socket
- $fp = @fsockopen($s20_ip, $s20_port, $errno, $errstr);
- if (!$fp)
- {
- echo "<h1>ERROR:SSocket connot be opened: $errstr ($errno)</h1>";
- exit(0);
- }
- //open listening socket
- $socket = stream_socket_server("udp://0.0.0.0:10000", $errno, $errstr, STREAM_SERVER_BIND);
- if (!$socket) {
- die("$errstr ($errno)");
- }
- //send registration
- $bytemac = hex_str2byte($s20_mac);
- $bytemacrev = hex_str2byte($s20_macrev);
- fwrite($fp, "\x68\x64\x00\x1e\x63\x6c$bytemac\x20\x20\x20\x20\x20\x20$bytemacrev\x20\x20\x20\x20\x20\x20");
- $status = 0;
- $pkt = stream_socket_recvfrom($socket, 24, 0, $peer);
- $status = ord(substr($pkt,23,1));
- //output at browser
- echo "<h1>Orvibo S20 statuspage</h1>";
- echo "<table border=1>";
- echo "<tr>";
- echo "<td>Actual state of S20 with MAC: $s20_mac and IP: $s20_ip </td>";
- echo "<td>S20 state: $status </td>";
- if($status=="0") echo "<td><a href=?schalter=ein><img src=schalter_aus.png border=0 height=30></a></td>";
- else echo "<td><a href=?schalter=aus><img src=schalter_an.png border=0 height=30></a></td>";
- echo "</tr>";
- echo "</table>";
- function hex_str2byte($string)
- {
- $bytes = "";
- for($i = 0; $i < strlen($string); $i+=2) $bytes .= chr(hexdec(substr($string, $i, 2)));
- return $bytes;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement