Skip to content

Instantly share code, notes, and snippets.

@jordam
Created August 13, 2015 12:59
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save jordam/483435b050aa4bc3a5f4 to your computer and use it in GitHub Desktop.
Save jordam/483435b050aa4bc3a5f4 to your computer and use it in GitHub Desktop.
A script to control MagicLight and other cheap wifi rgb lights
import socket, time
s = socket.socket()
address = ''
port = 5577
r = 0
g = 255
b = 0
keybit = "31".replace(':', '').decode('hex')
keybit += chr(r) + chr(g) + chr(b)
keybit += "00:f0:0f".replace(':', '').decode('hex')
keybit += chr(sum(bytearray(keybit))%256)
print sum(bytearray(keybit[:-1]))%256
print keybit.encode('hex')
try:
s.connect((address, port))
s.send("81:8a:8b:96".replace(':', '').decode('hex'))
s.recv(1000)
s.send("10:14:0f:08:0d:05:16:15:04:00:0f:8b".replace(':', '').decode('hex'))
s.recv(1000)
s.send(keybit)
except:
print("Could Not Connect (They are finnicy!)")
@tsauerwein
Copy link

Cool, this works! 👍

@Enteleform
Copy link

Any idea how to make this work in 3.4?

I tried replacing .decode with codecs.decode(___original_code___, 'hex'), but am still getting errors. I tried looking into it more, but the conversion process in your code is a bit over my head. I'm not quite sure what result the sequence of actions is leading to.

Current Error:

Traceback (most recent call last):
  File "C:\Users\Fico\Desktop\MagicLightTest.py", line 11, in <module>
    keybit += chr(r) + chr(g) + chr(b)
TypeError: can't concat bytes to str
[Finished in 0.2s]

Current Code:

import socket, time, codecs

s = socket.socket()
address = ''
port = 5577
r = 0
g = 255
b = 0

keybit = codecs.decode("31".replace(':', ''), 'hex')
keybit += chr(r) + chr(g) + chr(b)
keybit += codecs.decode("00:f0:0f".replace(':', ''), 'hex')
keybit += chr(sum(bytearray(keybit))%256)
print (sum(bytearray(keybit[:-1]))%256)
print (keybit.encode('hex'))
try:
        s.connect((address, port))
        s.send(codecs.decode("81:8a:8b:96".replace(':', ''), 'hex'))
        s.recv(1000)
        s.send(codecs.decode("10:14:0f:08:0d:05:16:15:04:00:0f:8b".replace(':', ''), 'hex'))
        s.recv(1000)
        s.send(keybit)
except:
        print("Could Not Connect (They are finnicy!)")

I also tried changing line 11 to:

keybit += codecs.encode(chr(r) + chr(g) + chr(b),'hex')

for which I get:

Traceback (most recent call last):
  File "C:\Python34\lib\encodings\hex_codec.py", line 15, in hex_encode
    return (binascii.b2a_hex(input), len(input))
TypeError: 'str' does not support the buffer interface

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Fico\Desktop\MagicLightTest.py", line 11, in <module>
    keybit += codecs.encode(chr(r) + chr(g) + chr(b),'hex')
TypeError: encoding with 'hex' codec failed (TypeError: 'str' does not support the buffer interface)
[Finished in 0.2s]

@drewrobb
Copy link

I found this project to be extremely helpful: https://github.com/beville/flux_led

@devdevgoat
Copy link

This worked for me! Took a while to find the IP address. My light bulb had a hostname like HF-LPB100-ZJ####, incase anyone comes here looking for how to get the ip address. I just had to try all 20 wireless clients until I found it haha.

Thanks for this!

@peterrobie
Copy link

This worked for me! Took a while to find the IP address. My light bulb had a hostname like HF-LPB100-ZJ####, incase anyone comes here looking for how to get the ip address. I just had to try all 20 wireless clients until I found it haha.

If you're looking for the IP Addr of the connected device you can look in the following location:

  1. Click on the gear icon in the top left
  2. Select device settings
  3. Select the device you want to get the information for
  4. Click Device Information
  5. Fourth item down is the IP for that device

@peterrobie
Copy link

peterrobie commented Oct 30, 2020

Also, removing lines 16 through 19 allowed the changes to the light to be almost instant. With the code listed above the delay was about a minute and caused the updates to randomly fail.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment