You are reading a single comment by @allObjects and its replies. Click here to read the full conversation.
  • @user100558, now I see where your are coming from.

    I'll try to explain what is going on:

    In Line 27 you are pulling in a piece of communication software so that your angular app running in the browser can use to connect to Puck via BLE and then send data - requests - to Puck and in return receive data - responses - from Puck - all via the BLE connection and Puck's console.

    Think of puck's console like a command or terminal window of a computer. You can type something, and if the terminal / console program - understands it, it will respond. Similar you enter ls in the linux shell and you get the list of files of the current directory you are in, or dir in windows bat shell und you get the list files of the current directory you are in.

    Puck's operating system - so to speak - though does not understand sh or bat shell commands, but it understands JavaScript. The data - or more precisely - source code you send to Puck as request is tried to be interpreted / executed and the response created in Puck's console is sent back to you.

    Where the confusion comes is that in your browser you have an object referred to with the global variable Puck that understands certain commands / has certain methods. On the Puck there is also an object referred to with the global variable Puck with its methods, but ***these 'Puck' objects are not the same', and therefore, do not understand the same commands / have not the same methods.

    Let's have a look at the key method of the Puck object residing in you browser. This key method is .write(arg1, arg2) and accepts two arguments:

    arg1 is a string of JavaScript source code that can be executed / is understood by Pucks JavaScript interpreter, such as you used to set the LED1 on when a button in your angular app in the browser triggers the 'on'-action, else the LED1 is set off (reset). The code - javascript expression - you use to set and reset the LED1 is the string you send in Line 3 and Line 9, respective. The string is terminated with a new line to tell the interpreter that it can now look at the string and interpret it:




    I'm sure you have the Espruino Web IDE installed and you have connected to the Puck via BLE. Enter any of these strings in the console - left pane - and you see the same effect by directly entering into Puck's console. The console of Espruino Web IDE is nothing else than a terminal that sends your key strokes - command - to Puck, and displays what Puck responds: echoing your key strokes and on pressing enter printing the response of your entered command.

    Since there is no meaningful response to setting and resetting a LED and these methods on the LED objects return nothing - in this case undefined - you see


    as response in the console - as the response to your command (beside the echo of what you typed).

    arg2 is a so called callback function that is invoked with one argument - the response from Puck. Both of these callback functions in Line 3 and Line 9 are defined as anonymous functions and without accepting an argument (even though in the JavaScript environment passed arguments are available in the local and predefined variable arguments). There is nothing wrong with not providing an argument in this example of turning LED on and off, because the response is anyway the string "undefined". (If you though would have a syntax or interpretation error, you don't catch it.)

    The arg2 is optional and could be dropped completely in your code. (Your try to read Puck's battery percentage throws actually an error, because it is directed towards your Puck object in the browser, but you meant to talk to the Puck object in Puck to return the battery percentage. Former Puck object does not understand, but latter one does, and you can get the battery percentage - just a little bit different - as I'll show later).

    To be clear what the browser side Puck.write() method does, is:

    • sending the JavaScript source as string as given as 1st argument to Puck
    • receive Pucks response as string and then invoke the callback function - which runs in the browser - with the response string as sole argument.

    To see these details look at - the function beginning with

    connection.write = function(data, callback) {

    data is the first argument and is the JavaScript source code executed on Puck, and callback is the function invoked the the response string from Puck as only parameter```.

    To use the callback with meaningful command data, let's head for the battery percentage:

    Replace Lines 3 and 4 with following lines:

                Puck.write('Puck.getBatteryPercentage();­\n',function(batteryPercentage) {
                    alert( batteryPercentage);

    Since ..getBatteryPercentage() is deprecated in the newest version of Espruino (Puck/Pixel/MDBT42Q) firmware, we do it right and use the E.getBattery() expression:

                Puck.write('E.getBattery();\n',function(­batteryPercentage) {
                   alert("Battery: "+batteryPercentage+"%");

    Now you are able to get your Magnetometer example going in a similar way.


    • arg1 is javascript source that is to be executed on Puck/Pixel/MDBT42Q
    • arg2 is callback function with argument 'to catch and process' the response created by execution of arg1 on Puck/Pixel/MDBT42Q

Avatar for allObjects @allObjects started