Reading data from bluetooth device



  • Hi I've done a search but it's a bit unclear to me what the current state of bluetooth support is.

    Am I likely to have success connecting this device https://wiki.hackerspace.pl/projects:xiaomi-flora

    It looks like I need
    sudo apt-get install python-pip
    pip install paho-mqtt
    pip install gattlib

    Are these supported on LoPy?



  • @seb

    I write this code to be able to find where it doesn't work

    from network import Bluetooth
    import time
    bt = Bluetooth()
    
    print("searching")
    bt.start_scan(-1)
    
    print("connection")
    conn = bt.connect('C47C8D643FAF')
    
    print("connected")
    
    
    def characteristic_callback(char):
        v = int.from_bytes(char.value(), "little")
        print("Got new value: {}".format(v))
    
    print("before services")
    services = conn.services()
    print("services")
    for service in services:
        print("Service: {}".format(service.uuid()))
        if service.uuid() == b'XXXXXXXXXXXXX':
            characteristics = service.characteristics()
            for characteristic in characteristics:
                print("Characteristic: {}".format(characteristic.uuid()))
                if characteristic.uuid() == b'XXXXXXXXXXXX':
                    characteristic.callback(trigger=Bluetooth.CHAR_NOTIFY_EVENT,
                                            handler=characteristic_callback,
                                            arg=None)
    

    So, in the terminal it print
    searching
    connection
    connected
    before services

    So it never reach the line :

    services = conn.services()
    

    in the page https://wiki.hackerspace.pl/projects:xiaomi-flora , it's specified that "You need to write 0xA01F to the handle 0x0033 to enable real-time data reading "

    How can I write the value 0xA01F in the handle 0x0033 ?

    Thanks in advance for your precious help.



  • Hi,

    I dont have access to this specific device to test but you can do something like this:

    from network import Bluetooth
    import time
    
    bluetooth = Bluetooth()
    bluetooth.start_scan(10)
    
    while True:
        adv = bluetooth.get_adv()
        if adv and binascii.hexlify(adv.mac) == 'XXXXXXXXXX':
            try:
                print('1')
                conn = bluetooth.connect(adv.mac)
            except:
                bluetooth.start_scan(5)
                continue
            break
    
    
    def characteristic_callback(char):
        v = int.from_bytes(char.value(), "little")
        print("Got new value: {}".format(v))
    
    
    services = conn.services()
    for service in services:
        print("Service: {}".format(service.uuid()))
        if service.uuid() == b'XXXXXXXXXXXXX':
            characteristics = service.characteristics()
            for characteristic in characteristics:
                print("Characteristic: {}".format(characteristic.uuid()))
                if characteristic.uuid() == b'XXXXXXXXXXXX':
                    characteristic.callback(trigger=Bluetooth.CHAR_NOTIFY_EVENT,
                                            handler=characteristic_callback,
                                            arg=None)
    
    

    You will need to replace the XXX parts with values that match your sensor



  • Hi @seb ,

    Can you help me with the connection and those GATT instructions ?

    from network import Bluetooth
    import time
    bt = Bluetooth()
    
    print("searching")
    bt.start_scan(-1)
    
    print("connection")
    bt.connect('C47C8D643FAF') # Flower 
    print("connected")
    

    I just succeed in a connection.
    How can I go further ?

    How can I send those 0x0038 , 0xA01F , 0x0033 , 0x0035 and read the returned value ? as describe their : https://wiki.hackerspace.pl/projects:xiaomi-flora



  • @sam_uk said in Reading data from bluetooth device:

    gattlib

    Our devices do support GATT directly in our firmware, you can find examples of this in the link I provided



  • @seb

    Thanks, it seems like paho-mqtt exists in micropython, but not gattlib.

    I've found this alternate Esp2866 route so I think I'll pursue that for now.

    Thanks

    Sam



  • @sam_uk said in Reading data from bluetooth device:

    https://wiki.hackerspace.pl/projects:xiaomi-flora

    Hi,

    The commands you list are for a full installation of python, not micropython, which does not have pip. Please check out our bluetooth api documentation for details about how to use the bluetooth radio in our modules: https://docs.pycom.io/chapter/firmwareapi/pycom/network/bluetooth/


Log in to reply
 

Pycom on Twitter