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

    Advertising data from 1 lopy, trying to read from another one

    Scheduled Pinned Locked Moved MicroPython
    bluetooth
    4 Posts 2 Posters 1.4k Views 2 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.
    • M Offline
      Morthen
      last edited by jmarcelino

      High,

      I want to communicate using bluetooth from one LoPy to another, one LoPy is the GATT server and the other one is the Central device. Problem is I can't connect my Central Device to my Gatt server.

      • list item
        Here is the GATT server code :

      And here is the Central device code :

      And here is the Central device console :

      I don't know why but the creation of the GATT service object isn't working.

      Any suggestion?

      1 Reply Last reply Reply Quote 0
      • J Offline
        jmarcelino
        last edited by

        Can you post your code as text please? It's really difficult to test and check as PNGs.

        You can put it between a

        ```
        Your code here
        ```

        block to keep the formatting.

        1 Reply Last reply Reply Quote 0
        • M Offline
          Morthen
          last edited by Morthen

          This is a classic example of GATT server code and central device code

          /******************************************/
          Here is the GATT server code fro the first lopy :

          from network import Bluetooth
          
          bluetooth = Bluetooth()
          bluetooth.set_advertisement(name='LoPy', service_uuid=b'1234567890123456')
          
          def conn_cb (bt_o):
              events = bt_o.events()
              if  events & Bluetooth.CLIENT_CONNECTED:
          	print("Client connected")
              elif events & Bluetooth.CLIENT_DISCONNECTED:
          	print("Client disconnected")
          
          bluetooth.callback(trigger=Bluetooth.CLIENT_CONNECTED | Bluetooth.CLIENT_DISCONNECTED, handler=conn_cb)
          
          bluetooth.advertise(True)
          
          srv1 = bluetooth.service(uuid=b'1234567890123456', isprimary=True)
          
          chr1 = srv1.characteristic(uuid=b'ab34567890123456', value=5)
          
          def char1_cb(chr):
              print("Write request with value = {}".format(chr.value()))
          
          char1_cb = chr1.callback(trigger=Bluetooth.CHAR_WRITE_EVENT, handler=char1_cb)
          
          srv2 = bluetooth.service(uuid=1234, isprimary=True)
          
          chr2 = srv2.characteristic(uuid=4567)
          

          /********************************************/

          /********************************************/

          This is the code of the central device on the second LoPy :

          from network import Bluetooth
          import time
          bt = Bluetooth()
          bt.start_scan(-1)
          
          while True:
            adv = bt.get_adv()
            if adv and bt.resolve_adv_data(adv.data, Bluetooth.ADV_NAME_CMPL) == 'Glucose':
                try:
                    print('1')
          	  conn = bt.connect(adv.mac)
          	  print('2')
          	  services = conn.services()
                    print('3')
          	  for service in services:
          	      time.sleep(0.050)
          	      if type(service.uuid()) == bytes:
          	          print('Reading chars from service = {}'.format(service.uuid()))
          	      else:
          	          print('Reading chars from service = %x' % service.uuid())
          	      chars = service.characteristics()
          	      for char in chars:
          	          if (char.properties() & Bluetooth.PROP_READ):
          	              print('char {} value = {}'.format(char.uuid(), char.read()))
          	  conn.disconnect()
          	  break
                except:
          	  print("Error while connecting or reading from the BLE device")
          	  break
            else:
                time.sleep(0.050)
          

          /**************************************/

          /*************************************/

          This is what I've acquired on the Central device consol after executing the two codes:

          1
          2
          Error while connecting or reading from the BLE device 
          

          /****************************************************/

          I don't know why but the connection is always closed, I can't connect my Central device to my Gatt server

          1 Reply Last reply Reply Quote 0
          • M Offline
            Morthen
            last edited by

            High,

            Can I have some news about my issue?
            I don't know how to solve it !

            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