You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
the connection closes 30-120 seconds after the script is run
Code
importtimeimportcolorsysimportasynciofrombleakimportBleakClient, BleakScannerBLEDOM_CHARACTERISTIC="0000fff3-0000-1000-8000-00805f9b34fb"importlogginglogging.basicConfig(level=logging.DEBUG, filename="py_log.log",filemode="w")
classBleLedDevice:
def__init__(self, peripheral, characteristics):
self.peripheral=peripheralself.characteristics=characteristics@staticmethodasyncdefnew(device_address: str) ->"BleLedDevice":
bt_client=BleakClient(device_address)
device=BleLedDevice(bt_client, [])
awaitdevice.connect()
awaitdevice.discover_characteristics()
awaitdevice.power_on()
returndeviceasyncdefconnect(self):
awaitself.peripheral.connect()
asyncdefdiscover_characteristics(self):
services=awaitself.peripheral.get_services()
forserviceinservices:
forcharacteristicinservice.characteristics:
ifcharacteristic.uuid==BLEDOM_CHARACTERISTIC:
self.characteristics.append(characteristic)
ifnotself.characteristics:
raiseException(f"Couldn't find the BLEDOM characteristic on the device.")
def_characteristic(self):
returnself.characteristics[0]
asyncdefpower_on(self):
""" Power LED strip on. """awaitself.generic_command(0x04, 0xF0, 0x00, 0x01, 0xFF)
asyncdefpower_off(self):
""" Power LED strip off. """awaitself.generic_command(0x04, 0x00, 0x00, 0x00, 0xFF)
asyncdefset_color(self, red: int, green: int, blue: int):
""" Show a solid colour. The values for `red`, `green` and `blue` are expected to be integers with values between 0 and 255 (inclusive). """awaitself.generic_command(0x05, 0x03, red, green, blue)
asyncdefset_brightness(self, value: int):
""" Show a solid brightness. The value for `value` is expected to be an integer between 0 and 100 (inclusive). """awaitself.generic_command(0x01, min(value, 100), 0, 0, 0)
asyncdefgeneric_command(
self,
id: int,
arg0: int=0x00,
arg1: int=0x00,
arg2: int=0x00,
arg3: int=0x00,
arg4: int=0x00,
):
data=bytearray([0x7E, 0x00, id, arg0, arg1, arg2, arg3, arg4, 0xEF])
print("sending message %s"%list(data))
awaitself.peripheral.write_gatt_char(self._characteristic(), data)
asyncdefmain():
address="BE:FF:F0:50:74:63"# Replace with the actual device addressdevice=awaitBleLedDevice.new(address)
awaitdevice.power_on()
transition_time=1# Время перехода между цветами, в секундахawaitdevice.set_brightness(10)
whileTrue:
forhueinrange(0, 360):
hue_normalized=hue/20.0r, g, b=colorsys.hsv_to_rgb(hue_normalized, 1, 1)
awaitdevice.set_color(int(r*255), int(g*255), int(b*255))
print(f"R: {int(r*255)}, G: {int(g*255)}, B: {int(b*255)}")
awaitasyncio.sleep(transition_time)
if__name__=="__main__":
asyncio.run(main())
Logs
Traceback:Traceback (most recent call last): File "C:\pythonProject\pythonProject1\2.py", line 121, in <module> asyncio.run(main()) File "C:\Program Files\Python311\Lib\asyncio\runners.py", line 190, in run return runner.run(main) ^^^^^^^^^^^^^^^^ File "C:\Program Files\Python311\Lib\asyncio\runners.py", line 118, in run return self._loop.run_until_complete(task) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Program Files\Python311\Lib\asyncio\base_events.py", line 653, in run_until_complete return future.result() ^^^^^^^^^^^^^^^ File "C:\pythonProject\pythonProject1\2.py", line 114, in main await device.set_color(int(r*255), int(g*255), int(b*255)) File "C:\pythonProject\pythonProject1\2.py", line 61, in set_color await self.generic_command(0x05, 0x03, red, green, blue) File "C:\pythonProject\pythonProject1\2.py", line 86, in generic_command await self.peripheral.write_gatt_char(self._characteristic(), data) File "C:\Program Files\Python311\Lib\site-packages\bleak\__init__.py", line 776, in write_gatt_char await self._backend.write_gatt_char(characteristic, data, response) File "C:\Program Files\Python311\Lib\site-packages\bleak\backends\winrt\client.py", line 887, in write_gatt_char raise BleakError("Not connected")bleak.exc.BleakError: Not connectedProcess finished with exit code 1
Description
the connection closes 30-120 seconds after the script is run
Code
Logs
logging:
The text was updated successfully, but these errors were encountered: