Xiaomi_LYWSD03MMC_for_HA icon indicating copy to clipboard operation
Xiaomi_LYWSD03MMC_for_HA copied to clipboard

Works great but !

Open VladoPortos opened this issue 4 years ago • 5 comments

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:

  • not sure what module you used, I have Wemos ESP32 - ESP Wroom 32 uploading the micropython from setup guide (with spiram) will work, just produce error lines at start saying that it could not enable ram or something like that, which makes sense as this module does not have external ram. So i tried to install the no-spiram version, and that one does not have ubloototh module :D...

  • I could not upload anything to ESP32 it gave me error: ampy.pyboard.PyboardError: could not enter raw repl To solve that I had to edit file: pyboard.py to add one line:

        # 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.

  • Feature: it is running now, and sending messages to MQTT server, I have 5 sensors and would be cool to identify them somehow ( maybe utilize the BLE icon the show when connected )

  • Strange looking errors ( not sure if to worry about that ) this one: Error: Connect [Errno 114] EALREADY

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'
  • Feature request ? have one script to scan for the devices print the output and safe to file... ( maybe let us edit it ) and not have it scan every time it restarts, it takes 5 min to finish :-/

  • Issue: wrong device detected ?

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
  • Lots of repeating messages:
Trying to connect to a4:c1:38:80:eb:23
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:80:eb:23
GAP procedure initiated: connect; peer_addr_type=0 peer_addr=a4:c1:38:80:eb:23 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:80:eb:23
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:80:eb:23
GAP procedure initiated: connect; peer_addr_type=0 peer_addr=a4:c1:38:80:eb:23 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:80:eb:23
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:80:eb:23
GAP procedure initiated: connect; peer_addr_type=0 peer_addr=a4:c1:38:80:eb:23 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:80:eb:23
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:80:eb:23
GAP procedure initiated: connect; peer_addr_type=0 peer_addr=a4:c1:38:80:eb:23 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

Thats about it what I had from today :D

VladoPortos avatar Jul 22 '20 20:07 VladoPortos

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 avatar Jul 23 '20 07:07 AnthonyKNorman

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

AnthonyKNorman avatar Jul 23 '20 07:07 AnthonyKNorman

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.

VladoPortos avatar Jul 23 '20 08:07 VladoPortos

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.

AnthonyKNorman avatar Aug 16 '20 17:08 AnthonyKNorman

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?

isoinh avatar Aug 18 '20 06:08 isoinh