Discovers Yeelight RGBW in C# App

Hi,

I have made an app in c# to control my yeelight RGB bulb but for some reason sometimes the application can’t find the bulb.

Some days it’s working perfeclty, and sometimes not.

While the windows demo app always working.

private async void DiscoverBulbs()
{
UdpClient client = new UdpClient();

        //convert to byte array
        byte[] dgram = Encoding.ASCII.GetBytes("M-SEARCH * HTTP/1.1\r\n" +
                  "HOST: 239.255.255.250:1982\r\n" +
                  "MAN: \"ssdp:discover\"\r\n" +
                  "ST: wifi_bulb");
        //Remote endpoint "239.255.255.250:1982"
        IPEndPoint remoteEndPoint = new IPEndPoint(m_MultiCastAddress, m_Port);
        //Join multicastgroup
        client.JoinMulticastGroup(m_MultiCastAddress);
        //Send the message
        client.Send(dgram, dgram.Length, remoteEndPoint);
        //After 1000 ms timeout we stop research
        client.Client.ReceiveTimeout = 1000;
        IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);   
        
                 
        await Task.Run(() =>
        {
            try
            {
                while (true)
                {             
                    byte[] receiveBytes = client.Receive(ref RemoteIpEndPoint);
                    string message = Encoding.ASCII.GetString(receiveBytes);
                                 
                    string ip = "";
                    GetSubString(message, "Location: yeelight://", ":", ref ip);
                    //if list already contains this bulb skip
                    bool alreadyExisting = m_Bulbs.Any(item => item.Ip == ip);
                    if (alreadyExisting == false)
                    {
                        string id = "";
                        GetSubString(message, "id: ", "\r\n", ref id);
                        string bright = "";
                        GetSubString(message, "bright: ", "\r\n", ref bright);
                        string power = "";
                        GetSubString(message, "power: ", "\r\n", ref power);
                        bool isOn = power.Contains("on");
                        m_Bulbs.Add(new Bulb(ip, id, isOn, Convert.ToInt32(bright)));
                    }              
                }
            }
            catch
            {                 
                //timeout
            }
            finally
            {
                client.Close();
            }          
        });          
    }

You can find the whole file here : https://github.com/ybenannoune/YeelightController/blob/master/MainWindow.xaml.cs

I’m wondering what is the problem

Thanks for help!

I have the same exact problem. I do a reset, everything, and it still won’t work (no UDP response). Then later in the day, maybe it’ll start working again. And then it won’t. I don’t really see a pattern

Do you know Wireshark or Ethereal which can capture the traffic goes through your NIC? You may have to get some IP packages to analyze why it’s not working for your program.
If you found all the messages are being sent & received as expected, then you may inspect your code carefully and check why. If some messages are missing, then you can post your result here and we will do some analysis.

Thanks,
weiwei

I read through your code and didn’t see anything suspicious, could you try to increase the timeout value a little bit (current 1s)?

@ty_syn

Yes i have exaclty this problem, so you are using c# and your code look same?

@weiwei

Thanks for your reply, i’m not familiar with Wireshark but i will definitively give a look.
however i have tried a different approach yesterday to fix this issue (it didn’t failed for the moment will see if it’s really working)

The timeout is actually 1 second, but i have tried 10 seconds in the past, that wasn’t the problem.

@ybenannoune yes using C#. Code is similar.

I am 100% sure my issue isn’t my code though, because I’m using a packet sender program to test the Yeelight’s response too. It either consistently responds with a UDP packet or consistently does nothing.

@weiwei
Sometimes especially when I leave the office and come back in the morning it will not work anymore until much later in the day. I was thinking it could be a DHCP issue as there might be router resets at night, but as I erase data and reset the device and connect it back to the router, set it to developer mode, etc., it will still not respond to UDP sent to the multicast (however the official Yeelight app can control the lamp). It just does it randomly.

@ty_syn

Yesterday i got this issue again and i tried With my new app, 1 received one réponse + all my failed try With the other app.

So i think that the problem is fixed for me. I will commit my changes later when i’m at home. If you Can give à try and see if that solve the issue for you too :slight_smile:

i updated the repo : https://github.com/ybenannoune/YeelightController

if you can give a look, i hope it will work for you

@ybenannoune OK, that’s cool. Maybe soon I’ll create an UWP application that makes use of Cortana.