如何解决如何通过 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 举报,一经查实,本站将立刻删除。