Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    Reading data from bluetooth device

    Scheduled Pinned Locked Moved LoPy
    bluetoothgatt
    7 Posts 3 Posters 3.0k Views 3 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S Offline
      sam_uk
      last edited by

      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?

      1 Reply Last reply Reply Quote 0
      • sebS Offline
        seb
        last edited by

        @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/

        S 1 Reply Last reply Reply Quote 0
        • S Offline
          sam_uk @seb
          last edited by

          @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

          1 Reply Last reply Reply Quote 0
          • sebS Offline
            seb
            last edited by

            @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

            1 Reply Last reply Reply Quote 0
            • L Offline
              luc hanneuse
              last edited by

              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

              1 Reply Last reply Reply Quote 0
              • sebS Offline
                seb
                last edited by

                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

                L 1 Reply Last reply Reply Quote 0
                • L Offline
                  luc hanneuse @seb
                  last edited by

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

                  1 Reply Last reply Reply Quote 0

                  Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                  Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                  With your input, this post could be even better 💗

                  Register Login
                  • 1 / 1
                  • First post
                    Last post

                  Pycom on Twitter

                  Pycom