AS
#!/usr/bin/env python3
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from bluepy import btle
import struct
mac_addr_LYWSD02 = 'A4:C1:38:F8:E8:88'
#mac_addr_LYWSD02 = 'E7:2E:01:42:6C:AB'
class MyDelegate(btle.DefaultDelegate):
def handleNotification(self, cHandle, data):
global HUM
global TEMP
HUM = data[2]
TEMP = struct.unpack('h', data[:2])[0] / 100
p = btle.Peripheral(mac_addr_LYWSD02)
p.setDelegate(MyDelegate())
uuid = 'EBE0CCC1-7A0A-4B0C-8A1A-6FF2997DA3A6'
ch = p.getCharacteristics(uuid=uuid)[0]
desc = ch.getDescriptors(forUUID=0x2902)[0]
desc.write(0x01.to_bytes(2, byteorder="little"), withResponse=True)
print ("LYWSD02")
i = 0
while True:
HUM = 0
TEMP = 0
p.waitForNotifications(10)
i += 1
print(i,"T =",TEMP,"*C, H =", HUM,"%")
if i >= 5 :
break
p.disconnect()