This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Regarding Python Scripts for nrf52840 to connect and transfer data.

Hi ,

What I am trying:- 

-connect the custom board with nrf52840 chip using nrf52 dongle on pc with python.

-Already set some commands on custom board so when my pc (via dongle) connects to custom board, I want to send some commands and accordingly receive the response.

For this I got my hands-on nrf dongle to give my laptop a BLE access.

And I am trying to access the board using dongle (using python script) and I am getting below error,


raise NordicSemiException('Failed to {}. Error code: {}'.format(wrapped.__name__, err_code))
pc_ble_driver_py.exceptions.NordicSemiException: Failed to open. Error code: 13.

Would anyone able to tell me how do i resolve this. 

I want to have python code that can connect and do some data transfer over BLE using Dongle connected to my pc and I have tried with hart_rate_monitor unsuccessfully. 

EDIT 1:- I am able to scan and connect my board using the nRF Connect windows app. Same I wanted to implement over python.

Thanks,

Hinesh

  • Hi 

    From your suggestions I am able to connect to specific device via dongle/dk connected to any com port on my pc.

    Thank you very much for this.

    While going through the forums and also over internet I found for read and write operation gatt libraries are used in python.

    Well, I am using windows and I was not able to find the same for windows in python for read and write from my nordic board via my pc using python.

    I can find below function in ble_driver.py. but 

    def ble_gattc_write(self, conn_handle, write_params)
    def ble_gattc_read(self, conn_handle, handle, offset)
    (using the BLEDriver class I tried, but not success ,May be I am doing something wrong here or lacking knowledge.)

    It will be good if you can input here,

    Thanks, 

  • Hi,

    There is a close mapping between those python functions and the SoftDevice API. More specifically, the functions that you refer to correspond to sd_ble_gattc_write() and sd_ble_gattc_read().

    Please note that the current version of pc-ble-driver-py uses the s132 SoftDevice version 3.1.0. We are currently working on a new version of pc-ble-driver using s132 and s140 versions 6.1.0, and pc-ble-driver-py is likely to be updated further down the road.

    Unfortunately we do not seem to have any other examples than the ones in the python/pc-ble-driver-py/examples folder.

    Regards,
    Terje

  • Hi 

    Thank you very much for the input.

    From example Hart rate collector, I have made some changes require to connect to my device (having soft device 140).

    I am able to scan and I also thought I am able to connect (by hard coding the device mac and name on the serial port) as well.

    But I am getting below error.,

    "Received advertisment report, address: 0xAABBCCDDEEFF, device_name: MYDEVICE
    New connection: 0
    ATT MTU exchanged: conn_handle=0 att_mtu=23
    ATT MTU exchange response: conn_handle=0
    Traceback (most recent call last):
      File "C:/My_Projects/Script.py", line 256, in <module>
        main(serial_port)
      File "C:/My_Projects/Script.py", line 237, in main
        conn_handle = collector.connect_and_discover()
      File "C:/My_Projects/Script.py", line 187, in connect_and_discover
        self.adapter.enable_notification(new_conn, BLEUUID(BLEUUID.Standard.battery_level))
      File "C:\Python27\lib\site-packages\pc_ble_driver_py\ble_driver.py", line 125, in wrapper
        err_code = wrapped(*args, **kwargs)
      File "C:\Python27\lib\site-packages\pc_ble_driver_py\ble_adapter.py", line 254, in enable_notification
        raise NordicSemiException('CCCD not found')
    pc_ble_driver_py.exceptions.NordicSemiException: CCCD not found
    
    Process finished with exit code 1
    "

    I checked in

    self.adapter.enable_notification(new_conn, BLEUUID(BLEUUID.Standard.battery_level))

    Where I have tried to input my UUID as below,

    self.adapter.enable_notification(new_conn,MY_UUID, BLEUUID(BLEUUID.Standard.battery_level))

    From the file ble_driver.py
    def enable_notification(self, conn_handle, uuid):

    where
    I am passing collection handle but
    handle = self.db_conns[conn_handle].get_cccd_handle(uuid)

    still I get NONE on conn_handle. so is it my device that is not sending or I am not sharing proper UUID?
    The UUID IS checked by me for correctness, But If that's not correct I can correct by trying.

    It will be really helpful to have your input So that I can do read and write to my device via nordic dongle.

    By the way:- I am working with nordic dongle to remove other hw requirement possibility.
    we have checked the communication working but by different hw to verify the ID's on customized device.
    and we successfully checked and received data.

    Here your input will be very very helpful to complete read and write.
    Also to check if that's the right way.


    Thanks,
    Hinesh
  • Hi,

    Yes, that error would be expected if you try enable_notification() with a characteristic UUID that has not been found on the device. For instance, the line...

    self.adapter.enable_notification(new_conn, BLEUUID(BLEUUID.Standard.battery_level))

    ...will try to enable notifications on the Bluetooth SIG specified Battery Level characteristic (i.e. UUID 0x2A19). If you use a custom UUID then you do not have a BLEUUID() but a full 128 bit UUID. Now, if your MY_UUID is correctly formatted then you should be able to use

    self.adapter.enable_notification(new_conn, MY_UUID)

    You can connect to the device from nRF Connect in order to investigate what characteristics (and UUIDs) are present on the device.

    Regards,
    Terje

  • Hi ,

    This are good suggestions and I have tried them as well without success.

    in self.adapter.enable_notification(new_conn,MY_UUID) I kept and also double checked in nRF connect app but still not able to send data.

    When I run the script

    I am able to scan and connect to my device.(provide your input for below debug output)

    Connected to pydev debugger (build 181.4892.64)
    Serial port used: COM10
    Enabling larger ATT MTUs
    Received advertisment report, address: 0x5031ADC0000A, device_name: MY_DEVICE
    New connection: 0
    ATT MTU exchanged: conn_handle=0 att_mtu=23
    ATT MTU exchange response: conn_handle=0

    The UUID I am passing I have also checked in python debug and it's same.

    And on Below line From file ble_adapter.py in

    def enable_notification(self, conn_handle, uuid):
    I went to for more debug.

    and I found is
    assert isinstance(uuid, BLEUUID), 'Invalid argument type'
    AssertionError: Invalid argument type

    I am not able to understand what is BLEUUID here. I changed BLEUUID in
    self.adapter.enable_notification to MY_UUID. and Still no connection.

    could you help in here. Also If you can tell about read and write function which
    I intent to use so that I will complete my script. and once tested
    I can share here so other can also be benefited.


    Thanks,

Related