tinytuya icon indicating copy to clipboard operation
tinytuya copied to clipboard

Music mode

Open frodeheg opened this issue 2 years ago • 4 comments

How is the music mode supposed to work? If I have this python script:

a = tinytuya.BulbDevice(DEVICEID, DEVICEIP, DEVICEKEY)
a.set_version(3.3)
a.set_mode('music')
data = a.status()
print(data)

I get the following output {'dps': {'20': True, '21': 'music', '22': 1000, '23': 750, '24': '007803e803e8', '25': '07464602000003e803e800000000464602007803e803e80000000046460200f003e803e800000000464602003d03e803e80000000046460200ae03e803e800000000464602011303e803e800000000', '26': 0, '41': True}} The light only turn red but does not respond to sound. Should it? I have no idea whether or not there is a microphone in the lightbulb but at least it did change to music mode.

frodeheg avatar Mar 25 '22 13:03 frodeheg

The Bulbs itself does not have a microphone. As far i assume and little bit know the music mode differs from color mode by more responsiveness as color updates would not be send to cloud and maybe not stored on device itself. As i seen on iot.tuya.com music mode is send only. The devices although wont send a reply local. TinyTuya implements commands to set music mode, but for now no function for updating the device's colors in this mode. As i implemented my Tuya in DiyHue I did some research how the payload send to devices maybe could look like but i found nothing usable till now. The tuya documentation did not helped. The only thing i found is this json snippet: { "commands":[ { "code":"music_data", "value":{ "change_mode":"direct", "bright":500, "temperature":800, "h":201, "s":611, "v":999 } } ] }

Maybe its an idea to setup a mitm proxy for capturing traffic and try to decrypt messages in music mode with smarthome app or maybe jasonacox has an better idea. Would be glad to hear something about :-)

panic2k avatar May 01 '22 18:05 panic2k

Hi @panic2k and @frodeheg . According to Tuya (see here), it seems that the app is the one that is sending color changes to the bulb. I'm sure you figured that part out by now. What we don't know is what the app is sending to the bulb.

It appears that DPS 27 is reserved for this (see here). If I use the async method, I can use this DPS to change the color in some random way, and the string I send seems to change (higher values have slower fades). I'm sure there is other combinations or string that drive more specialization. Perhaps the Phone app is just sending packets to indicate a beat which would cause the bulb to change to what appears to be the "beat of the music". In any case, here is a working script you can play with. See what you can figure out:

import tinytuya
import time

#tinytuya.set_debug() # uncomment if you want to see all packets

d = tinytuya.BulbDevice(DEVICEID, DEVICEIP, DEVICEKEY)
d.set_version(3.3)
d.set_socketPersistent(True)  

# Show status of device
data = d.status()
print('\nCurrent Status of Bulb: %r' % data)

# Music Test
print('Setting to Music')
d.set_mode('music')
data = d.status()

# Send some music data to bulb
x = 0
while (x<20):
    value = "%02d01" % x
    print (" > Sending %s" % value)
    payload = d.generate_payload(tinytuya.CONTROL, {"27": value})
    d.send(payload)
    if (x % 3):
        time.sleep(1)  # extend every 3 beat
    time.sleep(0.2)
    x = x + 1

# Done
print('\nDone')
d.set_white()

jasonacox avatar May 03 '22 05:05 jasonacox

Thank you, that's maybe great for some bulbs. I've tried with my GU10 bulbs, but they are stay just red. If i use Android App, the next color is displayed on phone - there must be an command to set the bulbs. I've no Idea how cloud messages could be decrypted, but maybe a deeper look at TuyaOS IDE could give an idea. In spare time i take a look this. For sure, that's no easy task. But maybe there's something useful there. :-)

panic2k avatar Jun 19 '22 02:06 panic2k

Thanks @panic2k ! I can't help but wonder if this sequence is similar to sending IR commands for IR Tuya devices. You might check these out:

  • Overview of IR DPS: https://github.com/jasonacox/tinytuya#version-33---universal-ir-controller-with-optional-temphumidity
  • Trick using Tuya IoT platform debugging: https://github.com/jasonacox/tinytuya/issues/74#issuecomment-1139730159

jasonacox avatar Jun 19 '22 03:06 jasonacox