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

    WiPy as an iBeacon

    Scheduled Pinned Locked Moved Discussion
    wipy2.0lopy blebeacon
    9 Posts 5 Posters 2.9k 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.
    • P Offline
      patrick
      last edited by patrick

      Have been struggling for a few days now getting the WiPy to advertise an iBeacon frame. I've read on this forum that it should be possible but till now without any luck.

      Does anyone have an example on how to do as the documentation on this is incomplete?

      S papasmurphP 2 Replies Last reply Reply Quote 0
      • S Offline
        surly @patrick
        last edited by

        @patrick I also need a better documentation to continue !

        J 1 Reply Last reply Reply Quote 0
        • papasmurphP Offline
          papasmurph @patrick
          last edited by

          @patrick Try this, courtesy of Abiro AB :).

          Also supports Eddystone-URL.

          from network import Bluetooth
          import pycom
          import time
          
          # https://docs.pycom.io/pycom_esp32/library/network.Bluetooth.html
          # https://support.kontakt.io/hc/en-gb/articles/201492492-iBeacon-advertising-packet-structure
          # https://github.com/google/eddystone/blob/master/protocol-specification.md
          # https://github.com/google/eddystone/tree/master/eddystone-url/
          # https://forum.pycom.io/topic/1120/creating-an-eddystone-beacon
          # https://www.mkompf.com/tech/eddystoneurl.html
          
          MODE_IBEACON = 1
          MODE_EDDYSTONEURL = 2
          mode = MODE_IBEACON
          
          def twocompl(x):
              if x < 0:
                  x += 256
          
              return x        
          
          timeSleep = 1
          
          bluetooth = Bluetooth()
          bluetooth.init()
          
          power1m = -59
          
          if mode == MODE_IBEACON:
              major = 22240
              minor = 234
          
              data = [
                  0x4c, 0x00, 0x02, 0x15, # Manufacturer data
                  0xA6, 0xE3, 0xE0, 0x06, 0xBB, 0xC4, 0x4D, 0xD1, 0x9C, 0xA3, 0x3E, 0x86, 0xC4, 0x95, 0x7D, 0xF0, # UUID (CliqTags)
                  (major >> 8) & 0xFF, major & 0xFF, # Major ID
                  (minor >> 8) & 0xFF, minor & 0xFF, # Minor ID
                  twocompl(power1m) # Power
              ]
          
              bluetooth.set_advertisement(name = None, manufacturer_data = bytes(data))
          elif mode == MODE_EDDYSTONEURL:
              url = 'https://www.abiro.com'
          
              uuid = [0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0xaa, 0xfe, 0x00, 0x00]
          
              url = url.replace('http://www.', chr(0))
              url = url.replace('https://www.', chr(1))
              url = url.replace('http://', chr(2))
              url = url.replace('https://', chr(3))
          
              url = url.replace('.com/', chr(0))
              url = url.replace('.org/', chr(1))
              url = url.replace('.edu/', chr(2))
              url = url.replace('.net/', chr(3))
              url = url.replace('.info/', chr(4))
              url = url.replace('.biz/', chr(5))
              url = url.replace('.gov/', chr(6))
              url = url.replace('.com', chr(7))
              url = url.replace('.org', chr(8))
              url = url.replace('.edu', chr(9))
              url = url.replace('.net', chr(10))
              url = url.replace('.info', chr(11))
              url = url.replace('.biz', chr(12))
              url = url.replace('.gov', chr(13))
          
              data = [
                  0xAA, 0xFE, # Eddystone ID
                  0x10, # Eddystone-URL
                  twocompl(power1m), # Power
              ]
          
              for c in url:
                  data.append(ord(c))
          
              bluetooth.set_advertisement(service_uuid = bytes(uuid), service_data = bytes(data))
          
          bluetooth.advertise(True)
          
          while True:
              time.sleep(timeSleep)
          
          P E 2 Replies Last reply Reply Quote 1
          • P Offline
            patrick @papasmurph
            last edited by patrick

            @papasmurph thx, your code works fine. I figured out whats wrong, if you put a name in this line

            bluetooth.set_advertisement(name = None, manufacturer_data = bytes(data))
            

            then the beacon advertisement doesn't work. I don't know if this is a bug or by design but it is easy to reproduce.

            J papasmurphP 2 Replies Last reply Reply Quote 0
            • J Offline
              jmarcelino @patrick
              last edited by

              @patrick
              There isn’t enough bytes in the (standard BLE 4.x) advertisement packet for both a name and beacon data, so if you have a name the extra beacon data is dropped

              1 Reply Last reply Reply Quote 0
              • papasmurphP Offline
                papasmurph @patrick
                last edited by

                @patrick I've seen beacons that include a name, that BLE scanners pick up, but it's not specified by iBeacon.

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

                  @papasmurph
                  In those cases the device name is sent in a scan response packet (as a response to a scan request sent by the WiPy), this different from the advertisement.

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

                    @surly
                    Have the answers above helped you? if you still have problems let me know please let us know what you're trying to do and where the problem is - I'll help you out

                    1 Reply Last reply Reply Quote 0
                    • E Offline
                      EchoBravoVictor @papasmurph
                      last edited by

                      @papasmurph You're code is not working in iBeacon mode with my WiPy. Any Idea ? No problem at all on Eddystone

                      I'm on 1.20.0.rc13

                      Thanks !

                      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