LIFX Developer Zone

How to broadcast a Packet to devices in Python?

#1

Hi,

I am trying to use the lan protocol of LIFX . I am trying to broadcast packet to all the devices. I am not building my own packet instead using the default packet provided by the documentation that turns all the ligths green . My code is as follows

from socket import *
s=socket(AF_INET, SOCK_DGRAM,IPPROTO_UDP)
s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)

s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
print(“this is encoded data in string:”,‘35’.encode())
print(“this is encoded data from hex string:”, bytes.fromhex(‘35 23’))
s.sendto(bytes.fromhex(‘31 00 00 34 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 66 00 00 00 00 55 55 FF FF FF FF AC 0D 00 04 00 00’),(‘255.255.255.255’,56700))

s.sendto(b’\x31\x00\x00\x34\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x66\x00\x00\x00\x00\x55\x55\xFF\xFF\xFF\xFF\xAC\x0D\x00\x04\x00\x00’,(‘255.255.255.255’,56700))

But the lights don’t turn green, neither do i get a reply(I have a separate listener). Is something wrong with my code or the udp broadcast is blocked by the network. I have tried it on like 3 different networks.

#2

Hi,

Your message is wrong. You have

310000340000000000000000000000000000000000000000000000000000000066000000005555FFFFFFFFAC0D00040000
size        : 49
protocol    : 1024
addressable : True
tagged      : True
reserved1   : b'\x00'
source      : 0
target      : b'\x00\x00\x00\x00\x00\x00\x00\x00'
reserved2   : b'\x00\x00\x00\x00\x00\x00'
res_required: False
ack_required: False
reserved3   : b'\x00'
sequence    : 0
reserved4   : b'\x00\x00\x00\x00\x00\x00\x00\x00'
pkt_type    : 102
reserved5   : b'\x00\x00'
reserved6   : b'\x00'
hue         : 21845
saturation  : 65535
brightness  : 65535
kelvin      : 3500
duration    : 1024

You want

390000148e596b0c0000000000000000000000000000020100000000000000006700000000005555ffffffffac0d000000000000803f000000
size        : 57
protocol    : 1024
addressable : True
tagged      : False
reserved1   : b'\x00'
source      : 208361870
target      : b'\x00\x00\x00\x00\x00\x00\x00\x00'
reserved2   : b'\x00\x00\x00\x00\x00\x00'
res_required: False
ack_required: True
reserved3   : b'\x00'
sequence    : 1
reserved4   : b'\x00\x00\x00\x00\x00\x00\x00\x00'
pkt_type    : 103
reserved5   : b'\x00\x00'
reserved6   : b'\x00'
transient   : False
hue         : 21845
saturation  : 65535
brightness  : 65535
kelvin      : 3500
period      : 0
cycles      : 1.0
skew_ratio  : 0
waveform    : 0
#3

actually, no, your messages is correct, my packet is a SetWaveform, yours is SetColor.

I’m not sure why yours doesn’t work, I’ll have a deeper look at your code.

#4

this script works for me

import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
sock.sendto(bytes.fromhex('31 00 00 34 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 66 00 00 00 00 55 55 FF FF FF FF AC 0D 00 04 00 00'),('255.255.255.255',56700))
#5

Thank you . Now that you have confirmed that it should work . I’ll take a look and see if udp broadcast was blocked on the networks i checked on.

1 Like