You are reading a single comment by @Gordon and its replies. Click here to read the full conversation.
  • I think there might be a bit of confusion because you can run code in two places - on the Puck itself or on the PC. Unfortunately both options allow you to use functions from the Puck object, which means it's not immediately obvious what you can do in each place.

    But on the PC, pretty much all you can do is Puck.write and Puck.eval.

    • Puck.write is one-way - you're just sending to the Puck and not waiting for a response
    • Puck.eval allows you to get a response back - in your Puck.getBatteryPercentage() you should really be using eval and not write

    You can run Puck.eval in an interval, but I'd say that doing it every second is about the most you'd want.

    Take a look at the page on Web Bluetooth - it should explain most of this: https://www.espruino.com/Web+Bluetooth

    Specifically if you want to send a bunch of information from the Puck (like magnetometer readings) then it'd be worth looking at the last section on two-way comms: https://www.espruino.com/Web+Bluetooth#t­wo-way-communications

    Instead of the light example there, using code that runs something like this on the Puck should work nicely for you:

    Puck.magOn(); Puck.on('mag', function(xyz) { Bluetooth.println(xyz); });
    

    edit: That still doesn't explain your disconnection issues though - is it possible that you actually just have a flat battery? That can often be a cause of stuff like this - there's enough charge for Puck.js to advertise, but when you connect it draws a bit more power, the voltage drops, and it resets.

About

Avatar for Gordon @Gordon started