puckjs bleak python mac / read advertisements

Posted on
  • Hello,
    is annoying out there managed to read accelerometer datas of the puckjs thru advertisement (manufacturer data) using the bleak python library?
    I tried all the different tutorial on the subject but not luck :/

  • Try using the Espruino code from https://www.espruino.com/BLE+Advertising­#python

    And then use:

    #!python3
    # Scans for and outputs BLE advertising containing manufacturer data 0x0590
    # needs: `pip install bleak`
    import asyncio
    import array
    from bleak import BleakScanner
    from bleak import BleakClient
    
    async def main():
        stop_event = asyncio.Event()
        
        def callback(device, advertising_data):
            # print(advertising_data)  # debug
            if advertising_data.manufacturer_data and (0x0590 in advertising_data.manufacturer_data):  
              d = advertising_data.manufacturer_data[0x059­0]          
              print("Found Espruino Manufacturer data", device)
              if advertising_data.local_name:
                print("  Name: ", advertising_data.local_name);
              print("  Data: ", ", ".join(hex(b) for b in d))
              # You could call stop_event.set() here if you found the device you want
              # and scanning will be stopped
            pass
    
        async with BleakScanner(callback) as scanner:
            await stop_event.wait()
           
    # Start scanning       
    asyncio.run(main())
    

    (I'll update the above link with the Bleak example - previously it only included code for Python Bluepy).

    It should be pretty straightforward to modify it to output accelerometer data, but let me know if you have any issues

  • Thanks @Gordon, works great!
    what would be the best way to advertise more frequently ? ( the fastest we can)

  • Just use NRF.setAdvertising(..., {interval:20}) on the Puck - 20ms is the fastest I think (50 times/sec)

  • I did:
    NRF.setTxPower(4);
    NRF.setConnectionInterval(20);
    NRF.setAdvertising({}, { manufacturer: 0x0590, manufacturerData: [pushbuttonCounter] },{interval:20});

    but still get the same result (or actually a tiny slower). I get a burst of 5 lines printed every second or so. Would I need to change something on the bleak side?

  • You need:

    NRF.setAdvertising({}, { manufacturer: 0x0590, manufacturerData: [pushbuttonCounter], interval:20});
    

    It's meant to go in the second argument, not the third - then hopefully things will work better

  • but still get the same result

    and BTW, if you expect the [pushbuttonCounter] to magically update the advertising data automatically when it changes then it won't do that

  • Thanks!
    got it working, as blemidi on osx is broken in term of auto reconnection I am basically making a workaround with advertisements. I am using the motion example, is there a way to advertise at interval 20 but only when the device is moving?

    here is the code:

    NRF.setTxPower(4);
    //NRF.setConnectionInterval(20);
    
    function rescaleToMidiCC(value, oldMin, oldMax) {
      var newMin = 0;
      var newMax = 127;
      var rescaled = Math.round((value - oldMin) * (newMax - newMin) / (oldMax - oldMin) + newMin);
      rescaled = Math.min(127, Math.max(0, rescaled));
      return rescaled;
    }
    
    
    require("puckjsv2-accel-movement").on();­
    var idleTimeout;
    
    Puck.on('accel',function(a) {
      LED.set();
    
      if (idleTimeout) clearTimeout(idleTimeout);
      idleTimeout = setTimeout(function() {
        idleTimeout = undefined;
        LED.reset();
        //active = false ;
      },500);
      const accel = Puck.accel();
      var x = ((accel.acc.x/128000)*10) *127;
      var y = ((accel.acc.y/128000)*10) *127;
      x = rescaleToMidiCC(x, -120, 120);
      y = rescaleToMidiCC(y, -120, 120);
      print(x + " " + y);
      NRF.setAdvertising({}, { manufacturer: 0x0590, manufacturerData:           [x,y], interval:20});
    
    });
    
  • Yes - looks like you're using require("puckjsv2-accel-bigmovement") anyway and even have the idle timeout, so all you need is:

    NRF.setAdvertising({}, { manufacturer: 0x0590, manufacturerData: [x,y], interval:375});
    

    inside your idleTimeout = setTimeout(function() { function

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

puckjs bleak python mac / read advertisements

Posted by Avatar for user155285 @user155285

Actions