ColorChord.NET icon indicating copy to clipboard operation
ColorChord.NET copied to clipboard

Feature request: Add MQTT output

Open HepoH3 opened this issue 4 years ago • 27 comments

Hi there! Thank you for your work — I tried to use built colorchord from original repo, but it didn't work out of box while yours did. Actually, I look for a way to add music mode to my light bulbs controlled via homeassistant and I think this is what I want. The problem is, I can't figure out how to make homeassistant deal with raw UDP packets, and it will be more convinient if there would be the MQTT output which is far more usual for homeassistant. Is there a chance that it would be done?

HepoH3 avatar Jan 19 '21 15:01 HepoH3

Hello and welcome!

Currently MQTT is not supported, but I’m not against adding support for it. However, I’m concerned that it may not work very well for what you are trying to do.

The extra hop through a server will add latency, especially when using MQTT, which I don’t think is optimized for low latency. ColorChord really needs a low latency connection to the light emitters in order for the effect to look satisfying.

Are there any other ways of interacting with your home assistant software, or perhaps even the lightbulbs themselves?

I was planning on adding MQTT support to control ColorChord.NET in the future (e.g. to turn output on/off remotely).

CaiB avatar Jan 19 '21 16:01 CaiB

@CaiB, thank you for your reply! Yeah, I'm afraid about extra latency too, but it's the easiest way to do it from homeassistant. I really don't know another way except writing some sort of addon for it, where I don't have any experience. Another way to do what I want is to use python library for my yeelight bulbs. And I think I can use your memory mapped file, but I can't figure out how to deal with it. May be you can share some example?

HepoH3 avatar Jan 19 '21 17:01 HepoH3

I remember someone trying to use base ColorChord with some Yeelight bulbs a while back here: https://github.com/cnlohr/colorchord/issues/112
They mention use of a TCP server and JSON commands, could you see if this may work on your lights as well? If so, I can probably implement this pretty easily.

As for memory mapping, note that it only works on Windows. There is a reference implementation of a receiver here, but I don’t use Python so I’m not sure how it would translate: https://github.com/CaiB/ColorChord.NET/blob/master/ColorChord.NET/Visualizers/MemoryMapReceiver.cs

CaiB avatar Jan 19 '21 17:01 CaiB

Yeah, I think it would work at most of yeelight bulbs. If it would help, here is a document with yeelight API: https://www.yeelight.com/download/Yeelight_Inter-Operation_Spec.pdf

HepoH3 avatar Jan 19 '21 18:01 HepoH3

OK, I can add this, probably within the next week. Do you have a way to enable music mode (and specify a server to connect to) in the software you use, or is this something I'd need to add to CC.NET? (This would mean I'd need to add bulb discovery as well)

CaiB avatar Jan 19 '21 18:01 CaiB

Yeah just wanted to say it to you that there is such thing as music mode, and it's enabled on my bulbs. I'm not sure I can specify a server, I never seen something about it. Thank you very much!

HepoH3 avatar Jan 19 '21 18:01 HepoH3

Hello again!

My apologies for the delay. I've made a very basic implementation of the Yeelight server. Could you please give it a try?

Start by downloading the latest autobuild, 114 or newer.

Here's a sample config file excerpt that will hopefully work:

...
    "Visualizers":
    [
        {
            "Type": "Prominent",
            "Name": "YeeSample",
            "LEDCount": 1,
            "FrameRate": 30,
            "SaturationAmplifier": 1.6,
            "Enable": true
        }
    ],
    "Outputs":
    [
        {
            "Type": "Yeelight",
            "Name": "YeeOut",
            "VisualizerName": "YeeSample",
            "Enable": true
        },
        {
            "Type": "DisplayOpenGL",
            "Name": "SampleDisplay",
            "VisualizerName": "YeeSample",
            "Modes":
            [
                {
                    "Type": "BlockStrip"
                }
            ],
            "WindowWidth": 400,
            "WindowHeight": 400
        }
    ],
...

Once you start ColorChord.NET, you should get asked by Windows to allow opening a port in your firewall. Make sure to accept this, otherwise the Yeelight can't connect to the TCP server.

Then you'll need to have your Yeelight connect in "music mode" to the computer you're running ColorChord.NET on by IP. This connection will need to be made via some other app you use to control your lights now. If you can't find a way to do this, I'll need to add that to ColorChord.NET, just let me know.

The default port is 14887, but you can change this by adding a "Port": 12345 line to the Yeelight output section.

To stop the music mode, simply close ColorChord.NET and your lights should connect back to however they were configured before.

CaiB avatar Jan 26 '21 06:01 CaiB

[
        {
            "Type": "Yeelight",
            "Name": "YeeOut",
            "VisualizerName": "YeeSample",
            "Enable": true
        },
        {
            "Type": "DisplayOpenGL",
            "Name": "SampleDisplay",
            "VisualizerName": "YeeSample",
            "Modes":
            [
                {
                    "Type": "BlockStrip"
                }
            ],
            "WindowWidth": 400,
            "WindowHeight": 400
        }
    ],

Thank you very much for your work! So, I have downloaded the latest build, set up the config and enabled the port when firewall has been asked this. But I'm not sure how to connect Yeelight to my computer. Currently the computer and the Yeelight both in the same local network handled by my router. Is this enough? If that's so than something is not working, because I don't see any effect. I thought, I'll need to set the bulbs IPs into the config so the app will know how to find them. The log says following:

[INF] Reading config for Prominent "YeeSample".
[INF] Reading config for Yeelight "YeeOut"
[INF] Waiting for Yeelight clients
[INF] Reading config for DisplayOpenGL "SampleDisplay".
[WRN] Display mode "ColorChord.NET.Outputs.Display.BlockStrip" does not support configuration.
[INF] Finished reading display modes under "SampleDisplay".

HepoH3 avatar Jan 26 '21 09:01 HepoH3

Yes, getting the Yeelight to actually connect to the ColorChord.NET TCP server needs to be done separately.

I asked you previously:

Do you have a way to enable music mode (and specify a server to connect to) in the software you use, or is this something I'd need to add to CC.NET? (This would mean I'd need to add bulb discovery as well)

The Yeelight needs to initiate the connection to the server (CC.NET), not the other way around.

If you don't have a way to tell the Yeelight to initiate the connection, I'll need to add this to ColorChord.NET (which I was planning to do anyways), I was just hoping that you had a way around this for now so we could test the functionality first.

CaiB avatar Jan 26 '21 09:01 CaiB

No, sorry. The only app I have is the yeelight android app. Actually there is a yeelight toolbox running on PC, from which I can send commands to my yeelights, but I didn't found list of that commands so maybe there is a way to connect them to the PC in music mode, but I don't now that particular command.

HepoH3 avatar Jan 26 '21 09:01 HepoH3

OK, give me a bit more time and I'll implement Yeelight discovery and connection.

CaiB avatar Jan 26 '21 09:01 CaiB

Oh hold on, if you have that tool and can send raw commands, try this:

{"id":1,"method":"set_music","params":[1,"MYIP",PORT]} Replace MYIP with the IP of your PC, and PORT with the CC.NET port (14887 default)

CaiB avatar Jan 26 '21 10:01 CaiB

Oh hold on, if you have that tool and can send raw commands, try this:

{"id":1,"method":"set_music","params":[1,"MYIP",PORT]} Replace MYIP with the IP of your PC, and PORT with the CC.NET port (14887 default)

Give me a minute (somehow I don't get a notify even if I subsribed to this issue 😞)

HepoH3 avatar Jan 26 '21 10:01 HepoH3

I think it doesn't work. I tried to run: {"id":1,"method":"set_music","params":[1,"192.168.1.34",14887]}, and then start your app, but don't see any changes in bulb behaviour. Unfortunately there is no log in the toolbox so I can't say was the command completed successfuly.

HepoH3 avatar Jan 26 '21 10:01 HepoH3

You'll need to start CC.NET before you send the command.

CaiB avatar Jan 26 '21 10:01 CaiB

Oh, wait, I can see a change in your log:

[INF] Reading config for NoteFinder.
[INF] Reading config for Prominent "YeeSample".
[INF] Reading config for Yeelight "YeeOut"
[INF] Waiting for Yeelight clients
[INF] Reading config for DisplayOpenGL "SampleDisplay".
[WRN] Display mode "ColorChord.NET.Outputs.Display.BlockStrip" does not support configuration.
[INF] Finished reading display modes under "SampleDisplay".
[INF] Yeelight client connecting

The last line. It was not here before.

HepoH3 avatar Jan 26 '21 10:01 HepoH3

@HepoH3 OK, so it should theoretically be sending over the commands to turn on, then display the colours. Is the bulb still not doing anything at all?

CaiB avatar Jan 26 '21 10:01 CaiB

Nope. Tried to turn off it before start thats all, and tried to reboot the bulbs, but got nothing.

HepoH3 avatar Jan 26 '21 10:01 HepoH3

Meanwhile the Visualizer at the window is working so it's not the case that there is no sound or something like this.

HepoH3 avatar Jan 26 '21 10:01 HepoH3

@HepoH3 Gotcha, OK. It looks like the bulb is connecting correctly, just something is wrong with what I'm sending it. I'll look into it some more and also add the discovery features. I'll let you know when the new version is ready for testing.

CaiB avatar Jan 26 '21 10:01 CaiB

Okay, thank you!

HepoH3 avatar Jan 26 '21 10:01 HepoH3

@HepoH3 Actually, I just found a potential issue. Could you try it again with the newest autobuild (115)? Just use the same config.json as before.

CaiB avatar Jan 26 '21 10:01 CaiB

Give me a sec

@HepoH3 Actually, I just found a potential issue. Could you try it again with the newest autobuild (115)? Just use the same config.json as before.

HepoH3 avatar Jan 26 '21 10:01 HepoH3

yeah, it's working now!

HepoH3 avatar Jan 26 '21 10:01 HepoH3

@HepoH3 Yay, that's awesome. How is the latency? (Could you record & upload a short video?)

I'll still add the discovery & connection stuff, since using another program just to connect the bulb isn't exactly very convenient :)

I'm heading to bed now, but will let you know when I get the new features added to try out.

CaiB avatar Jan 26 '21 10:01 CaiB

https://youtu.be/4V-nyitY3NQ

HepoH3 avatar Jan 26 '21 11:01 HepoH3

There was a problem into connecting all the bulbs, but that was a toolbox problem, here the multiple bulbs on work: https://youtu.be/XO8njKKdhXk

HepoH3 avatar Jan 26 '21 11:01 HepoH3