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

    LoPy stops detecting BLE advertisements

    Scheduled Pinned Locked Moved Issues & Bugs
    lopybluetoothblebeaconibeacon
    123 Posts 15 Posters 84.1k Views 14 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.
    • J Offline
      jmarcelino @alanm101
      last edited by jmarcelino

      @alanm101
      What is this gatt_client.c code? do you mean the gatt_client project at https://github.com/espressif/esp-idf/tree/master/examples/bluetooth/gatt_client ?

      Did you change any esp_ble_scan_params_t there?

      I’m glad you got good results with IDF directly, but I still feel it’s not optimal. My plan is to use the HCI layer directly to scan beacons and skip 90% of the stack.

      A 1 Reply Last reply Reply Quote 0
      • A Offline
        alanm101 @jmarcelino
        last edited by

        @jmarcelino Yes, that's the code.

        Changes:
        Line 313: Changed time from 30s to 20000s.
        Lines 331 - 347 removed.

        I'm getting interesting results with further tests. Will post later.

        A 1 Reply Last reply Reply Quote 0
        • A Offline
          alanm101 @alanm101
          last edited by

          This is depressing.

          I put all my beacons in a faraday cage (aka microwave oven) and turned off all bluetooth devices. I checked with the Nordic app; no bluetooth signals. I then disabled bluetooth on my phone.

          I fired up a single ESP32 to broadcast an advertisement once per second, using the ble_adv example from the IDF. The serial output of the ESP32 confirmed 1 per second. Fired up the simple code posted before on the Lopy. After around 100 minutes, it stopped picking up beacon signals. After a hard reset, it started working again, so it was not the ESP32 that stopped advertising.

          One thing is strange. Even though the ESP32 is definitely advertising once per second (I can post the serial log and the code), the Lopy is picking up 2.3 - 2.5 advertisements per second. What? Is this expected behaviour? I fired up the Nordic app again and still just the ESP32 broadcasting.

          I'm beginning to think this is a lost cause. Controlled environment, controlled broadcast, one beacon broadcasting once per second, but still the Lopy fails. Anything else I should try before giving up on the Lopy?

          J A 3 Replies Last reply Reply Quote 0
          • J Offline
            jmarcelino @alanm101
            last edited by

            @alanm101
            Don’t give up yet :) This BLE scanning problems have been on my “to look into” list for far too long and I’ll look into it next week.

            A 2 Replies Last reply Reply Quote 0
            • A Offline
              alanm101 @jmarcelino
              last edited by

              @jmarcelino Thanks J. If there's anything else I can try meanwhile that might shed more light, please let me know.

              1 Reply Last reply Reply Quote 0
              • A Offline
                alanm101 @alanm101
                last edited by

                This post is deleted!
                1 Reply Last reply Reply Quote 0
                • A Offline
                  alanm101 @alanm101
                  last edited by

                  @alanm101 My ignorance knows no bounds. The adv is broadcast on 3 channels, hence the multiple events per broadcast. My new tests are <cough> compensating for that.

                  A 1 Reply Last reply Reply Quote 0
                  • A Offline
                    alanm101 @alanm101
                    last edited by alanm101

                    Test1: Single beacon using ESP32 at one second intervals.

                    Changes to ble_adv/main/app_bt.c
                    Lines 157-158:
                    uint16_t adv_intv_min = 256x3; // 160ms
                    uint16_t adv_intv_max = 256x3; // 160ms

                    <The 'x' is replaced with an asterisk. Some issue with uploading.>

                    Result: 2 hours with no problems.

                    Test2: Single beacon using ESP32 at one second intervals.

                    Changes to ble_adv/main/app_bt.c
                    Lines 157-158:
                    uint16_t adv_intv_min = 256x2; // 160ms
                    uint16_t adv_intv_max = 256x2; // 160ms

                    <The 'x' is replaced with an asterisk. Some issue with uploading.>

                    Result: 50 minutes, advs no longer detected.

                    A 1 Reply Last reply Reply Quote 0
                    • A Offline
                      alanm101 @alanm101
                      last edited by

                      I'm no longer convinced that this is a Lopy/uPython issue. I hacked raw C code and ran on a generic ESP32 using newly acquired knowledge. The code failed. It could be my hacks were wrong or perhaps the ESP32 or the IDF is screwed. I'm now trying new uPython code on the Lopy.

                      1 Reply Last reply Reply Quote 0
                      • A Offline
                        alanm101 @jmarcelino
                        last edited by

                        @jmarcelino Hi. Have you had a chance to work on this?

                        Regards,
                        Alan.

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

                          @alanm101
                          Starting on it tonight! I got this to be partially funded by my current client so I’ll have more time for it :)

                          A 1 Reply Last reply Reply Quote 0
                          • A Offline
                            alanm101 @jmarcelino
                            last edited by

                            @jmarcelino This hack is working for me so far. If I don't detect a BLE event within 10 seconds, I machine.reset().

                            from network import Bluetooth
                            import machine
                            import time
                            import binascii
                            import gc
                            
                            beaconlist = ['d3e0f6cd4ca9','e9f1db4814ef']
                            beaconevents = []
                            timelastdata = time.time()
                            
                            def new_adv_event(event):
                                global beaconlist, beaconevents, timelastdata
                                if event.events() == Bluetooth.NEW_ADV_EVENT:
                                    anydata = True
                                    while anydata:
                                        adv = bluetooth.get_adv()
                                        if adv != None:
                                            timelastdata = time.time()
                                            devid = binascii.hexlify(adv[0]).decode('utf-8')
                                            rssi = str(adv[3]*-1)
                                            if devid in beaconlist:
                                                if len(beaconevents) > 5:
                                                    beaconevents.pop(0)
                                                beaconevents.append([devid, rssi])  
                                        else:
                                            anydata = False
                            
                            print('Starting BLE scan')
                            bluetooth = Bluetooth()
                            bluetooth.callback(trigger = Bluetooth.NEW_ADV_EVENT, handler = new_adv_event)
                            bluetooth.init()
                            bluetooth.start_scan(-1)
                            
                            cycles = 0
                            p_in = machine.Pin('G17',machine.Pin.IN, pull=machine.Pin.PULL_UP)
                            
                            while True:
                                if p_in() == 0:
                                    print('pin')
                                    bluetooth.stop_scan()
                                    break
                            
                                cycles += 1
                                # Run garbage collector every 20 cycles.
                                if cycles%20 == 0:
                                    gc.collect()
                                # If no BLE event for 10 seconds, hard reset
                                if time.time() - timelastdata > 10:
                                    machine.reset()
                            
                            D 1 Reply Last reply Reply Quote 0
                            • D Offline
                              duffo64 @alanm101
                              last edited by duffo64

                              @alanm101
                              This is a small Arduino sketch on ESP32 Thing (Sparkfun) running without a glitch since this morning.
                              Moreover, it's happily seeing even my EMBC02 beacons. So, should I presume that in the end it's a micropython issue ?

                              0_1508254202656_uptime.png

                              /Claudio

                              D 1 Reply Last reply Reply Quote 1
                              • D Offline
                                duffo64 @duffo64
                                last edited by duffo64

                                Wow...

                                On 1.10.1.b1 it's really, really, really better !

                                ...did I say "really" ?

                                /Claudio

                                1 Reply Last reply Reply Quote 0
                                • A Offline
                                  alanm101
                                  last edited by

                                  Thanks! I will test this weekend.

                                  D 1 Reply Last reply Reply Quote 0
                                  • D Offline
                                    duffo64 @alanm101
                                    last edited by duffo64

                                    @alanm101
                                    Despite my initial enthusiasm, I must say that the situation is slightly better using 1.10.2.b1. Basically I had two issues:

                                    • my beacons were not recognized, and this is solved
                                    • callback for advertisements stops firing after a while, but I know that the program is not frozen somewhere. This still unsolved

                                    The very same script is loaded on four WiPy 2.0, two WiPy 3.0 and one LoPy. Only one WiPy 3.0 survived after 18 hours. Sigh.

                                    /Claudio

                                    L 1 Reply Last reply Reply Quote 0
                                    • L Offline
                                      livius @duffo64
                                      last edited by

                                      @duffo64
                                      If it survived only on wipy3, this can point me that memory is here problem (only guess).
                                      Did you tried track it down and print gc.mem_free() in the code to see memory consumption?
                                      And do you have gc.collect() in the code or you have enabled automatic garbage collector?

                                      think about people first, then computers

                                      D 1 Reply Last reply Reply Quote 0
                                      • D Offline
                                        duffo64 @livius
                                        last edited by duffo64

                                        @livius Hi,

                                        Been there, done that. Unfortunately the culprit is not does not seem lack of memory (one WiPy3 still working, the other went south like all WiPy 2).
                                        Anyway, I have collection in code as soon as a ble scan cycle ends.

                                        /Claudio

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

                                          Follow up:

                                          I restarted both WiPy 3 giving them another chance without modifying code at all, and this time was better: more than 24 hours of normal behavior without reboots or BLE lockups. Still, BLE acts randomly in the long run.
                                          BTW there is an IDF related issue on Github that could lead to weird results on the BLE side, as I read.

                                          /Claudio

                                          L 1 Reply Last reply Reply Quote 1
                                          • L Offline
                                            livius @duffo64
                                            last edited by

                                            @duffo64
                                            good finding on github.
                                            I saw this kind of assert on the forum.

                                            think about people first, then computers

                                            D 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
                                            • 2
                                            • 3
                                            • 4
                                            • 5
                                            • 6
                                            • 7
                                            • 4 / 7
                                            • First post
                                              Last post

                                            Pycom on Twitter

                                            Pycom