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:
    655
    Hardware:
    RPi1/Zero, RPi2, RPi3, 32/64bit, +Arduino, +nodeMCU/ESP8266
    Cool!

    Thanks for sharing
     
  3. Squizzy

    Squizzy New Member

    Messages:
    17
    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:
    655
    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.
     
  7. lemonCMS

    lemonCMS New Member

    Messages:
    1
    Hardware:
    RPi3, +Arduino, +nodeMCU/ESP8266
    Hi, i just wanted to share my version of the python script.

    Code:
    #!/usr/bin/python
    
    import socket
    import time
    import math
    from yeelight import *
    
    ip_address = "192.168.178.131"
    timeout = 200
    
    def rgb2hsv(r, g, b):
        r, g, b = r/255.0, g/255.0, b/255.0
        mx = max(r, g, b)
        mn = min(r, g, b)
        df = mx-mn
        if mx == mn:
            h = 0
        elif mx == r:
            h = (60 * ((g-b)/df) + 360) % 360
        elif mx == g:
            h = (60 * ((b-r)/df) + 120) % 360
        elif mx == b:
            h = (60 * ((r-g)/df) + 240) % 360
        if mx == 0:
            s = 0
        else:
            s = df/mx
        v = mx
        return round(h), round(s * 100, 1), round(v * 100, 1)
    
    #UDP SERVER
    port = 19446
    last = ""
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.bind(("", port))
    print("waiting on port:", port)
    
    #YEELIGHT
    bulb = Bulb(ip_address, effect="smooth", duration=timeout)
    bulb.turn_on()
    status = "on";
    # bulb.effect = "smooth"
    
    #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)
            new = bytearray(d[0])
            hsv = rgb2hsv(new[0], new[1], new[2])   
    
            if new != last:
                if hsv[2] == 0.0:
                    if status == "on":
                        bulb.turn_off()
                        status = "off"
                elif status == "off":
                    bulb = Bulb(ip_address, effect="smooth", duration=timeout)
                    while True:
                        try:
                            bulb.start_music()
                            break
                        except BulbException:
                            break
    
                    bulb.turn_on()
                    status = "on"
    
                if status == "on":
                    bulb.set_hsv(*hsv)
    
            last = new
    except KeyboardInterrupt:
        print("Exit ...")
    
     
  8. cmvac

    cmvac New Member

    Messages:
    8
    Hardware:
    RPi2, 32/64bit, Wetek
    Hi guys, are these smartbulbs working with Hyperion with the latest script? I am planning to buy some but want to be sure they work. And can we connect several, or is it limited in number? I have a Pi 3 and Hyperion, but I want, I have - i must ;):omg: - expand my hyperion setup to the rest of the living room.

    Also, I need to know how to run this script on the Pi. I know, noob question, but there are a lot of people with access to this forum who are not as versed in Python.

    In short: Do they work, can we use more than one and could we have a noob tutorial to run the script on our Pis?