如何通过 Python 从 MCCGQ02HL 传感器读取数据?

问题描述

我正在读取传感器 mccgq02hl 的打开状态。我已经在类似的小米 lywsd03mmc 上加密和读取数据。但是当我更改 mccgq02hl 的 UUID 时。它不通知也不读取数据。

更多细节,我想写一个代码,在Window OS上通过Python读取mccgq02hl传感器的状态。由于不完全支持window(支持Linux),我没有尝试过pygatt。

目前,我正在为 lywsd03mmc 传感器使用 Bleak python 库,它可以读取并通知温带和潮湿数据。但不适用于 mccgq02hl 传感器(打开/关闭状态)。

这是我的code

import logging
import asyncio

from bleak import BleakClient
from bleak import _logger as logger

CHaraCTERISTIC_UUID = "f000aa65-0451-4000-b000-000000000000"  # <--- Change to the characteristic you want to enable notifications from.
ADDRESS = "24:71:89:cc:09:05"  # <--- Change to your device's address here if you are using Windows or Linux

SLEEP_TIME = 2
TIME_OUT = 20


def notification_handler(sender,data):
    """Simple notification handler which prints the data received."""
    print("---> Notification handler:\t {0}: {1}".format(sender,data))


def disconnect_callback(client: BleakClient):
    print("xxxxx Client with address {} got disconnected!".format(client.address))


async def connect_to_device(address: str,debug=True):
    if debug:
        import sys
        l = logging.getLogger("asyncio")
        l.setLevel(logging.DEBUG)
        h = logging.StreamHandler(sys.stdout)
        h.setLevel(logging.DEBUG)
        l.addHandler(h)
        logger.addHandler(h)

    while True:
        print("Waiting connect to sensor....")
        try:
            async with BleakClient(address,timeout=TIME_OUT,disconnected_callback=disconnect_callback) as client:
                # await client.connect();
                is_connected = await client.is_connected()
                if is_connected:
                    print("Connected to Device")

                    await client.start_notify(
                        CHaraCTERISTIC_UUID,notification_handler,)
                    while True:
                        if not is_connected:
                            print("Device disconnected!!!")
                            break
                        await asyncio.sleep(SLEEP_TIME)
                        print("=========")

                    # await client.stop_notify(CHaraCTERISTIC_UUID)
                else:
                    print(f"Failed to connect to Device")
        except Exception as e:
            print(f"Exception when connect: {e}")

        print(f"\n---Reconnect! Sleep {SLEEP_TIME} seconds!")
        await asyncio.sleep(SLEEP_TIME)


if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.set_debug(True)
    task = asyncio.ensure_future(connect_to_device(ADDRESS,debug=True))
    loop.run_until_complete(task)

所以我的问题是如何在 Window 上的 Python 中从 mccgq02hl 传感器读取数据?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)