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

    Send data Lopy to LoPy via BLE

    Scheduled Pinned Locked Moved WiPy 2&3
    lopy blelopydata lopydata lora
    4 Posts 2 Posters 1.3k 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.
    • S Offline
      stefan85e
      last edited by stefan85e

      Re: Send data WiPy to LoPy via BLE

      I try found out how to send data between the devices...
      this is the code I have now:

      #from network import LoRa
      from network import Bluetooth
      
      import machine
      import socket
      import pycom
      import binascii
      import network
      from network import WLAN
      from machine import RTC
      
      bluetooth = Bluetooth()
      bluetooth.set_advertisement(name="stefan", manufacturer_data="lopy_v1")
      bluetooth.start_scan(-1) #Change to 10 to have it searching for 10 sec instead if forever
      
      adv = None
      
      while True:
          adv = bluetooth.get_adv()
          if adv and bluetooth.resolve_adv_data(adv.data, Bluetooth.ADV_NAME_CMPL) == 'LoPy':
      
              try:
                  bluetooth.connect(adv.mac)
      
                  print(bluetooth.resolve_adv_data(adv.data, Bluetooth.ADV_APPEARANCE))
                  print("Connected to device with addr = {}".format(binascii.hexlify(adv.mac)))
      
                  #bluetooth.connect(adv.mac)
                  pycom.heartbeat(False)
                  for cycles in range(1): # stop after 1 cycles
                      pycom.rgbled(0x000080) # blue
                      time.sleep(0.5)
                      pycom.rgbled(0x000000) # blue
                      time.sleep(0.5)
                      pycom.rgbled(0x000080) # blue
                      time.sleep(0.5)
                      pycom.rgbled(0x000000) # blue
      
                  print(bluetooth.resolve_adv_data(adv.data, Bluetooth.ADV_APPEARANCE))
      
              except:
                  #start scanning again
                  bluetooth.start_scan(5)
      
                  continue
              break
          else:
              if adv != None:
                  print('Hej')
      
      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)
      
              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()))
                      print('1')
                  else:
                      if char1_read_counter < 3:
                          print('Read request on char 1')
                          print('2')
                      else:
                          return 'ABC DEF'
                          print('3')
      
              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)
              print('4')
      

      I was thinking if it had do to with this one:

      characteristic.read()
      characteristic.write(value)

      https://docs.pycom.io/chapter/firmwareapi/pycom/network/bluetooth/gattccharacteristic.html

      J 1 Reply Last reply Reply Quote 0
      • J Offline
        jcaron @stefan85e
        last edited by

        @stefan85e Please edit your post and add ``` on separate lines before and after your code so it is properly formatted.

        Thanks!

        S 1 Reply Last reply Reply Quote 0
        • S Offline
          stefan85e @jcaron
          last edited by

          @jcaron what do you mean!?

          J 1 Reply Last reply Reply Quote 0
          • J Offline
            jcaron @stefan85e
            last edited by

            @stefan85e Looks like @seb made the edit for you in the meantime.

            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