|
3 | 3 | """ |
4 | 4 | TinyTuya - Smart Bulb RGB Music Test |
5 | 5 |
|
6 | | - TODO: Figure out what strings to send to the bulb to |
7 | | - represent real music data, beats, note, fades, etc. |
8 | | -
|
9 | 6 | Author: Jason A. Cox |
10 | 7 | For more information see https://github.com/jasonacox/tinytuya |
11 | 8 |
|
12 | 9 | """ |
13 | 10 | import tinytuya |
14 | 11 | import time |
15 | 12 | import random |
| 13 | +import os |
16 | 14 |
|
17 | 15 | #tinytuya.set_debug() |
18 | 16 |
|
19 | | -# SmartBulb |
20 | 17 | DEVICEID = "01234567891234567890" |
21 | | -DEVICEIP = "10.0.1.99" |
22 | | -DEVICEKEY = "0123456789abcdef" |
23 | | -DEVICEVERS = "3.3" |
| 18 | +DEVICEIP = "Auto" # Will try to discover the bulb on the network |
| 19 | +DEVICEKEY = "" # Leave blank to read from devices.json |
| 20 | +DEVICEVERS = 3.3 # Must be set correctly unless IP=Auto |
| 21 | + |
| 22 | +# Check for environmental variables and always use those if available |
| 23 | +DEVICEID = os.getenv("DEVICEID", DEVICEID) |
| 24 | +DEVICEIP = os.getenv("DEVICEIP", DEVICEIP) |
| 25 | +DEVICEKEY = os.getenv("DEVICEKEY", DEVICEKEY) |
| 26 | +DEVICEVERS = os.getenv("DEVICEVERS", DEVICEVERS) |
24 | 27 |
|
25 | 28 | print("TinyTuya - Smart Bulb Music Test [%s]\n" % tinytuya.__version__) |
26 | 29 | print('TESTING: Device %s at %s with key %s version %s' % |
27 | 30 | (DEVICEID, DEVICEIP, DEVICEKEY, DEVICEVERS)) |
28 | 31 |
|
29 | 32 | # Connect to Tuya BulbDevice |
30 | | -d = tinytuya.BulbDevice(DEVICEID, DEVICEIP, DEVICEKEY) |
31 | | -d.set_version(float(DEVICEVERS)) # IMPORTANT to always set version |
32 | | -# Keep socket connection open between commands |
33 | | -d.set_socketPersistent(True) |
| 33 | +d = tinytuya.BulbDevice(DEVICEID, address=DEVICEIP, local_key=DEVICEKEY, version=DEVICEVERS, persist=True) |
| 34 | + |
| 35 | +if (not DEVICEIP) or (DEVICEIP == 'Auto') or (not DEVICEKEY) or (not DEVICEVERS): |
| 36 | + print('Device %s found at %s with key %r version %s' % |
| 37 | + (d.id, d.address, d.local_key, d.version)) |
34 | 38 |
|
35 | 39 | # Show status of device |
36 | 40 | data = d.status() |
|
41 | 45 | d.set_mode('music') |
42 | 46 | data = d.status() |
43 | 47 |
|
44 | | -x = 0 |
45 | | -while (x<20): |
| 48 | +d.set_socketPersistent( True ) |
| 49 | + |
| 50 | +# Devices respond with a command ACK, but do not send DP updates. |
| 51 | +# Setting the 2 options below causes it to wait for a response but |
| 52 | +# return immediately after an ACK. |
| 53 | +d.set_sendWait( None ) |
| 54 | +d.set_retry( False ) |
| 55 | + |
| 56 | +for x in range(100): |
46 | 57 | # Value is 0 1111 2222 3333 4444 5555 |
47 | 58 | # see: https://developer.tuya.com/en/docs/iot/solarlight-function-definition?id=K9tp16f086d5h#title-10-DP27(8)%3A%20music |
48 | | - mode = 0 # 0 - hard transitions (jumping), 1 - smooth transitions (gradient) |
49 | | - hue = random.randint(0,360) # 1111 (hue: 0–360, 0X0000–0X0168) |
50 | | - saturation = random.randint(0,1000) # 2222 (saturation: 0–1000, 0X0000–0X03E8) |
51 | | - brightness = random.randint(0,1000) # 3333 (brightness: 0–1000, 0X0000–0X03E8) |
52 | | - white_brightness = 0 # 4444: indicates brightness. It ranges from 0 to 1,000. |
53 | | - temperature = 0 # 5555: the temperature value (0–1000) |
54 | | - value = f"{mode:01X}{hue:04X}{saturation:04X}{brightness:04X}{white_brightness:04X}{temperature:04X}" |
55 | | - print (" > Sending %s" % value) |
56 | | - payload = d.generate_payload(tinytuya.CONTROL, {"27": value}) |
57 | | - d.send(payload) |
58 | | - if (x % 3): |
59 | | - time.sleep(1) # extend every 3 beat |
60 | | - time.sleep(0.2) |
61 | | - x = x + 1 |
| 59 | + red = random.randint(0,255) |
| 60 | + green = random.randint(0,255) |
| 61 | + blue = random.randint(0,255) |
| 62 | + |
| 63 | + if (x % 6 == 0): |
| 64 | + # extend every 6 beat |
| 65 | + d.set_music_colour( d.MUSIC_TRANSITION_FADE, red, green, blue ) |
| 66 | + time.sleep(2) |
| 67 | + else: |
| 68 | + # Jump! |
| 69 | + d.set_music_colour( d.MUSIC_TRANSITION_JUMP, red, green, blue ) |
| 70 | + time.sleep(0.1) # the bulbs seem to get upset if updates are faster than 0.1s (100ms) |
62 | 71 |
|
63 | 72 | # Done |
64 | 73 | print('\nDone') |
65 | | -d.set_white() |
| 74 | +d.turn_off() |
0 commit comments