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

    Bluetooth WiPy client connecting to WiPy peripheral

    Scheduled Pinned Locked Moved WiPy 2&3
    bluetooth
    10 Posts 5 Posters 4.2k Views 5 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.
    • L Offline
      lcgeldenhuys
      last edited by

      Hi,

      I have a WiPy setup as a BLE peripheral as per the example:

      import binascii
      from network import Bluetooth
      from machine import Timer
      
      bt_periph = Bluetooth()
      bt_periph.set_advertisement(name = 'mag_test', service_uuid = b'1234567890123456')
      
      def conn_cb (bt_o):
          events = bt_o.events()
          if  events & Bluetooth.CLIENT_CONNECTED:
              bt_o.advertise(False)
              print("Client connected")
          elif events & Bluetooth.CLIENT_DISCONNECTED:
              bt_o.advertise(True)
              print("Client disconnected")
      
      bt_periph.callback(trigger = Bluetooth.CLIENT_CONNECTED | Bluetooth.CLIENT_DISCONNECTED, handler = conn_cb)
      bt_periph.advertise(True)
      srv_test = bt_periph.service(uuid = b'1234567890123456', isprimary = True, nbr_chars = 1)
      blaster = srv_test.characteristic(uuid = b'ab34567890123456', properties = Bluetooth.PROP_READ, value = 55)
      
      def blaster_cb(chr):
          print("Read request, returning value = {}".format(blaster.value()))
      
      blaster_cb = blaster.callback(trigger = Bluetooth.CHAR_READ_EVENT, handler = blaster_cb)
      

      I also have a WiPy setup as a client, trying to connect so I can read the service characteristic. However, whilst the peripheral is telling me that the client is connected, the client tells me that it is not:

      import binascii
      from network import Bluetooth
      import time
      
      bt_client = Bluetooth()
      bt_client.start_scan(-1)
      
      while True:
      	adv = bt_client.get_adv()
      	if adv and bt_client.resolve_adv_data(adv.data, Bluetooth.ADV_NAME_CMPL) == 'mag_test':
      		print ("Found mag test peripheral")
      		print("RSSI : {}, MAC : {}".format(adv.rssi, binascii.hexlify(adv.mac)))
      		bt_client.stop_scan()
      		
      		try:
      			conn = bt_client.connect(adv.mac)
      			while not conn.isconnected():
      				conn = bt_client.connect(adv.mac)
      				time.sleep(0.100)
      
      			print("Connected...")
      
      			services = conn.services()
      		    
      			for service in services:
      				time.sleep(0.300)
      				if type(service.uuid()) == bytes:
      					print('Reading characteristics from service = {}'.format(service.uuid()))
      				else:
      					print('Reading characteristics from service = %x' % service.uuid())
      		      	
      				chars = service.characteristics()
      		      	
      				while conn.isconnected():
      					for idx, char in enumerate(chars):
      						if (char.properties() & Bluetooth.PROP_READ):
      							print("char.read : {}".format(char.read()))
      						time.sleep(1.000)
      		   	
      			print("Disconnected...")
      			break
      		
      		except Exception as ex:
      			print("Exception caught...")
      			print(ex)
      	
      	else:
      		time.sleep(0.050)
      

      The conn.isconnected() never returns True, despite the peripheral reporting a connection. I know the connection works, because I can connect and read the characteristic value with a smartphone application.

      If I ignore the False connection state and try to create a services object anyway services = conn.services() then an exception is generated with "connection is already closed"

      Any help for troubleshooting / hunting down the cause would be greatly appreciated.

      Lourens

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

        Also, tried using the example client code to connect to other peripherals, e.g. the Tile. Same behaviour, namely a mobile app (specifically https://itunes.apple.com/us/app/lightblue-explorer-bluetooth/id557428110?mt=8) is able to connect and interrogate the services and characteristics, but the WiPy fails.

        Using current firmware 1.6.5.b1

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

          I downgraded to 1.4.0.b1 as I was sure this worked some time ago, and indeed, it works again!

          I 1 Reply Last reply Reply Quote 0
          • I Offline
            Innocenzo @lcgeldenhuys
            last edited by Innocenzo

            @lcgeldenhuys So the problem is the WiPy firmware? I actually use the release 1.6.7.b1 and your code does not work.

            There is a fix for that or the only way to perform a BLE connection is to downgrade the firmware?

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

              I understand you're re-trying to connect but triggering a new connection ( conn = bt_client.connect() ) ten times a second sounds like a bad idea to me. I can't see why you'd have to do that but I'll do some tests later today.

              I 1 Reply Last reply Reply Quote 0
              • I Offline
                Innocenzo @jmarcelino
                last edited by

                @jmarcelino I try also to increase the sleep time (a connection every 2 seconds) but the problem persists

                1 Reply Last reply Reply Quote 0
                • I Offline
                  Innocenzo
                  last edited by

                  When power up the BLE I Have this messages:

                  BTDM CONTROLLER VERSION: 010101
                  BTDM ROM VERSION 0101
                  BD_ADDR: 24:0A:C4:XX:XX:XX
                  NVDS MAGIC FAILED
                  RF Init OK with coex
                  Enable Classic BT
                  Enable Low Energy

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

                    @Innocenzo
                    That's OK. As we all know there is no magic, only great science and engineering. ;-)

                    Seriously now, we all get that message it's related to storage of BLE bonding keys in the NVS ( non-volatile storage ) area which isn't ready yet. Nothing to worry about.

                    1 Reply Last reply Reply Quote 1
                    • Y Offline
                      yunying
                      last edited by

                      I have the same problem with two Lopy, I lose connection immediately:)

                      1 Reply Last reply Reply Quote 0
                      • D Offline
                        daniel
                        last edited by

                        Hello,

                        Thanks for pointing out this bug. It has been fixed on the latest release 1.7.1.b1.

                        Cheers,
                        Daniel

                        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