Skip to content

BLE connection handling update.#979

Merged
1technophile merged 1 commit into1technophile:developmentfrom
h2zero:ble-connect
Jun 17, 2021
Merged

BLE connection handling update.#979
1technophile merged 1 commit into1technophile:developmentfrom
h2zero:ble-connect

Conversation

@h2zero
Copy link
Collaborator

@h2zero h2zero commented Jun 9, 2021

Create a class for each connectable device to handle the specific parameters separately in an extendable form.

@h2zero h2zero force-pushed the ble-connect branch 6 times, most recently from 5829b2f to 78c801a Compare June 11, 2021 02:58
@h2zero
Copy link
Collaborator Author

h2zero commented Jun 11, 2021

@NorthernMan54 I've added the extra data conditions to this if you'd like to test them. I don't know what the extra data is but if you would like to provide that information I can incorporate it as well.

@NorthernMan54
Copy link
Collaborator

I need to figure out my test environments and likely drop my fork of the main openMQTTGateway codebase and switch to yours. Will do that over the weekend, I just have a couple of small checkins to openMQTTGateway to get out of the way first.

@h2zero
Copy link
Collaborator Author

h2zero commented Jun 12, 2021

Rebasing to develpoment after the updates, seems to be an issue with discovery. Investigating now.

@h2zero h2zero force-pushed the ble-connect branch 4 times, most recently from 5f7ca18 to 42782e6 Compare June 12, 2021 17:26
@h2zero
Copy link
Collaborator Author

h2zero commented Jun 12, 2021

Issues resolved, works well with LYWSD03MMC. Hopefully @NorthernMan54 can test with the DT24.

@NorthernMan54
Copy link
Collaborator

@h2zero I cloned your ble-connect branch to my dev environment this am, then took the family to the beach.

should be able complete testing tomorrow am

@h2zero
Copy link
Collaborator Author

h2zero commented Jun 12, 2021

No hurry, you'll need to rebase as I just pushed an update. Thanks!

@NorthernMan54
Copy link
Collaborator

@h2zero That works really good, I didn't see any issues

PS I added the temp from the second part of the message, but got stuck in git hell trying to create a pull request on your fork. Am thinking once this is merged into the main development branch I can add it then. Your second message handling worked perfectly.

My WIP on DT24 Temperature

https://github.com/NorthernMan54/OrigOpenMQTTGateway/pull/2

@h2zero
Copy link
Collaborator Author

h2zero commented Jun 13, 2021

That works really good, I didn't see any issues

Great!

PS I added the temp from the second part of the message, but got stuck in git hell trying to create a pull request on your fork. Am thinking once this is merged into the main development branch I can add it then. Your second message handling worked perfectly.

😆 I know the pain. Agreed, it would be better to submit a PR for the update after this is merged.
Glad to hear it's working, I will make this PR official now.

@h2zero h2zero marked this pull request as ready for review June 13, 2021 01:38
@h2zero
Copy link
Collaborator Author

h2zero commented Jun 13, 2021

@NorthernMan54 does this device have any readable/writable characteristics?

@NorthernMan54
Copy link
Collaborator

The device does have this interface, https://github.com/NiceLabs/atorch-console/blob/master/docs/protocol-design.md#command but in the grand scheme of things it is not very useful. I have been using it for a while, and never used these.

@h2zero
Copy link
Collaborator Author

h2zero commented Jun 13, 2021

Thanks, I thought if there was something useful there I could add the write functions.

@NorthernMan54
Copy link
Collaborator

Did you look at #980 ?

im thinking that the trigger is the ble data message and sensor data on the same topic. Should they be combined into a single message?

I have the same with the DT24, but my client ignores the message of the field is missing.

@h2zero
Copy link
Collaborator Author

h2zero commented Jun 13, 2021

I briefly looked at that issue, I will keep it in mind with the changes I'm working on.

@h2zero
Copy link
Collaborator Author

h2zero commented Jun 13, 2021

@NorthernMan54 I just added a commit that I think will resolve #980, could you test it when you have a chance?

@NorthernMan54
Copy link
Collaborator

@h2zero That's an interesting approach

On startup/restart I see a signal strength/BLE message

{"id":"20:05:07:16:13:61","name":"DT24-BLE","manufacturerdata":"733688a0200507151361","rssi":-67,"model":"DT24"}

Then afterwards just these every ~3 minutes ( device status )

{"model":"DT24","id":"20:05:07:16:13:61","volt":15.8,"current":0,"power":0,"energy":0,"price":2.01}

Which I believe is what you were expecting

@h2zero
Copy link
Collaborator Author

h2zero commented Jun 14, 2021

Thanks for testing it!

Yes, that is the expected result as it addresses the issue referred to. It was a pretty simple solution to add with this PR, hopefully @fhb can confirm it resolves it.

Not sure if this would cause issues for other uses though, @1technophile would need to judge that.

@NorthernMan54
Copy link
Collaborator

I was thinking about what it would take to combine the data into a single message. Would it make sense to include the signal strength etc with the device status ?

@h2zero
Copy link
Collaborator Author

h2zero commented Jun 15, 2021

Looks like I missed adding the return call on line 127 in ZgatewayBLEConnect.ino.

If you'd like to add that and try again I'd appreciate it.

@h2zero
Copy link
Collaborator Author

h2zero commented Jun 15, 2021

Another failure, after a successful publish

1 step forward and 2 back lol. No idea why it would fail after publishing there. I would need to decode that backtrace.

@NorthernMan54
Copy link
Collaborator

Give me a couple of hours, just need to wait for the end of my day job

@1technophile
Copy link
Owner

Not sure if this would cause issues for other uses though, @1technophile would need to judge that.

Let's see what @1technophile thinks, in my setup I don't have a use case for the BLE/RSSI data so not something I'm looking for.

For the 2 devices that require a connection, I don't think we need rssi. So we can go forward on with this change.
We will do something if there is a demand or a use case for a device that requires it.

If you could just add a macro to deactivate this filter (the filter is activated per default), this way if somebody needs to see the rrsi and what the connectable device is advertising (could be useful also for debugging)

@NorthernMan54
Copy link
Collaborator

This is the backtrace

N: Found 16 devices, scan number 115 end
N: BLE Connect begin
T: Model to connect found: 20:05:07:16:13:61
T: Found service: 0xffe0
T: Client isConnected, freeHeap: 149004
N: Registering notification - xTaskGetCurrentTaskHandle
N: Registering notification - xTaskGetCurrentTaskHandle 3FFDF538
T: Callback from 0xffe1 characteristic 20
T: Callback from 0xffe1 characteristic 16
T: Device identified creating BLE buffer
T: Device identified in CB: 20:05:07:16:13:61
N: xTaskNotifyGive 3FFDF538
N: Subject: /BTtoMQTT/200507161361
N: Received json : {"model":"DT24","id":"20:05:07:16:13:61","volt":15.6,"current":0,"power":0,"energy":0,"price":2.01}
T: jsonPublishing
Guru Meditation Error: Core  0 panic'ed (LoadProhibited). Exception was unhandled.
Core 0 register dump:
PC      : 0x400e3e74  PS      : 0x00060230  A0      : 0x800ea62a  A1      : 0x3ffdf160  
A2      : 0x00000000  A3      : 0x3ffc3d04  A4      : 0x3ffe2898  A5      : 0x3ffdf1b4  
A6      : 0x0000805f  A7      : 0x00100080  A8      : 0x3ffc3d04  A9      : 0x00000001  
A10     : 0xfffffff2  A11     : 0x00000001  A12     : 0x00060a20  A13     : 0x00000001  
A14     : 0x00060623  A15     : 0x00000000  SAR     : 0x00000010  EXCCAUSE: 0x0000001c  
EXCVADDR: 0x00100080  LBEG    : 0x4000c2e0  LEND    : 0x4000c2f6  LCOUNT  : 0x00000000  

ELF file SHA256: 0000000000000000

Backtrace: 0x400e3e74:0x3ffdf160 0x400ea627:0x3ffdf190 0x400ea6a2:0x3ffdf1b0 0x400ebc3a:0x3ffdf1f0 0x400e673e:0x3ffdf210 0x400e6380:0x3ffdf250 0x400eefde:0x3ffdf280 0x400ece4d:0x3ffdf2b0 0x400ece5f:0x3ffdf2d0 0x400f5582:0x3ffdf2f0 0x400e32eb:0x3ffdf310 0x40090d3e:0x3ffdf330
  #0  0x400e3e74:0x3ffdf160 in NimBLERemoteCharacteristic::descriptorDiscCB(unsigned short, ble_gatt_error const*, unsigned short, ble_gatt_dsc const*, void*) at .pio/libdeps/ddd-bt-rtl/NimBLE-Arduino/src/NimBLERemoteCharacteristic.cpp:328
  #1  0x400ea627:0x3ffdf190 in ble_gattc_disc_all_dscs_rx_idata at .pio/libdeps/ddd-bt-rtl/NimBLE-Arduino/src/nimble/host/src/ble_gattc.c:623
  #2  0x400ea6a2:0x3ffdf1b0 in ble_gattc_read_err at .pio/libdeps/ddd-bt-rtl/NimBLE-Arduino/src/nimble/host/src/ble_gattc.c:623
  #3  0x400ebc3a:0x3ffdf1f0 in ble_gattc_rx_find_type_value_complete at .pio/libdeps/ddd-bt-rtl/NimBLE-Arduino/src/nimble/host/src/ble_gattc.c:623
  #4  0x400e673e:0x3ffdf210 in ble_att_clt_tx_find_type_value at .pio/libdeps/ddd-bt-rtl/NimBLE-Arduino/src/nimble/host/src/ble_att_clt.c:594
  #5  0x400e6380:0x3ffdf250 in ble_att_chan_mtu at .pio/libdeps/ddd-bt-rtl/NimBLE-Arduino/src/nimble/host/src/ble_att.c:554
  #6  0x400eefde:0x3ffdf280 in ble_hs_hci_util_rand at .pio/libdeps/ddd-bt-rtl/NimBLE-Arduino/src/nimble/host/src/ble_hs_hci_util.c:178
  #7  0x400ece4d:0x3ffdf2b0 in ble_hs_is_enabled at .pio/libdeps/ddd-bt-rtl/NimBLE-Arduino/src/nimble/nimble_npl_os.h:109
  #8  0x400ece5f:0x3ffdf2d0 in ble_hs_is_enabled at .pio/libdeps/ddd-bt-rtl/NimBLE-Arduino/src/nimble/nimble_npl_os.h:109
  #9  0x400f5582:0x3ffdf2f0 in nimble_port_stop at .pio/libdeps/ddd-bt-rtl/NimBLE-Arduino/src/porting/nimble/src/nimble_port.c:130
  #10 0x400e32eb:0x3ffdf310 in NimBLEDevice::startAdvertising() at .pio/libdeps/ddd-bt-rtl/NimBLE-Arduino/src/NimBLEDevice.cpp:859 (discriminator 1)
  #11 0x40090d3e:0x3ffdf330 in vPortTaskWrapper at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/port.c:355 (discriminator 1)

@h2zero
Copy link
Collaborator Author

h2zero commented Jun 15, 2021

@1technophile will do, thanks.

@NorthernMan54 are you also operating a BLE server instance? That is what the backtrace is suggesting.

@NorthernMan54
Copy link
Collaborator

@h2zero Nope

@h2zero
Copy link
Collaborator Author

h2zero commented Jun 15, 2021

That's really strange then.

Seeing this NimBLEDevice::startAdvertising() in the backtrace is quite confusing if you're not calling that. When I get back to my PC I'll try to figure it out.

@NorthernMan54
Copy link
Collaborator

I enabled 'CORE_DEBUG_LEVEL=4' and rerunning...Hopefully it gives a better clue

@NorthernMan54
Copy link
Collaborator

With CORE_DEBUG_LEVEL=4

D NimBLEClient: "Service Discovered >> status: 0 handle: 10"
D NimBLERemoteService: ">> NimBLERemoteService()"
D NimBLERemoteService: "<< NimBLERemoteService(): 0xffe0"
D NimBLEClient: "Service Discovered >> status: 14 handle: -1"
D NimBLEClient: "<< << Service Discovered"
D NimBLEClient: "<< retrieveServices"
D NimBLERemoteService: ">> getCharacteristic: uuid: 0xffe1"
D NimBLERemoteService: ">> retrieveCharacteristics() for service: 0xffe0"
D NimBLERemoteService: "Characteristic Discovered >> status: 0 handle: 12"
D NimBLERemoteCharacteristic: ">> NimBLERemoteCharacteristic()"
D NimBLERemoteCharacteristic: "<< NimBLERemoteCharacteristic(): 0xffe1"
D NimBLERemoteService: "Characteristic Discovered >> status: 14 handle: -1"
D NimBLERemoteService: "<< Characteristic Discovered"
D NimBLEClient: "Got Client event "
D NimBLEClient: "Notify Recieved for handle: 12"
D NimBLEClient: "checking service 0xffe0 for handle: 12"
D NimBLEClient: "Got Notification for characteristic Characteristic: uuid: 0xffe1, handle: 12 0x000c, props:  0x1c"
D NimBLERemoteService: "<< retrieveCharacteristics()"
D NimBLERemoteCharacteristic: ">> setNotify(): Characteristic: uuid: 0xffe1, handle: 12 0x000c, props:  0x1c, 01"
D NimBLERemoteCharacteristic: ">> getDescriptor: uuid: 0x2902"
D NimBLERemoteCharacteristic: ">> retrieveDescriptors() for characteristic: 0xffe1"
D NimBLEClient: "Got Client event "
D NimBLEClient: "Notify Recieved for handle: 12"
D NimBLEClient: "checking service 0xffe0 for handle: 12"
D NimBLEClient: "Got Notification for characteristic Characteristic: uuid: 0xffe1, handle: 12 0x000c, props:  0x1c"
D NimBLEClient: "Invoking callback for notification on characteristic Characteristic: uuid: 0xffe1, handle: 12 0x000c, props:  0x1c"
N: Device not identified
D NimBLERemoteCharacteristic: ">> getDescriptor: uuid: 00002902-0000-1000-8000-00805f9b34fb"
D NimBLERemoteCharacteristic: ">> retrieveDescriptors() for characteristic: 0xffe1"
D NimBLERemoteCharacteristic: "Descriptor Discovered >> status: 0 handle: 13"
Guru Meditation Error: Core  0 panic'ed (LoadProhibited). Exception was unhandled.
Core 0 register dump:
PC      : 0x401abcfb  PS      : 0x00060a30  A0      : 0x800e52c1  A1      : 0x3ffdf2e0  
A2      : 0xffe14910  A3      : 0x3f4049fc  A4      : 0x3f406314  A5      : 0x3ffdf2b0  
A6      : 0x00000000  A7      : 0x0000000d  A8      : 0x8013ca20  A9      : 0x3ffdf280  
A10     : 0x401aac04  A11     : 0x3ffc8b18  A12     : 0x3f406314  A13     : 0x0000001f  
A14     : 0x00000001  A15     : 0x00000001  SAR     : 0x00000004  EXCCAUSE: 0x0000001c  
EXCVADDR: 0xffe14930  LBEG    : 0x400014fd  LEND    : 0x4000150d  LCOUNT  : 0xfffffff9  

ELF file SHA256: 0000000000000000

Backtrace: 0x401abcfb:0x3ffdf2e0 0x400e52be:0x3ffdf300 0x400ec4d7:0x3ffdf330 0x400ec552:0x3ffdf350 0x400edaea:0x3ffdf390 0x400e85ee:0x3ffdf3b0 0x400e8230:0x3ffdf3f0 0x400f11aa:0x3ffdf420 0x400ef019:0x3ffdf450 0x400ef02b:0x3ffdf470 0x400f774e:0x3ffdf490 0x400e4236:0x3ffdf4b0 0x40090d3e:0x3ffdf4d0
  #0  0x401abcfb:0x3ffdf2e0 in NimBLERemoteCharacteristic::getRemoteService() at .pio/libdeps/ddd-bt-rtl/NimBLE-Arduino/src/NimBLERemoteCharacteristic.cpp:345
  #1  0x400e52be:0x3ffdf300 in NimBLERemoteCharacteristic::descriptorDiscCB(unsigned short, ble_gatt_error const*, unsigned short, ble_gatt_dsc const*, void*) at .pio/libdeps/ddd-bt-rtl/NimBLE-Arduino/src/NimBLERemoteCharacteristic.cpp:319
  #2  0x400ec4d7:0x3ffdf330 in ble_gattc_disc_all_dscs_cb at .pio/libdeps/ddd-bt-rtl/NimBLE-Arduino/src/nimble/host/src/ble_gattc.c:623
  #3  0x400ec552:0x3ffdf350 in ble_gattc_disc_all_dscs_rx_idata at .pio/libdeps/ddd-bt-rtl/NimBLE-Arduino/src/nimble/host/src/ble_gattc.c:623
  #4  0x400edaea:0x3ffdf390 in ble_gattc_rx_find_info_idata at .pio/libdeps/ddd-bt-rtl/NimBLE-Arduino/src/nimble/host/src/ble_gattc.c:623
  #5  0x400e85ee:0x3ffdf3b0 in ble_att_clt_rx_find_info at .pio/libdeps/ddd-bt-rtl/NimBLE-Arduino/src/nimble/host/src/ble_att_clt.c:594
  #6  0x400e8230:0x3ffdf3f0 in ble_att_rx at .pio/libdeps/ddd-bt-rtl/NimBLE-Arduino/src/nimble/host/src/ble_att.c:554
  #7  0x400f11aa:0x3ffdf420 in ble_hs_hci_evt_acl_process at .pio/libdeps/ddd-bt-rtl/NimBLE-Arduino/src/nimble/host/src/ble_hs_hci_evt.c:959
  #8  0x400ef019:0x3ffdf450 in ble_hs_process_rx_data_queue at .pio/libdeps/ddd-bt-rtl/NimBLE-Arduino/src/nimble/nimble_npl_os.h:109
  #9  0x400ef02b:0x3ffdf470 in ble_hs_event_rx_data at .pio/libdeps/ddd-bt-rtl/NimBLE-Arduino/src/nimble/nimble_npl_os.h:109
  #10 0x400f774e:0x3ffdf490 in ble_npl_event_run at .pio/libdeps/ddd-bt-rtl/NimBLE-Arduino/src/nimble/nimble_npl_os.h:121
      (inlined by) nimble_port_run at .pio/libdeps/ddd-bt-rtl/NimBLE-Arduino/src/porting/nimble/src/nimble_port.c:81
  #11 0x400e4236:0x3ffdf4b0 in NimBLEDevice::host_task(void*) at .pio/libdeps/ddd-bt-rtl/NimBLE-Arduino/src/NimBLEDevice.cpp:859 (discriminator 1)
  #12 0x40090d3e:0x3ffdf4d0 in vPortTaskWrapper at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/port.c:355 (discriminator 1)

@NorthernMan54
Copy link
Collaborator

This is a successful BLE Connect

N: BLE Connect begin
D NimBLEClient: ">> connect(20:05:07:16:13:61)"
D NimBLEClient: "Got Client event "
I NimBLEClient: "Connected event"
D NimBLEClient: "Got Client event "
I NimBLEClient: "mtu update event; conn_handle=0 mtu=136"
I NimBLEClient: "Connection established"
D NimBLEClient: ">> deleteServices"
D NimBLEClient: "<< deleteServices"
D NimBLEClientCallbacks: "onConnect: default"
D NimBLEClient: "<< connect()"
D NimBLEClient: ">> getService: uuid: 0xffe0"
D NimBLEClient: ">> retrieveServices"
D NimBLEClient: "Service Discovered >> status: 0 handle: 10"
D NimBLERemoteService: ">> NimBLERemoteService()"
D NimBLERemoteService: "<< NimBLERemoteService(): 0xffe0"
D NimBLEClient: "Service Discovered >> status: 14 handle: -1"
D NimBLEClient: "<< << Service Discovered"
D NimBLEClient: "<< retrieveServices"
D NimBLERemoteService: ">> getCharacteristic: uuid: 0xffe1"
D NimBLERemoteService: ">> retrieveCharacteristics() for service: 0xffe0"
D NimBLEClient: "Got Client event "
D NimBLEClient: "Notify Recieved for handle: 12"
D NimBLEClient: "checking service 0xffe0 for handle: 12"
D NimBLEClient: "Got Client event "
D NimBLEClient: "Notify Recieved for handle: 12"
D NimBLEClient: "checking service 0xffe0 for handle: 12"
D NimBLERemoteService: "Characteristic Discovered >> status: 0 handle: 12"
D NimBLERemoteCharacteristic: ">> NimBLERemoteCharacteristic()"
D NimBLERemoteCharacteristic: "<< NimBLERemoteCharacteristic(): 0xffe1"
D NimBLERemoteService: "Characteristic Discovered >> status: 14 handle: -1"
D NimBLERemoteService: "<< Characteristic Discovered"
D NimBLERemoteService: "<< retrieveCharacteristics()"
D NimBLERemoteCharacteristic: ">> setNotify(): Characteristic: uuid: 0xffe1, handle: 12 0x000c, props:  0x1c, 01"
D NimBLERemoteCharacteristic: ">> getDescriptor: uuid: 0x2902"
D NimBLERemoteCharacteristic: ">> retrieveDescriptors() for characteristic: 0xffe1"
D NimBLERemoteCharacteristic: "Descriptor Discovered >> status: 0 handle: 13"
D NimBLERemoteCharacteristic: "Descriptor Found"
D NimBLERemoteDescriptor: ">> NimBLERemoteDescriptor()"
D NimBLERemoteDescriptor: "<< NimBLERemoteDescriptor(): 0x2902"
D NimBLERemoteCharacteristic: "<< Descriptor Discovered. status: 0"
D NimBLERemoteCharacteristic: "<< setNotify()"
D NimBLERemoteDescriptor: ">> Descriptor writeValue: Descriptor: uuid: 0x2902, handle: 13"
D NimBLEClient: "Got Client event "
D NimBLEClient: "Notify Recieved for handle: 12"
D NimBLEClient: "checking service 0xffe0 for handle: 12"
D NimBLEClient: "Got Notification for characteristic Characteristic: uuid: 0xffe1, handle: 12 0x000c, props:  0x1c
Descriptor: uuid: 0x2902, handle: 13"
D NimBLEClient: "Invoking callback for notification on characteristic Characteristic: uuid: 0xffe1, handle: 12 0x000c, props:  0x1c
Descriptor: uuid: 0x2902, handle: 13"
D NimBLEClient: "Got Client event "
D NimBLEClient: "Notify Recieved for handle: 12"
D NimBLEClient: "checking service 0xffe0 for handle: 12"
D NimBLEClient: "Got Notification for characteristic Characteristic: uuid: 0xffe1, handle: 12 0x000c, props:  0x1c
Descriptor: uuid: 0x2902, handle: 13"
D NimBLEClient: "Invoking callback for notification on characteristic Characteristic: uuid: 0xffe1, handle: 12 0x000c, props:  0x1c
Descriptor: uuid: 0x2902, handle: 13"
N: Subject: /BTtoMQTT/200507161361
N: Received json : {"model":"DT24","id":"20:05:07:16:13:61","volt":15.6,"current":0,"power":0,"energy":0,"price":2.01}
D NimBLEClient: ">> disconnect()"
D NimBLEClient: "<< disconnect()"
D NimBLEClient: "Got Client event "
D NimBLEClientCallbacks: "onDisconnect: default"
D NimBLEClient: ">> deleteServices"
D NimBLERemoteService: ">> deleteCharacteristics"
D NimBLERemoteCharacteristic: ">> deleteDescriptors"
D NimBLERemoteCharacteristic: "<< deleteDescriptors"
D NimBLERemoteService: "<< deleteCharacteristics"
D NimBLEClient: "<< deleteServices"
N: BLE Connect end

@NorthernMan54
Copy link
Collaborator

Exception from BLE Connect begin

N: BLE Connect begin
D NimBLEClient: ">> connect(20:05:07:16:13:61)"
D NimBLEClient: "Got Client event "
I NimBLEClient: "Connected event"
D NimBLEClient: "Got Client event "
I NimBLEClient: "mtu update event; conn_handle=0 mtu=136"
I NimBLEClient: "Connection established"
D NimBLEClient: ">> deleteServices"
D NimBLEClient: "<< deleteServices"
D NimBLEClientCallbacks: "onConnect: default"
D NimBLEClient: "<< connect()"
D NimBLEClient: ">> getService: uuid: 0xffe0"
D NimBLEClient: ">> retrieveServices"
D NimBLEClient: "Service Discovered >> status: 0 handle: 10"
D NimBLERemoteService: ">> NimBLERemoteService()"
D NimBLERemoteService: "<< NimBLERemoteService(): 0xffe0"
D NimBLEClient: "Service Discovered >> status: 14 handle: -1"
D NimBLEClient: "<< << Service Discovered"
D NimBLEClient: "<< retrieveServices"
D NimBLERemoteService: ">> getCharacteristic: uuid: 0xffe1"
D NimBLERemoteService: ">> retrieveCharacteristics() for service: 0xffe0"
D NimBLERemoteService: "Characteristic Discovered >> status: 0 handle: 12"
D NimBLERemoteCharacteristic: ">> NimBLERemoteCharacteristic()"
D NimBLERemoteCharacteristic: "<< NimBLERemoteCharacteristic(): 0xffe1"
D NimBLERemoteService: "Characteristic Discovered >> status: 14 handle: -1"
D NimBLERemoteService: "<< Characteristic Discovered"
D NimBLEClient: "Got Client event "
D NimBLEClient: "Notify Recieved for handle: 12"
D NimBLEClient: "checking service 0xffe0 for handle: 12"
D NimBLEClient: "Got Notification for characteristic Characteristic: uuid: 0xffe1, handle: 12 0x000c, props:  0x1c"
D NimBLERemoteService: "<< retrieveCharacteristics()"
D NimBLERemoteCharacteristic: ">> setNotify(): Characteristic: uuid: 0xffe1, handle: 12 0x000c, props:  0x1c, 01"
D NimBLERemoteCharacteristic: ">> getDescriptor: uuid: 0x2902"
D NimBLERemoteCharacteristic: ">> retrieveDescriptors() for characteristic: 0xffe1"
D NimBLEClient: "Got Client event "
D NimBLEClient: "Notify Recieved for handle: 12"
D NimBLEClient: "checking service 0xffe0 for handle: 12"
D NimBLEClient: "Got Notification for characteristic Characteristic: uuid: 0xffe1, handle: 12 0x000c, props:  0x1c"
D NimBLEClient: "Invoking callback for notification on characteristic Characteristic: uuid: 0xffe1, handle: 12 0x000c, props:  0x1c"
N: Device not identified
D NimBLERemoteCharacteristic: ">> getDescriptor: uuid: 00002902-0000-1000-8000-00805f9b34fb"
D NimBLERemoteCharacteristic: ">> retrieveDescriptors() for characteristic: 0xffe1"
D NimBLERemoteCharacteristic: "Descriptor Discovered >> status: 0 handle: 13"
Guru Meditation Error: Core  0 panic'ed (LoadProhibited). Exception was unhandled.
Core 0 register dump:
PC      : 0x401abcfb  PS      : 0x00060e30  A0      : 0x800e52c1  A1      : 0x3ffdf760  
A2      : 0xffe14910  A3      : 0x3f4049fc  A4      : 0x3f406314  A5      : 0x3ffdf730  
A6      : 0x00000000  A7      : 0x0000000d  A8      : 0x8013ca20  A9      : 0x3ffdf700  
A10     : 0x401aac04  A11     : 0x3ffc8b18  A12     : 0x3f406314  A13     : 0x0000001f  
A14     : 0x00000001  A15     : 0x00000001  SAR     : 0x00000004  EXCCAUSE: 0x0000001c  
EXCVADDR: 0xffe14930  LBEG    : 0x400014fd  LEND    : 0x4000150d  LCOUNT  : 0xfffffff9  

ELF file SHA256: 0000000000000000

Backtrace: 0x401abcfb:0x3ffdf760 0x400e52be:0x3ffdf780 0x400ec4d7:0x3ffdf7b0 0x400ec552:0x3ffdf7d0 0x400edaea:0x3ffdf810 0x400e85ee:0x3ffdf830 0x400e8230:0x3ffdf870 0x400f11aa:0x3ffdf8a0 0x400ef019:0x3ffdf8d0 0x400ef02b:0x3ffdf8f0 0x400f774e:0x3ffdf910 0x400e4236:0x3ffdf930 0x40090d3e:0x3ffdf950
  #0  0x401abcfb:0x3ffdf760 in NimBLERemoteCharacteristic::getRemoteService() at .pio/libdeps/ddd-bt-rtl/NimBLE-Arduino/src/NimBLERemoteCharacteristic.cpp:345
  #1  0x400e52be:0x3ffdf780 in NimBLERemoteCharacteristic::descriptorDiscCB(unsigned short, ble_gatt_error const*, unsigned short, ble_gatt_dsc const*, void*) at .pio/libdeps/ddd-bt-rtl/NimBLE-Arduino/src/NimBLERemoteCharacteristic.cpp:319
  #2  0x400ec4d7:0x3ffdf7b0 in ble_gattc_disc_all_dscs_cb at .pio/libdeps/ddd-bt-rtl/NimBLE-Arduino/src/nimble/host/src/ble_gattc.c:623
  #3  0x400ec552:0x3ffdf7d0 in ble_gattc_disc_all_dscs_rx_idata at .pio/libdeps/ddd-bt-rtl/NimBLE-Arduino/src/nimble/host/src/ble_gattc.c:623
  #4  0x400edaea:0x3ffdf810 in ble_gattc_rx_find_info_idata at .pio/libdeps/ddd-bt-rtl/NimBLE-Arduino/src/nimble/host/src/ble_gattc.c:623
  #5  0x400e85ee:0x3ffdf830 in ble_att_clt_rx_find_info at .pio/libdeps/ddd-bt-rtl/NimBLE-Arduino/src/nimble/host/src/ble_att_clt.c:594
  #6  0x400e8230:0x3ffdf870 in ble_att_rx at .pio/libdeps/ddd-bt-rtl/NimBLE-Arduino/src/nimble/host/src/ble_att.c:554
  #7  0x400f11aa:0x3ffdf8a0 in ble_hs_hci_evt_acl_process at .pio/libdeps/ddd-bt-rtl/NimBLE-Arduino/src/nimble/host/src/ble_hs_hci_evt.c:959
  #8  0x400ef019:0x3ffdf8d0 in ble_hs_process_rx_data_queue at .pio/libdeps/ddd-bt-rtl/NimBLE-Arduino/src/nimble/nimble_npl_os.h:109
  #9  0x400ef02b:0x3ffdf8f0 in ble_hs_event_rx_data at .pio/libdeps/ddd-bt-rtl/NimBLE-Arduino/src/nimble/nimble_npl_os.h:109
  #10 0x400f774e:0x3ffdf910 in ble_npl_event_run at .pio/libdeps/ddd-bt-rtl/NimBLE-Arduino/src/nimble/nimble_npl_os.h:121
      (inlined by) nimble_port_run at .pio/libdeps/ddd-bt-rtl/NimBLE-Arduino/src/porting/nimble/src/nimble_port.c:81
  #11 0x400e4236:0x3ffdf930 in NimBLEDevice::host_task(void*) at .pio/libdeps/ddd-bt-rtl/NimBLE-Arduino/src/NimBLEDevice.cpp:859 (discriminator 1)
  #12 0x40090d3e:0x3ffdf950 in vPortTaskWrapper at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/port.c:355 (discriminator 1)

Rebooting...

@h2zero
Copy link
Collaborator Author

h2zero commented Jun 16, 2021

Thanks @NorthernMan54, that's very helpful. Looks like a timing issue, I'll look into it now.

@h2zero
Copy link
Collaborator Author

h2zero commented Jun 16, 2021

@NorthernMan54 I just pushed a commit to the branch here https://github.com/h2zero/OpenMQTTGateway/tree/bugfix/ble-connect. Sorry to bother you testing this but I do not have access to that device or I would test it myself. The issue seems to be that it sends notifications constantly regardless of subscribing, so if this doesn't work we might simply be able to remove the subscribe call completely and just wait for the data.

@NorthernMan54
Copy link
Collaborator

I started it about 15 minutes ago, and no exceptions yet. Going to leave it overnight and see what happens.

PS I thought this should be before the subscribe, to ensure it was initialed prior to the subscribe as I had seen cases where the response was received prior to the taskhandle being initialized.

https://github.com/h2zero/OpenMQTTGateway/blob/ebd865c072ad54e185e98066a5cd91125823e122/main/ZgatewayBLEConnect.ino#L152

@h2zero
Copy link
Collaborator Author

h2zero commented Jun 16, 2021

I started it about 15 minutes ago, and no exceptions yet. Going to leave it overnight and see what happens.

Awesome, thanks!

I thought this should be before the subscribe, to ensure it was initialed prior to the subscribe as I had seen cases where the response was received prior to the taskhandle being initialized.

I re balanced that in the notification handler, this way it should avoid the timing issue, at least that's what I'm hoping for.

@NorthernMan54
Copy link
Collaborator

That fix nailed it

{"uptime":38168,
"version":"ddd-bt-rtl-v0.9.6-42-g151bac2[bugfix/ble-connect]",
"freemem":109944,
"freestack":3968,

@h2zero
Copy link
Collaborator Author

h2zero commented Jun 16, 2021

Thanks! I'll merge those changes into this PR.

@1technophile 1technophile added this to the v0.9.7 milestone Jun 16, 2021
h2zero added a commit to h2zero/OpenMQTTGateway that referenced this pull request Jun 17, 2021
Uses 1technophile#979

This allows reading and writing BLE characteristics from an MQTT message.

Example format:
mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoBT/config -m '{"ble_write_address":"AA:BB:CC:DD:EE:FF", "ble_write_service":"cba20d00-224d-11e6-9fb8-0002a5d5c51b", "ble_write_char":"cba20002-224d-11e6-9fb8-0002a5d5c51b", "ble_write_value":"TEST", "ttl":4 }'
mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoBT/config -m '{"ble_read_address":"30:AE:A4:7C:3C:A6", "ble_read_service":"cba20d00-224d-11e6-9fb8-0002a5d5c51b", "ble_read_char":"cba20002-224d-11e6-9fb8-0002a5d5c51b", "ttl": 2 }'
The ttl parameter is the number of calls to connect (defaults to 1), which occur after the BLE scan completes.

A response is provided over MQTT in the format:
write : {"id":"30:AE:A4:7C:3C:A6","service":"cba20d00-224d-11e6-9fb8-0002a5d5c51b","characteristic":"cba20002-224d-11e6-9fb8-0002a5d5c51b","write":"TEST","success":true}
read : {"id":"30:AE:A4:7C:3C:A6","service":"cba20d00-224d-11e6-9fb8-0002a5d5c51b","characteristic":"cba20002-224d-11e6-9fb8-0002a5d5c51b","read":"TEST","success":true}
@1technophile
Copy link
Owner

Thanks for the PR!

@1technophile 1technophile merged commit 792807e into 1technophile:development Jun 17, 2021
h2zero added a commit to h2zero/OpenMQTTGateway that referenced this pull request Jun 18, 2021
Uses 1technophile#979

This allows reading and writing BLE characteristics from an MQTT message.

Example format:
mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoBT/config -m '{"ble_write_address":"AA:BB:CC:DD:EE:FF", "ble_write_service":"cba20d00-224d-11e6-9fb8-0002a5d5c51b", "ble_write_char":"cba20002-224d-11e6-9fb8-0002a5d5c51b", "ble_write_value":"TEST", "ttl":4 }'
mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoBT/config -m '{"ble_read_address":"30:AE:A4:7C:3C:A6", "ble_read_service":"cba20d00-224d-11e6-9fb8-0002a5d5c51b", "ble_read_char":"cba20002-224d-11e6-9fb8-0002a5d5c51b", "ttl": 2 }'
The ttl parameter is the number of calls to connect (defaults to 1), which occur after the BLE scan completes.

A response is provided over MQTT in the format:
write : {"id":"30:AE:A4:7C:3C:A6","service":"cba20d00-224d-11e6-9fb8-0002a5d5c51b","characteristic":"cba20002-224d-11e6-9fb8-0002a5d5c51b","write":"TEST","success":true}
read : {"id":"30:AE:A4:7C:3C:A6","service":"cba20d00-224d-11e6-9fb8-0002a5d5c51b","characteristic":"cba20002-224d-11e6-9fb8-0002a5d5c51b","read":"TEST","success":true}
1technophile pushed a commit that referenced this pull request Jun 22, 2021
* EXPERIMENTAL: Ble read/write characteristics over MQTT.

Uses #979

This allows reading and writing BLE characteristics from an MQTT message.

Example format:
mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoBT/config -m '{"ble_write_address":"AA:BB:CC:DD:EE:FF", "ble_write_service":"cba20d00-224d-11e6-9fb8-0002a5d5c51b", "ble_write_char":"cba20002-224d-11e6-9fb8-0002a5d5c51b", "ble_write_value":"TEST", "ttl":4 }'
mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoBT/config -m '{"ble_read_address":"30:AE:A4:7C:3C:A6", "ble_read_service":"cba20d00-224d-11e6-9fb8-0002a5d5c51b", "ble_read_char":"cba20002-224d-11e6-9fb8-0002a5d5c51b", "ttl": 2 }'
The ttl parameter is the number of calls to connect (defaults to 1), which occur after the BLE scan completes.

A response is provided over MQTT in the format:
write : {"id":"30:AE:A4:7C:3C:A6","service":"cba20d00-224d-11e6-9fb8-0002a5d5c51b","characteristic":"cba20002-224d-11e6-9fb8-0002a5d5c51b","write":"TEST","success":true}
read : {"id":"30:AE:A4:7C:3C:A6","service":"cba20d00-224d-11e6-9fb8-0002a5d5c51b","characteristic":"cba20002-224d-11e6-9fb8-0002a5d5c51b","read":"TEST","success":true}

* Add generic BLE connect class and read data as hex.

* Add value_type parameter and document usage

* Add note to docs
@h2zero h2zero deleted the ble-connect branch August 3, 2021 01:38
JeroenWiersma pushed a commit to JeroenWiersma/OpenMQTTGateway that referenced this pull request Oct 18, 2025
JeroenWiersma pushed a commit to JeroenWiersma/OpenMQTTGateway that referenced this pull request Oct 18, 2025
* EXPERIMENTAL: Ble read/write characteristics over MQTT.

Uses 1technophile#979

This allows reading and writing BLE characteristics from an MQTT message.

Example format:
mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoBT/config -m '{"ble_write_address":"AA:BB:CC:DD:EE:FF", "ble_write_service":"cba20d00-224d-11e6-9fb8-0002a5d5c51b", "ble_write_char":"cba20002-224d-11e6-9fb8-0002a5d5c51b", "ble_write_value":"TEST", "ttl":4 }'
mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoBT/config -m '{"ble_read_address":"30:AE:A4:7C:3C:A6", "ble_read_service":"cba20d00-224d-11e6-9fb8-0002a5d5c51b", "ble_read_char":"cba20002-224d-11e6-9fb8-0002a5d5c51b", "ttl": 2 }'
The ttl parameter is the number of calls to connect (defaults to 1), which occur after the BLE scan completes.

A response is provided over MQTT in the format:
write : {"id":"30:AE:A4:7C:3C:A6","service":"cba20d00-224d-11e6-9fb8-0002a5d5c51b","characteristic":"cba20002-224d-11e6-9fb8-0002a5d5c51b","write":"TEST","success":true}
read : {"id":"30:AE:A4:7C:3C:A6","service":"cba20d00-224d-11e6-9fb8-0002a5d5c51b","characteristic":"cba20002-224d-11e6-9fb8-0002a5d5c51b","read":"TEST","success":true}

* Add generic BLE connect class and read data as hex.

* Add value_type parameter and document usage

* Add note to docs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants