I switched things over to use Bluetooth.println(...) and NRF.setConnectionInterval(7.5). I also consolidated all the logs into a single call.
But then I started looking into ble advertising because for my use case, one direction communication is fine and if I could get 20 ms between accelerometer updates that would be awesome. I found some sample code in the puck.js documentation, and modified it like so:
var presses = 0;
NRF.setConnectionInterval(7.5)
NRF.setAdvertising({},{name: "Puck", discoverable: true, manufacturer: 0x0590, manufacturerData:[presses]});
function updateAdvertising() {
//print (E.getTemperature())
//print (E.getBattery())
presses++;
NRF.setAdvertising({},{manufacturer: 0x0590, manufacturerData:[presses]});
}
// Update advertising now
updateAdvertising();
// Update advertising every 20 miliseconds...
setInterval(updateAdvertising, 1);
setInterval(()=>presses++, 1);
Yes, those are intentionally very fast intervals. I was just banging my head against it trying to get it to update quickly.
on macOS, here is my python script:
import asyncio
import time
from bleak import BleakScanner
last_update_time = None
def handle_advertisement(device, advertisement_data):
global last_update_time
current_time = time.time()
if last_update_time is not None:
time_since_last_update = current_time - last_update_time
delta_time_str = f", Delta Time: {time_since_last_update:.2f} seconds"
else:
delta_time_str = ""
last_update_time = current_time
if advertisement_data.local_name == "Puck":
print(advertisement_data.local_name)
manufacturer_data = advertisement_data.manufacturer_data
if 1424 in manufacturer_data:
data = manufacturer_data[1424]
data_str = ''.join(format(x, '02x') for x in data)
print(f"Manufacturer Data: {data_str}{delta_time_str}")
async def main():
scanner = BleakScanner()
scanner.register_detection_callback(handle_advertisement)
await scanner.start()
try:
while True:
await asyncio.sleep(0.1) # Reduced sleep time for potentially more frequent updates
except KeyboardInterrupt:
await scanner.stop()
if __name__ == "__main__":
asyncio.run(main())
This doesn't correctly parse my "presses" int, but for now i'm just looking at how fast it updates. This is what my script prints out:
Ok - so that's mostly good. But here's the weird part. In mac OS terminal, I visually see the updates coming in roughly once a second, even though delta time is a fraction of that.
Sooooo now i'm wondering if my mac BLE service is just buffering stuff up or something? If this is a dead end, I can go back to a direct blue tooth connection which definitely seems to be much faster. But wow it'd be cool if i could do this with advertising. :). Thanks!
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Ok - i made some progress here.
I switched things over to use
Bluetooth.println(...)
andNRF.setConnectionInterval(7.5)
. I also consolidated all the logs into a single call.But then I started looking into ble advertising because for my use case, one direction communication is fine and if I could get 20 ms between accelerometer updates that would be awesome. I found some sample code in the puck.js documentation, and modified it like so:
Yes, those are intentionally very fast intervals. I was just banging my head against it trying to get it to update quickly.
on macOS, here is my python script:
This doesn't correctly parse my "presses" int, but for now i'm just looking at how fast it updates. This is what my script prints out:
Ok - so that's mostly good. But here's the weird part. In mac OS terminal, I visually see the updates coming in roughly once a second, even though delta time is a fraction of that.
Sooooo now i'm wondering if my mac BLE service is just buffering stuff up or something? If this is a dead end, I can go back to a direct blue tooth connection which definitely seems to be much faster. But wow it'd be cool if i could do this with advertising. :). Thanks!