puck.ReadBattery returns Puck not defined

74 views
Skip to first unread message

Jaybee

unread,
Apr 26, 2020, 12:49:38 PM4/26/20
to DroidScript
I'm impressed using the Puck.js Plugin to remotely monitor temperature using espruino MDBT42Q device as temperature sensor, the puck.ReadBattery returns Puck not defined, this is a common error in espruino Javascript, the use of E.getBatteryPercentage() has been replaced with NRF.getBattery(), is there a way to get round this?

Dave

unread,
Apr 28, 2020, 8:44:42 AM4/28/20
to androi...@googlegroups.com
Is that a recent change?  Do you have the latest firmware installed?


You could try sending something like this as a work-around:-
(The ReadTemp or another similar call is needed to setup the internal callbacks)

puck.ReadTemp( OnRead );
puck
.Send( "console.log('BAT|'+NRF.getBattery());\n" );



Or if you are only interested in the battery level :-

puck.SetOnReceive( OnRead )
puck
.Send( "console.log(NRF.getBattery());\n" );




Jaybee

unread,
Apr 28, 2020, 5:12:41 PM4/28/20
to DroidScript
Hi thanks, great that works in console but how do I get same result using ShowPopup in App, Droidscript v 1.8, Puck change fairly recent still getting to grips with DS.

Steve Garman

unread,
Apr 28, 2020, 6:01:18 PM4/28/20
to DroidScript
/*
To tell the app to accept string data sent from puck and call a callback function use
puck.SetOnReceive( OnRead )

in the OnRead function show a popup.
*/

//////// sample code ////////
app.LoadPlugin( "PuckJS" );

function OnStart()
{
lay = app.CreateLayout( "Linear", "VCenter,FillXY" );

// the output from cosole.log will be sent to
// this app
var code = "console.log('Hello Folks');\n";

txt = app.CreateTextEdit( code, 0.9, 0.8, "MultiLine" );
lay.AddChild( txt );
app.AddLayout( lay );

btnSend = app.CreateButton( "Send", 0.3, 0.1 );
btnSend.SetOnTouch( btnSend_OnTouch );
lay.AddChild( btnSend );


puck = app.CreatePuckJS();
puck.Scan( "Puck" );

// This tells the app to call function OnRead
// when data received from puck
puck.SetOnReceive( OnRead )
}

// called when data received
function OnRead(result)
{
app.ShowPopup( result );
}

// called when send button pressed
function btnSend_OnTouch()
{
puck.SendCode( txt.GetText() );
}

Jaybee

unread,
Apr 29, 2020, 11:29:36 AM4/29/20
to DroidScript
Thanks Steve, the issue is still the code in the Example script Read battery level, in particular "puck.ReadBattery(OnRead), unlike the similar Read Temperature example does not work, debug console returns error "Puck not defined". DS readBattery is using Espruino command "Puck.getBatteryPercentage(), this command has generally been replaced by: NRF.get.Battery(). This resolves issue in Espruino script but not updated in DS? Just a guess. Is there an update likely?

Steve Garman

unread,
Apr 29, 2020, 11:36:01 AM4/29/20
to DroidScript
Sorry, I assumed Dave's post covered that for now.

If you type the code below into the TextEdit
of the code I posted, then press the Send button, do you get the battery reading?

console.log('BAT|'+NRF.getBattery());

Jaybee

unread,
Apr 29, 2020, 1:14:17 PM4/29/20
to DroidScript
Thanks Steve for your patience, the "work around" works, I'll try to knit it into my code. Thanks again.

Steve Garman

unread,
Apr 29, 2020, 1:35:16 PM4/29/20
to DroidScript
It's probably worth pointing out that if you put the command into a variable, you need to terminate it with "\n" as Dave did.

Dave

unread,
Apr 29, 2020, 4:46:31 PM4/29/20
to DroidScript
You could actually fix the Puck plugin yourself.  It would be very easy.  Just download the plugin from here:-


and edit the PuckJs.inc file with a text editor


Dave

unread,
Apr 29, 2020, 4:57:49 PM4/29/20
to DroidScript
FYI, it looks like you should be using E.getBattery() instead of NRF.getBattery() as a direct replacement for Puck.getBatteryPercentage()

Steve Garman

unread,
Apr 30, 2020, 1:41:03 AM4/30/20
to DroidScript
I have uploaded a modified version of PuckJS.zip plugin to my Google Drive https://drive.google.com/file/d/1fmTyP139ms5Fn0IfritBneDh3jMhTSj0/view?usp=drivesdk

This fixes the problem with newer versions of Espruino failing to read the battery level. This is the exact same fix I expect to see in the next official release of the plugin.

For anyone who cares, I have not incremented the version number.

To install it should reasonably simple:
- Make sure you have a folder  /sdcard/DroidScript/Plugins
- If it does not exist you will need to create it.
- Place the PuckJS.zip file in that Plugins folder.
- Restart DroidScript.
- The plugin will be installed.

This method can be used to install any properly packaged plugin that comes as a .zip file

Jaybee

unread,
Apr 30, 2020, 6:09:09 AM4/30/20
to DroidScript
The Hardware reference does state "deprecated to E.getBattery()" as you say but this still does not work hence the use of NRF.getBattery(), I've not yet tried out your suggestion to modify Puckjs.zip or download Steve's modified version, I'm sure you Guys have resolved it, I'm just overwhelmed by the effort both of you are making. I'll give it a go.

Steve Garman

unread,
Apr 30, 2020, 6:17:59 AM4/30/20
to DroidScript
My modified version uses E.getBattery() so it may not work for you but I would be grateful if you would test it anyway.

I don't have a device that shows the problem and would like confirmation that it does not work before trying something else.

Jaybee

unread,
May 1, 2020, 9:33:37 AM5/1/20
to DroidScript
Sorry for the delay in responding, it's a little more complex, the Puck and Bangle certainly respond to E.getBattery() correctly, however my project uses the bare bones MDBT42Q which requires NRF.getBattery(), so I think all is now well, your updating of PuckJS will ensure correct Plugin compatibility with Puck & Bangle and Dave's advice concerning self modification of PuckJS.Zip has allowed me to properly resolve Battery Voltage reporting for my project, Thanks for your help. 
Reply all
Reply to author
Forward
0 new messages