AnthonyKNorman / Xiaomi_LYWSD03MMC_for_HA

Collecting data via Bluetooth from the Xiaomi LYWSD03MMC Temperature Display using ESP32 running Micropython
51 stars 7 forks source link

Works great but ! #5

Open VladoPortos opened 3 years ago

VladoPortos commented 3 years ago

Hello, Thanks so much for this, it works ok ( although I'm coding in python a lot, this was first time micropython ) Issues I had:

        # Brief delay before sending RAW MODE char if requests
        if _rawdelay > 0:
            time.sleep(_rawdelay)

        self.serial.write(b'\r\x03\x03') # ctrl-C twice: interrupt any running program

        # flush input (without relying on serial.flushInput())
        n = self.serial.inWaiting()
        while n > 0:
            self.serial.read(n)
            n = self.serial.inWaiting()

        time.sleep(2)  #<----- THIS ONE HERE

        self.serial.write(b'\r\x01') # ctrl-A: enter raw REPL
        data = self.read_until(1, b'raw REPL; CTRL-B to exit\r\n>')
        if not data.endswith(b'raw REPL; CTRL-B to exit\r\n>'):```

After that it uploaded fine.

GAP procedure initiated: connect; peer_addr_type=0 peer_addr=a4:c1:38:3d:82:19 scan_itvl=16 scan_window=16 itvl_min=24 itvl_max=40 latency=0 supervision_timeout=256 min_ce_len=16 max_ce_len=768 own_addr_type=0
self.connected False
Trying to connect to a4:c1:38:3d:82:19
Error: Connect [Errno 114] EALREADY
self.connected False
connected peripheral has disconnected. 65535 255  0: 0: 0: 0: 0: 0
Trying to connect to a4:c1:38:3d:82:19
GAP procedure initiated: connect; peer_addr_type=0 peer_addr=a4:c1:38:3d:82:19 scan_itvl=16 scan_window=16 itvl_min=24 itvl_max=40 latency=0 supervision_timeout=256 min_ce_len=16 max_ce_len=768 own_addr_type=0
self.connected False
IRQ peripheral connect
GATT procedure initiated: write; att_handle=56 len=2
.A peripheral has sent a notify request. 0 54 b'\xd5\n5\x93\x0c'
A gattc_write() has completed - status. 0 56 0
GAP procedure initiated: connection parameter update; conn_handle=0 itvl_min=12 itvl_max=24 latency=0 supervision_timeout=90 min_ce_len=16 max_ce_len=768
write ok
GATT procedure initiated: write; att_handle=70 len=3
.A gattc_write() has completed - status. 0 70 0
write ok
.....A peripheral has sent a notify request. 0 54 b'\xd5\n5\x93\x0c'
Data received
GAP procedure initiated: terminate connection; conn_handle=0 hci_reason=19
.connected peripheral has disconnected. 0 0 a4:c1:38:3d:82:19
{"temperature": "27.73", "humidity": "53", "batt": "100", "voltage": "3.219"}
b'espble/a4c1383d8219'
Found:
Found Address: b8:bc:5b:6b:e9: 7 Name: name <--- Not correct
Found Address: a0:2c:36:f0:4b:68 Name: LM4311 <--- My clock 
Found Address: a4:c1:38:d4: 4:d0 Name: LYWSD03MMC <-- gaps in address ? ( creates mqtt with a gap, cause issues later )
Found Address: a4:c1:38:3d:82:19 Name: LYWSD03MMC
Found Address: a4:c1:38:fe:6e:77 Name: LYWSD03MMC
Found Address: a4:c1:38:80:eb:23 Name: LYWSD03MMC
Found Address: a4:c1:38:38:62:6b Name: LYWSD03MMC
Time set from server

Thats about it what I had from today :D

AnthonyKNorman commented 3 years ago

Wow, lot's to address. As you found, I used the specific version of MP for the bluetooth support. I also get the SPI error. No clue about the ampy issue. I don't have a problem. Saving devices to a file is obviously possible. Had you thought of trying it yourself? I also get loads of connection error. The biggest effort in the code was dealing with all the errors. It doesn't matter that it finds other devices. It will only use those called LYWSD03MMC. The missing leading zero in the MAC address is a bug and I will address that.

AnthonyKNorman commented 3 years ago

Can you let me know if the new version of ble.py fixes the spaces in the mac address?

VladoPortos commented 3 years ago

Hi @AnthonyKNorman I'm going at it whole morning ( also created merge request for the fix of prettify function, you have implemented the same fix I have proposed :) ) however as you say there is lots of connection errors.

Some of them I can't really explain... from the output it never connect to the device at first try, it have to do it 3+ time to get connection and reply... same goes for getting names of the device during the initial scan, and this is bigger issue for me as it tends to miss one device ( in my case it will detect all 5 devices, but tend to miss one at random because it could not get it name back, And sometimes it will find all 5 ok ) I have tried to extend the scanning time and also connection scanning time, nothing seems to help.

AnthonyKNorman commented 3 years ago

I don't know what to suggest. I think the bluetooth connection to these things is pretty flaky. It took much longer to build in all the error handling than it did to get it to initially connect.

isoinh commented 3 years ago

Hi,

Just got mine to work and noticed that there is lots of connection issues in the log:

2020-08-18 06:32:57 Error: Connect [Errno 114] EALREADY
2020-08-18 06:33:01 Error: Connect [Errno 114] EALREADY
2020-08-18 06:33:03 Error: Connect [Errno 114] EALREADY
2020-08-18 06:33:05 Error: Connect [Errno 114] EALREADY
2020-08-18 06:33:12 Error: Connect [Errno 114] EALREADY
2020-08-18 06:33:17 Found Address: a4:c1:38:0f:39:c8 Name: LYWSD03MMC
2020-08-18 06:33:17 Found Address: a4:c1:38:1b:74:08 Name: LYWSD03MMC
2020-08-18 06:33:18 Time set from server
2020-08-18 06:33:19 Error: Connect [Errno 114] EALREADY
2020-08-18 06:33:33 Error: Connect [Errno 114] EALREADY
2020-08-18 06:34:40 Error: Connect [Errno 114] EALREADY
2020-08-18 06:34:43 Error: Connect [Errno 114] EALREADY
2020-08-18 06:34:54 Error: Connect [Errno 114] EALREADY
2020-08-18 06:34:56 Error: Connect [Errno 114] EALREADY

As you can see these errors came just within few minutes. It seems that everything is working as it should despite those connection errors but I'm a bit worried that there would be a lot of junk after a while. Is the log file rotated or how could I disable logging?