Skip to content

Instantly share code, notes, and snippets.

@aaronpk
Created June 23, 2011 18:43
Show Gist options
  • Select an option

  • Save aaronpk/1043248 to your computer and use it in GitHub Desktop.

Select an option

Save aaronpk/1043248 to your computer and use it in GitHub Desktop.
x10 UDP proxy
#!/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