from matrix_lite import led from time import sleep from tuya_iot import TuyaOpenAPI import json import math # Cloud project authorization info ACCESS_ID = 'your-access-key' ACCESS_KEY = 'your-secret-key' # Select an endpoint base on your project availability zone # For more info: https://developer.tuya.com/en/docs/iot/api-request?id=Ka4a8uuo1j4t4 ENDPOINT = "https://openapi.tuyain.com" # Project configuration USERNAME = 'your-email' # email address or phone number PASSWORD = 'your-password' DEVICE_ID = 'your-device-id' # Initialization of tuya openapi openapi = TuyaOpenAPI(ENDPOINT, ACCESS_ID, ACCESS_KEY) openapi.login(USERNAME, PASSWORD) ## Control the Device with Python ### # commands = {'commands': [{'code':'switch_led','value': True}]} # request = openapi.post(f'/v1.0/iot-03/devices/{DEVICE_ID}/commands', commands) # print(request) def hsv2rgb(h, s, v): h = float(h) s = float(s) v = float(v) h60 = h / 60.0 h60f = math.floor(h60) hi = int(h60f) % 6 f = h60 - h60f p = v * (1 - s) q = v * (1 - f * s) t = v * (1 - (1 - f) * s) r, g, b = 0, 0, 0 if hi == 0: r, g, b = v, t, p elif hi == 1: r, g, b = q, v, p elif hi == 2: r, g, b = p, v, t elif hi == 3: r, g, b = p, q, v elif hi == 4: r, g, b = t, p, v elif hi == 5: r, g, b = v, p, q r, g, b = int(r * 255), int(g * 255), int(b * 255) return r, g, b while True: # Get device status summary request = openapi.get(f'/v1.0/iot-03/devices/{DEVICE_ID}/status') switch_led = request[ 'result' ][0] # get state of led if switch_led['value'] == True: print(request) colour_data = request[ 'result' ][ 3 ] print(colour_data) value = colour_data[ 'value' ] print(value) hsv_val = json.loads(value) h = float(hsv_val[ 'h' ]) s = float(hsv_val[ 's' ]) / 255 * 100 #convert to percent v = float(hsv_val[ 'v' ]) / 255 * 100 #convert to percent r, g, b = hsv2rgb(h, s/100, v/100) # s and v vlaues should be between 0-1 print(r,g,b) led.set({'r': r, 'g': g, 'b': b, 'w': 0}) # object else: led.set('black') sleep(5)