You are reading a single comment by @allObjects and its replies. Click here to read the full conversation.
  • The browser local Puck object is very helpful for essential and basic stuff, such as connectivity and sending data and receiving response back.

    BUT: What if I want to execute something much more elaborate on my Puck and get the response back?

    Response: create these elaborate code pieces in the Espruino IDE in the right hand side Editor pane as custom functions with your names and upload them to Puck (and save them there - so on re-power / restart, the functions are still there). Then just invoke these functions by the names in your local browser application.

    For example, you create a function that responds in JSON battery percentage and BTN1 status - pressed or not.

    function batAndBut() {
      var responseObject = { battery: E.getBattery(), but: digitalRead(BTN1) };
      var response = JSON.stringify(responseObject);
      console.log(response);
    }
    

    The example is intentionally made verbose.
    Upload this code to Puck. Then test it with entering

    batAndBut();
    

    in the console. The response you should see is something like:

    {"battery":85,"but": 0}  
    

    Now, in your angular Web app you do something like that:

      Puck.write('echo(0);batAndBut();echo(1);­\n',function(data) {
          var batAndBut = JSON.parse(data);
          alert("Battery: "+batAndBut.battery+"%,"
                   +" BTN1"+((batAndBut.but)?"":" NOT")+" pressed."
                    );
        });
    

    Enjoy!

    PS: since I could not test (yet), line 1 may need to be:

      Puck.write('echo(0);\nbatAndBut();\necho­(1);\n',function(data) {
    
About

Avatar for allObjects @allObjects started