1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Xiaomi RGB Bulb simple UDP server solution

Discussion in 'Hyperion Setup Showcase' started by navigonic, 10 December 2016.

  1. navigonic

    navigonic New Member

    Messages:
    9
    Hardware:
    RPi3
    So this is to use the RGB Bulb as a cheap ambilight:

    Set the mode as udp (new implementation is what I use)
    Set it up as 1 LED (use left or right), port 19446

    then set the ip to whatever computer can run this script:.

    You'll need to "pip install yeelight"

    The script/server:
    import socket
    import time
    from yeelight import *

    #UDP SERVER
    port = 19446
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.bind(("", port))
    print("waiting on port:", port)

    #YEELIGHT
    bulb = Bulb("BULB_IP_ADDRESS")
    bulb.turn_on()
    bulb.effect = "sudden"

    #Stop/Start music mode, bypasses lamp rate limits, ensures that previous sockets close before starting
    bulb.stop_music()
    time.sleep(1)
    bulb.start_music()
    time.sleep(1)

    while True:
    d = s.recvfrom(3)
    bulb.set_rgb(*bytearray(d[0]))
     
    Last edited: 10 December 2016
  2. penfold42

    penfold42 Moderator Developer

    Messages:
    564
    Hardware:
    RPi1/Zero, RPi2, RPi3, 32/64bit, +Arduino, +nodeMCU/ESP8266
    Cool!

    Thanks for sharing
     
  3. Squizzy

    Squizzy New Member

    Messages:
    16
    Hardware:
    RPi2
    EDIT: For me it is not working well. the color change is not reliable :( is it a timing problem? or a hardware limitation?


    Original Post:
    i ve got some Exception errors. i fixed it. dont know if this is a clean solution. but it works :p


    Code:
    #!/usr/bin/python
    
    import socket
    import time
    from yeelight import *
    
    #UDP SERVER
    port = 19446
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.bind(("", port))
    print("waiting on port:", port)
    
    #YEELIGHT
    bulb = Bulb("192.168.2.105")
    bulb.turn_on()
    bulb.effect = "sudden"
    
    #Stop/Start music mode, bypasses lamp rate limits, ensures that previous sockets close before starting
    while True:
        try:
            bulb.stop_music()
            break
        except BulbException:
            break
    time.sleep(1)
    while True:
        try:
            bulb.start_music()
            break
        except BulbException:
            break
    time.sleep(1)
    
    try:
        while True:
            d = s.recvfrom(3)
            bulb.set_rgb(*bytearray(d[0]))
    except KeyboardInterrupt:
        print "Exit ..."
     
    Last edited: 14 January 2017
  4. penfold42

    penfold42 Moderator Developer

    Messages:
    564
    Hardware:
    RPi1/Zero, RPi2, RPi3, 32/64bit, +Arduino, +nodeMCU/ESP8266
    Packet loss ?

    Is it udp to the light ?

    I'm assuming you py is running on the same box as Hyperion ? If so, almost no chance of packet loss there !

    Is hyperion flooding your py script with updates ?
     
  5. Reevee

    Reevee New Member

    Messages:
    1
    Hardware:
    RPi1/Zero, RPi2, RPi3
    I would really like to add the Yeelight to my Kodi setup with Hyperion. Do you have some kind of dummy manual to achieve this, because I don't know where to start.

    My situation is:
    • Running Libreelec on a Raspberry Pi
    • Hyperion runs on this Raspberry Pi with WS2801 leds
    • A separate Yeelight wifi Bulb
    • several Raspberry Pi's if necessary...
     
    • Like Like x 1
  6. nedir

    nedir New Member

    Messages:
    1
    Hardware:
    RPi1/Zero, RPi2

    Hello Squizzy,

    You can probe if is a hardware limitation if you add this line into a loop:

    echo -ne '{"id":1,"method":"set_rgb","params":[255, "sudden", 0]}\r\n' |nc -w1 192.168.168.100 112233

    With this you send directly a command to yeelight, and changin the "255" you can change the color.

    I doesn't have yeelight bulb, but if anyone probe that is possible the rate of color change's, I buy one.