0

Recently bought some of the LightwaveRF home automation stuff - works really well! After a bit of googling and trial and error I have a couple of PHP scripts that can control the lights!

The main script: control.php is passed a few parameters which it takes in and converts to a string to be broadcast using UDP sockets on port 9760. For example a correct string would be: "001,!R2D2F1|".

R2 = Room 2 D2 = Device 2 F1 = on (acceptables values are F1 = on, F0 = off, or a range between 2-32 for a dimming value. 32 = 100%.

This works fine, however the 'OK' message is then passed back on UDP port 9761 - I need to be able to listen for this OK, so I can chain multiple light commands. If you don't wait and send 4 straight after each other you often only get one or two of the commands actually running.

At the moment, I have a file, listen.php which listens on port 9761 and prints out the message to the screen. I can tie the two together by:

  • Running listen.php so it starts listening
  • Running control.php once listen.php is runninng
  • Sorted. Lights change and the OK is printed and listen.php stops execution.

However, I need this process to be automatic, i.e start listening, send command, wait for OK, finish all in one script - but cant figure out how to do this!

Any ideas are appreciated. Thanks!

Dave Clarke
  • 2,677
  • 4
  • 22
  • 35
  • 1
    I think you may have a problem using PHP for this. PHP is single threaded so you are going to have issues sending and receiving simultaneously. – sberry Sep 26 '12 at 23:44
  • Damn, haha! I guess I'll have to use C++ for this then? Could you provide any socket examples/simple scripts? Thanks. – Dave Clarke Sep 27 '12 at 15:17
  • Done a bit of researhc - could I not use [pcntl_fork()](http://php.net/manual/en/function.pcntl-fork.php) for this? – Dave Clarke Sep 27 '12 at 17:17

0 Answers0