Created
June 23, 2011 18:43
-
-
Save aaronpk/1043248 to your computer and use it in GitHub Desktop.
x10 UDP proxy
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
#!/usr/bin/php | |
<?php | |
/* | |
* Send X10 commands to this proxy script like "!on A4" with the following function | |
* | |
* function send_x10_command($msg) | |
* { | |
* $udpstr = serialize(array('command'=>$msg)); | |
* $sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); | |
* socket_sendto($sock, $udpstr, strlen($udpstr), 0, 'example.com', 23456); | |
* } | |
* | |
*/ | |
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP) or die("Could not create socket\n"); | |
// Listen on all IPs on port 23456 | |
$listen = '0.0.0.0'; | |
$port = 23456; | |
$bind = socket_bind($socket, $listen, $port) or die("Could not bind to socket\n"); | |
$i = 1; | |
$input = ''; | |
while( 1 ) { | |
$bytes = socket_recvfrom($socket, $input, 1024, 0, $listen, $port); | |
if (trim($input) != "") { | |
$command = unserialize($input); | |
if(preg_match('/!(on|off) ([^ ]+)/', $command['command'], $match)) { | |
shell_exec('heyu ' . $match[1] . ' ' . $match[2]); | |
} | |
} | |
$i++; | |
} | |
socket_close($socket); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment