from dataclasses import dataclass
import time
from bleak import BleakClient
import asyncio
Mac="A4:C1:38:"#你的温度计的MAC
@dataclass
class Result:
temperature: float
humidity: int
voltage: float
battery: int = 0
async def main(address):
client = BleakClient(address,timeout=30.0)
await client.connect()
print("连接成功")
while(1):
buff=await client.read_gatt_char("ebe0ccc1-7a0a-4b0c-8a1a-6ff2997da3a6")
try:
result = Result(0,0,0,0)
temp=int.from_bytes(buff[0:2],byteorder='little',signed=True)/100
humidity=int.from_bytes(buff[2:3],byteorder='little')
voltage=int.from_bytes(buff[3:5],byteorder='little') / 1000
battery = round((voltage - 2) / (3.261 - 2) * 100, 2)
result.temperature = temp
result.humidity = humidity
result.voltage = voltage
result.battery = battery
print(result)
except Exception as e:
print(e)
time.sleep(1)
asyncio.run(main(Mac))
请自行安装库,部分代码借鉴 @请叫我雯子小姐的小爷