Send data from WiPy to LoPy with BLE


  • Pybytes Beta

    Hi,

    I' m trying to send data (I just need to send a temperature value) via BLE from WiPy to LoPy with the example posted on the offical documentation.

    The boards connects properly, but when the Client tries to get the services list (services = conn.services()) it doesn't succeed.

    LoPy (Client):

    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) == 'WiPy':
          try:
              conn = bt.connect(adv.mac)
              services = conn.services()
              print(services)
              for service in services:
                  print(service.uuid())
                  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)
    

    WiPy (Server):

    from network import Bluetooth
    import pycom
    
    pycom.heartbeat(False)
    bluetooth = Bluetooth()
    bluetooth.set_advertisement(name='WiPy', service_uuid=b'1234567890123456')
    
    def conn_cb (bt_o):
        events = bt_o.events()
        if  events & Bluetooth.CLIENT_CONNECTED:
            print("Client connected")
            pycom.rgbled(0x007f00) # green
        elif events & Bluetooth.CLIENT_DISCONNECTED:
            print("Client disconnected")
            pycom.rgbled(0x7f0000) # red
    
    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)
    
    char1_read_counter = 0
    def char1_cb_handler(chr):
        global char1_read_counter
        char1_read_counter += 1
    
        events = chr.events()
        if  events & Bluetooth.CHAR_WRITE_EVENT:
            print("Write request with value = {}".format(chr.value()))
        else:
            if char1_read_counter < 3:
                print('Read request on char 1')
            else:
                return 'ABC DEF'
    
    char1_cb = chr1.callback(trigger=Bluetooth.CHAR_WRITE_EVENT | Bluetooth.CHAR_READ_EVENT, handler=char1_cb_handler)
    
    srv2 = bluetooth.service(uuid=1234, isprimary=True)
    
    chr2 = srv2.characteristic(uuid=4567, value=0x1234)
    char2_read_counter = 0xF0
    def char2_cb_handler(chr):
        global char2_read_counter
        char2_read_counter += 1
        if char2_read_counter > 0xF1:
            return char2_read_counter
    
    char2_cb = chr2.callback(trigger=Bluetooth.CHAR_READ_EVENT, handler=char2_cb_handler)
    


  • @nagarjuna-reddy same here



  • @amartinez
    hi
    i have same problem like
    services=conn.services() is not started how to solve if you get a answer.


  • Pybytes Beta

    @bucknall Thank you so much! I' m a bit desesperated with this!



  • Hi @amartinez,

    I've been testing this out this morning and it appears to be a bug in the connecting device code - I've just been speaking with Daniel and he's going to debug it today. I'll keep you in the loop and you can see the github issue here - https://github.com/pycom/pycom-micropython-sigfox/issues/15.


  • Pybytes Beta

    I've updated the WiPy (Server) code with Alex's help, but it still not working.

    from network import Bluetooth
    import pycom
    
    pycom.heartbeat(False)
    bluetooth = Bluetooth()
    bluetooth.set_advertisement(name='WiPy', service_uuid=b'1234567890123456')
    pycom.rgbled(0x00007f)  # Blue
    
    def conn_cb(bt_o):
        events = bt_o.events()
        if  events & Bluetooth.CLIENT_CONNECTED:
            print("Client connected")
            pycom.rgbled(0x007f00) # green
        elif events & Bluetooth.CLIENT_DISCONNECTED:
            print("Client disconnected")
            pycom.rgbled(0x7f0000) # red
    
    bluetooth.callback(trigger=Bluetooth.CLIENT_CONNECTED | Bluetooth.CLIENT_DISCONNECTED, handler=conn_cb)
    
    bluetooth.advertise(True)
    
    srv1 = bluetooth.service(uuid=b'1234567890123456', isprimary=True)
    char1 = srv1.characteristic(uuid=b'ab34567890123456', value=25)
    
    temperature = 27   #Temperature value
    char1.value(temperature)
    
    srv1.start()
    

  • Pybytes Beta

    @livius done, sorry.



  • @amartinez
    Will be better if you post textual data not images
    to post code you can surround it by triple ```
    link to other possible formatting
    http://commonmark.org/help/


Log in to reply
 

Pycom on Twitter