omon23
Posts: 8
Joined: Sat Nov 30, 2013 9:44 am

PHP sending UDP request

Sun Nov 06, 2016 12:50 pm

Hello,

I have recently purchased a MiLight wifi controller for my LED strip. It works fine, and I can control my RGB LED strip with the MiLight app.

What I am trying to do, is control it from my raspberry pi. When the app 'controls' the LED strip, it sends a hex code to the WiFi controller via UDP. I managed to find some of the hex codes using the 'Packet Capture' app on my phone.

Then, using 'Packet Sender' or 'UDP sender' on my phone, I can send these hex codes to the wifi controller and it changes the colour,brightness etc. accordingly:
udp.png
udp.png (11.43 KiB) Viewed 2728 times
However, I want to send these hex code requests via UDP using PHP and I am having no luck. Whatever I do, nothing happens. Is there a proper way of sending hex codes?

Here's what I've tried (the hex code I have tried to send is 22 00):

Code: Select all

<?php
echo "test";

$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); //Create the socket
$msg = "22 00";
$len = strlen($msg);

socket_sendto($sock, $msg, $len, 0, '192.168.0.18', 8899);
socket_close($sock);
echo "test2";

?>
The 'test' statements both show, but nothing happens with my LED strip. Any help is appreciated.

Heater
Posts: 15949
Joined: Tue Jul 17, 2012 3:02 pm

Re: PHP sending UDP request

Mon Nov 07, 2016 5:00 pm

omon23,

As far as I can tell you need to send actual binary data. Two bytes containing the bit patterns of the hex values 22 00.

But you are sending the string "22 00" which is a totally different thing. In fact its the bytes we could write as 32 32 20 30 30 in HEX.

Sorry I have no idea how to deal with binary data in PHP.

Perhaps the escape sequences you have posted are what you need: "\00

Note that the character " is 22 in HEX and the \00 is an escape sequence for the byte value 00 HEX.
Last edited by Heater on Tue Nov 08, 2016 3:12 am, edited 1 time in total.
Memory in C++ is a leaky abstraction .

User avatar
DougieLawson
Posts: 39121
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: PHP sending UDP request

Mon Nov 07, 2016 7:10 pm

Which byte order is needed?

Code: Select all

<?php
$binarydata = pack("n", 0x1234);
?>
gets 0x12 0x34 big-endian

Code: Select all

<?php
$binarydata = pack("v",0x5678);
?>
gets 0x78 0x56 little-endian
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

omon23
Posts: 8
Joined: Sat Nov 30, 2013 9:44 am

Re: PHP sending UDP request

Mon Nov 07, 2016 8:52 pm

I've managed to work it out :)

Well, first of all, I made a very stupid mistake. I had named my socket variables differently, which means that it was trying to call a variable which didn't exist.

Then, I changed '$msg = "22 00";' to '$msg = hex2bin("2200");', and hey presto, it controlled my LED strip. I can change all the colours now of my led strip, turn it on/off, control the brightness etc. using the hex codes I captured from the official milight app.

So now, it is all working, but thanks for everyone's help anyway. :D

Return to “Other programming languages”