Xiaomi RGB Bulb simple UDP server solution

  • 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]))

  • 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



  • 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...


  • 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.

  • Hi, i just wanted to share my version of the python script.


  • 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?

  • Just completed the setup process. As I am also quite noobish, it took some time.


    The Yeelights work, you can use more than 1 (I got 2) and yes, i will try to do a tutorial.


    My setup: RPi2 with latest Raspbian, LED strip with 50 LEDs behind the TV and 2 Yeelight RGB bulbs. For this tutorial it is assumed you have your simple ambilight up and running (typically some sort of LED strip behind TV controlled by hyperion). It is also assumed that the python script is run on the same raspberry as hyperion. You may have to change the ports and IPs according to your network environment.


    Below the step-by-step tutorial:


    -Locate your hyperion folder where your config is. For me it's /etc/hyperion


    -Add as many hyperion configs as you have Yeelight RGB bulbs, use udp device (for each config a different port!):

    Code
    "device" :
       {
         "name"  : "MyPiudp",
         "type"  : "udp",
         "output"  : "127.0.0.1:20450", "protocol" : 0,
         "rate"  : 1000000,
         "colorOrder" : "rgb"
       },


    -In each config, setup a single LED (example below is for my Yeelight which is to the left of the tv)

    Code
    "leds" :
        [
            {
                "index" : 0,
                "hscan" : { "minimum" : 0.0000, "maximum" : 0.1000 },
                "vscan" : { "minimum" : 0.0000, "maximum" : 1.0000 }
            }
        ],


    -In each config, use different ports for the jsonServer and the protoSever and write them down as you will need them in the next step. Then you can add color transformation, smoothing etc. You don't need grabbers in your yeelight configs, because they will use the grabber of your "main" config.


    -In your main config, add forwarder for json and proto. Example below is for two yeelight configs.

    Code
    "forwarder" :
        {
        "json" : [ "127.0.0.1:19450", "127.0.0.1:19452" ],
        "proto" : [ "127.0.0.1:19451", "127.0.0.1:19453" ]
        },


    -Add your configs to hyperion: Multiple Hyperion Instances


    -In the yeelight android app, activate LAN-control for each yeelight you want to use


    -Create a new python file, for example /home/pi/yee.py. I use the script by lemonCMS: Xiaomi RGB Bulb simple UDP server solution Pay attention to the port in the script. In my example you would have to use 20450.


    -Open a terminal and type

    Code
    pip install yeelight


    -Start the python script by typing

    Code
    python yee.py


    I think that's all you have to do. If you have any questions, feel free to ask.

  • Hi, I just got my two Yeelights, it took some time to get here from China ;). I am using LibreELEC with Hyperion. I know where the config file is - in the .config folder - but I am having trouble in finding the hyperion.service file. It is in a different folder then OSMC and Raspbian. Does anyone know where that file is in LibreELEC?

  • Houston, we have a problem: pip is not available in LibreELEC, I forgot that this OS is stripped of almost all Linux shell commands. I am not a newbie, but I never really had to fiddle with that before. Does anybody know any alternative way to get this to work with LibreELEC? Or do I need to scrap my LibreELEC system and start fresh with OSMC or Raspbian?


  • I've been fidling with the YeeMee addon for Kodi (https://forum.kodi.tv/showthre…97&pid=2687134#pid2687134) but it has (at least for me) a huge lag. Is there any lag with Hyperion and the Yeelights? I need to know this before I scratch my LibreELEC instalation and install Raspbian.


    Also, would there be any wiz who could port this to LibreELEC, for all of us who use that OS? I really don't use or need Raspbian. It would really be amazing to have these cool and cheap lights working with all Hyperion compatible systems.

  • As the Yeelights use WLAN, the lag is larger than driving an ambilight by wire over the gpio pins of a RPi. For me the lag is far from huge, but its slightly noticeable. Of course the lag depends on your network conditions and the hyperion config (smoothing etc) you use.


    Can't help with the LibreELEC issue. I use Raspbian (with Kodi) because i need a full-featured OS.

  • Thanks for thw work lemonCMS and soulsick. Somebody should make good wiki article out of it ;)
    Quick question: Why is black on my TV white on my yeelight? All other colors are working great!

  • Thanks, I didn't copy the transform block from my main config. The gamma values fixed the problem for me! Quick demo:

    Externer Inhalt www.youtube.com
    Inhalte von externen Seiten werden ohne Ihre Zustimmung nicht automatisch geladen und angezeigt.
    Durch die Aktivierung der externen Inhalte erklären Sie sich damit einverstanden, dass personenbezogene Daten an Drittplattformen übermittelt werden. Mehr Informationen dazu haben wir in unserer Datenschutzerklärung zur Verfügung gestellt.


    I asked lemonCMS to add the code to his Github Account: https://github.com/lemonCMS/hyperion_udp_yeelight
    Could be good place to submit pull requests :)

  • As the Yeelights use WLAN, the lag is larger than driving an ambilight by wire over the gpio pins of a RPi. For me the lag is far from huge, but its slightly noticeable. Of course the lag depends on your network conditions and the hyperion config (smoothing etc) you use.


    Can't help with the LibreELEC issue. I use Raspbian (with Kodi) because i need a full-featured OS.



    So, I gave up on LibreELEC, and installed this with Pipplware (http://pipplware.pplware.pt). It all went fine until I got to the "python yee.py" part. I get the following error:


    pi@pipplware:~ $ python yee.py
    ('waiting on port:', 19450)
    Traceback (most recent call last):
    File "yee.py", line 40, in <module>
    bulb.turn_on()
    File "<decorator-gen-7>", line 2, in turn_on
    File "/usr/local/lib/python2.7/dist-packages/yeelight/main.py", line 53, in _command
    result = self.send_command(method, params).get("result", [])
    File "/usr/local/lib/python2.7/dist-packages/yeelight/main.py", line 277, in send_command
    raise_from(BulbException('A socket error occurred when sending the command.'), ex)
    File "/usr/local/lib/python2.7/dist-packages/future/utils/__init__.py", line 4
    54, in raise_from
    raise e
    yeelight.main.BulbException: A socket error occurred when sending the command.


    The UDP port is 20450, as you advised. What am I getting wrong?

Jetzt mitmachen!

Sie haben noch kein Benutzerkonto auf unserer Seite? Registrieren Sie sich kostenlos und nehmen Sie an unserer Community teil!