I was using the pypi version of the library from here https://pypi.org/project/lywsd02/ which was working fine, but then upgraded to the latest code from here and hit a timeout error:
Traceback (most recent call last):
File "./xiaomi.py", line 11, in
print(client.temperature)
File "/home/pi/.local/lib/python3.7/site-packages/lywsd02/client.py", line 61, in temperature
return self.data.temperature
File "/home/pi/.local/lib/python3.7/site-packages/lywsd02/client.py", line 69, in data
self._get_sensor_data()
File "/home/pi/.local/lib/python3.7/site-packages/lywsd02/client.py", line 136, in _get_sensor_data
self._subscribe(UUID_DATA, self._process_sensor_data)
File "/home/pi/.local/lib/python3.7/site-packages/lywsd02/client.py", line 163, in _subscribe
desc.write(bytes([0, 1]), withResponse=True)
File "/home/pi/.local/lib/python3.7/site-packages/bluepy/btle.py", line 250, in write
self.peripheral.writeCharacteristic(self.handle, val, withResponse)
File "/home/pi/.local/lib/python3.7/site-packages/bluepy/btle.py", line 543, in writeCharacteristic
return self._getResp('wr')
File "/home/pi/.local/lib/python3.7/site-packages/bluepy/btle.py", line 407, in _getResp
resp = self._waitResp(wantType + ['ntfy', 'ind'], timeout)
File "/home/pi/.local/lib/python3.7/site-packages/bluepy/btle.py", line 368, in _waitResp
raise BTLEGattError("Bluetooth command failed", resp)
bluepy.btle.BTLEGattError: Bluetooth command failed (code: 129, error: A timeout occured)
Looking at the difference between the two versions, the problem appears to be in line 163 of client .py : desc.write(bytes([0, 1]), withResponse=True). Replacing this with the equivalent line from the pypi version 0.0.5 : desc.write(0x01.to_bytes(2, byteorder="little"), withResponse=True) fixes the problem.
I was using the pypi version of the library from here https://pypi.org/project/lywsd02/ which was working fine, but then upgraded to the latest code from here and hit a timeout error:
Traceback (most recent call last): File "./xiaomi.py", line 11, in
print(client.temperature)
File "/home/pi/.local/lib/python3.7/site-packages/lywsd02/client.py", line 61, in temperature
return self.data.temperature
File "/home/pi/.local/lib/python3.7/site-packages/lywsd02/client.py", line 69, in data
self._get_sensor_data()
File "/home/pi/.local/lib/python3.7/site-packages/lywsd02/client.py", line 136, in _get_sensor_data
self._subscribe(UUID_DATA, self._process_sensor_data)
File "/home/pi/.local/lib/python3.7/site-packages/lywsd02/client.py", line 163, in _subscribe
desc.write(bytes([0, 1]), withResponse=True)
File "/home/pi/.local/lib/python3.7/site-packages/bluepy/btle.py", line 250, in write
self.peripheral.writeCharacteristic(self.handle, val, withResponse)
File "/home/pi/.local/lib/python3.7/site-packages/bluepy/btle.py", line 543, in writeCharacteristic
return self._getResp('wr')
File "/home/pi/.local/lib/python3.7/site-packages/bluepy/btle.py", line 407, in _getResp
resp = self._waitResp(wantType + ['ntfy', 'ind'], timeout)
File "/home/pi/.local/lib/python3.7/site-packages/bluepy/btle.py", line 368, in _waitResp
raise BTLEGattError("Bluetooth command failed", resp)
bluepy.btle.BTLEGattError: Bluetooth command failed (code: 129, error: A timeout occured)
Looking at the difference between the two versions, the problem appears to be in line 163 of client .py : desc.write(bytes([0, 1]), withResponse=True). Replacing this with the equivalent line from the pypi version 0.0.5 : desc.write(0x01.to_bytes(2, byteorder="little"), withResponse=True) fixes the problem.