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.
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.
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.
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
andPuck.eval
.Puck.write
is one-way - you're just sending to the Puck and not waiting for a responsePuck.eval
allows you to get a response back - in yourPuck.getBatteryPercentage()
you should really be usingeval
and notwrite
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#two-way-communications
Instead of the light example there, using code that runs something like this on the Puck should work nicely for you:
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.