LoPy stops detecting BLE advertisements
-
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?
-
@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. -
@jmarcelino Thanks J. If there's anything else I can try meanwhile that might shed more light, please let me know.
-
This post is deleted! -
@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.
-
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.
-
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.
-
@jmarcelino Hi. Have you had a chance to work on this?
Regards,
Alan. -
@alanm101
Starting on it tonight! I got this to be partially funded by my current client so I’ll have more time for it :) -
@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() -
@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 ?
-
Wow...
On 1.10.1.b1 it's really, really, really better !
...did I say "really" ?
-
Thanks! I will test this weekend.
-
@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.
-
@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 printgc.mem_free()in the code to see memory consumption?
And do you havegc.collect()in the code or you have enabled automatic garbage collector? -
@livius Hi,
Been there, done that. Unfortunately the culprit
is notdoes 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. -
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. -
@duffo64
good finding on github.
I saw this kind of assert on the forum. -
Yet another update:
This is a WiPy 1.0 with a not-so-recent firmware. Running since two weeks and counting. BLE, WiFi and MQTT are running without hiccups :D
{"ts":1517336479,"pmac":"24xxxxxxxxxx","up":1302802,"fw":"1.12.0.b1","ip":"192.168.0.111"}
-
Hi @duffo64,
Can you clarify what was the change that made the difference in the end?
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