TobKra96 / music_led_strip_control

Audio visualization for LED strips in real-time with web interface on a raspberry pi.
https://tobkra96.github.io/music_led_strip_control/
MIT License
297 stars 64 forks source link

Set effect on group of devices via REST API #138

Closed fherbet closed 2 years ago

fherbet commented 2 years ago

Hi !

1st of all, thanks a ton for this wonderful project ! i have now a main ledstrip driven by a pi3 + 2nd one connected to NodeMCU that are on both side of my sofa and both hidden in vertical lamps! vu meter effect really rocks while listening to music!

i'm now planning to build many more NodeMCU satellites for other rooms !

currently , i'm trying to integrate this into my domotic station (jeedom) via python scripts and requests library to interact with MLSC REST API.

all works fine except that i'm trying to find a way to set same effect on 2 devices at the same time (and not all devices because they're not in same room.)

obv when i use this code, both devices acts in a synchronized way

def seteffect(data):
    r = requests.post(url + '/api/effect/active', json=data)

# off for all devices
elif sys.argv[1] == ('off2') :
    jsondata={'effect': 'effect_off'}
    seteffect(jsondata)

# gradient for all devices
elif sys.argv[1] == ('gradient2') :
    jsondata={'effect': 'effect_gradient'}
    seteffect(jsondata)

when i use that one, i get a delay between both 'seteffect'

if sys.argv[1] == ('off') :
    jsondata={"device": "device_0","effect": "effect_off"}
    jsondata2={"device": "device_1","effect": "effect_off"}
    seteffect(jsondata)
    seteffect(jsondata2)

elif sys.argv[1] == ('gradient') :
    jsondata={"device": "device_0","effect": "effect_gradient"}
    jsondata2={"device": "device_1","effect": "effect_gradient"}
    seteffect(jsondata)
    seteffect(jsondata2)

looks like it's not possible to have a single json object to push effect on several devices like json object we get when we retrieve devices effect > GET /api​/effect​/active > Return active effect

{
  "devices": [
    {
      "device": "device_0",
      "effect": "effect_off"
    },
    {
      "device": "device_1",
      "effect": "effect_off"
    }
  ]
}

Am I wrong ? or i just missed something ?

thanks very much !

Teraskull commented 2 years ago

Hi @fherbet,

Thanks for your feedback! I just pushed a commit to allow setting effects for multiple devices via the API. Please use the branch https://github.com/TobKra96/music_led_strip_control/tree/dev_2.3 and test if your strips won't have the delay.

The body for the API call should be like in this example:

{
  "devices": [
    {
      "device": "device_0",
      "effect": "effect_single"
    },
    {
      "device": "device_1",
      "effect": "effect_gradient"
    }
  ]
}

I will also update the API documentation with more examples later.

TobKra96 commented 2 years ago

Merged into branch dev_2.3. Thank you 👍

fherbet commented 2 years ago

thanks guys !

i have tried updating MLSC using below command :

curl -sSL https://raw.githubusercontent.com/TobKra96/music_led_strip_control/master/setup.sh | sudo bash -s -- -b dev_2.3

but i'm still running stable 2.2 (master)

what would you suggest to run MLSC branch dev_2.3 ?

thanks !

Edit :

looks like i did it with

root@ledstrip:/share/music_led_strip_control# git branch dev_2.3
root@ledstrip:/share/music_led_strip_control# git pull origin dev_2.3
From https://github.com/TobKra96/music_led_strip_control
 * branch            dev_2.3    -> FETCH_HEAD

then

pi@ledstrip:~ $ sudo systemctl stop mlsc.service
pi@ledstrip:~ $ sudo systemctl start mlsc.service

image

API for multiple device via your function "set_active_effect_for_multiple" now works ! using that py code

def seteffect(data):
    r = requests.post(url + '/api/effect/active', json=data)

if sys.argv[1] == ('gradient') :
    jsondata={
              "devices": [
                {
                  "device": "device_0",
                  "effect": "effect_gradient"
                },
                {
                  "device": "device_1",
                  "effect": "effect_gradient"
                }
              ]
            }
    seteffect(jsondata)

delay is now close to 0 sec ! wonderful job guys !! really awesome project!