Looking For Sample Code - button presses and sensors

Posted on
  • Hi All,

    I'm looking to use a few puck-js units in my home automation.

    I've implemented this guide for button control and led control:

    https://www.espruino.com/BLE+Node-RED

    But I'm unsure of how to weave in the other sensors. I've spend 45 minutes or so looking, but haven't been able to find some code I can use.

    Can someone point me to an example that I can build off of? I'm a beginning JS coder.

  • Tue 2020.01.28

    @user108908, which specific sensors did you have in mind?

    https://www.espruino.com/Tutorials

  • Thanks - yes, I've poked around in there... not yet sure how to weave it all together and suspect the code is out there somewhere.

    Forgot to mention I have gotten EspurinoHub installed, which appears to aggregate some of the advertising and post results to MQTT.

    I'd like to get readings from the magnetometer (open/close door), temp and light sensor, as well as battery level.

  • Tue 2020.01.28

    'not yet sure how to weave it all together'
    'I'd like to get readings from the magnetometer (open/close door), temp and light sensor'

    So @user108908, is it possible that what is being attempted has started with a bit of overkill with a not so suitable starter example?

    . . . or . . . is the task at hand a question for where to decode the sensor data, rather than/or how to interface with MQTT and EspruinoHUB?

    https://www.espruino.com/Quick+Start+Cod­e

    I grabbed this easier to follow example Puck Detecting Movement from the 'Tips and Tricks' page:

    https://www.espruino.com/Tips+and+Tricks­

    Obtaining the current battery level:

    https://www.espruino.com/Reference#l_E_g­etBattery

    "You can use Puck.mag() to return one magnetometer reading (with x, y, and z axes)" code snippet from:

    https://www.espruino.com/Puck.js#on-boar­d-peripherals



    As the Tutorials section is directed at providing a solution map for others to follow to reach the same proven presentation, see beneath heading in the specific 'Tutorials' posting pane on the right hand side,

    http://forum.espruino.com/microcosms/130­/

    "Post any tutorials/examples for Espruino here!" that a better thread section might provide better visibility for what is desired here?


    For: @Gordon This post most likely would benefit user108908 if it were posted under:
    Home >> Official Espruino Boards >> Puck.js   or   Interfacing, perhaps?

    Also, where did the basic Getting Started video (~4-6min) Intro to Espruino link that showed a broad overview from typing code into the WebIDE and simple interface snippets get to? I used to (> ~6mo ago) fetch that from the main menu. If it is there, now not as intuative location as before.

  • Hi!

    It's great that you've got EspruinoHub set up - that should make things way easier.

    There's the example of button presses on https://www.espruino.com/BLE+Node-RED:

    var pressCount = 0;
    setWatch(function() {
      pressCount++;
      NRF.setAdvertising({
        0xFFFF : [pressCount]
      });
    }, BTN, { edge:"rising", repeat:true, debounce:50 });
    

    That uses 0xFFFF which we just use as a general UUID for testing, but there are a few others listed on https://github.com/espruino/EspruinoHub#­advertising that you can add too:

    • 1809 decodes to temp (Temperature in C)
    • 180f decodes to battery
    • feaa decodes to url (Eddystone)
    • 2a6d decodes to pressure (Pressure in pa)
    • 2a6e decodes to temp (Temperature in C)
    • 2a6f decodes to humidity (Humidity in %)

    So for instance if you want to show battery as well as button presses, you could do:

    setInterval(function() {
      NRF.setAdvertising({
        0xFFFF : [pressCount],
        0x180f : [E.getBattery()]
      });
    }, 60000); // update once a minute
    

    You can also make your own by modifying EspruinoHub at https://github.com/espruino/EspruinoHub/­blob/master/lib/attributes.js#L38

    There's another option mentioned in https://github.com/espruino/EspruinoHub#­advertising too, which is actually much nicer and tidier - you advertise JSON:

    var data = {a:1,b:2};
    NRF.setAdvertising({},{
      showName:false,
      manufacturer:0x0590,
      manufacturerData:JSON.stringify(data)
    });
    

    The only thing to watch out for there is you don't want your JSON string to get too long, since there's a ~20 byte limit on the size of the data you can advertise.

    As an example though, if you want to broadcast door openings then you can look at the code to measure when the door is open at http://www.espruino.com/Puck.js+Door+Lig­ht

    var zero = Puck.mag();
    var doorOpen = false;
    function onMag(p) {
      p.x -= zero.x;
      p.y -= zero.y;
      p.z -= zero.z;
      var s = Math.sqrt(p.x*p.x + p.y*p.y + p.z*p.z);
      var open = s<1000;
      if (open!=doorOpen) {
        doorOpen = open;
        digitalPulse(open ? LED1 : LED2, 1,1000);
      }
    }
    Puck.on('mag', onMag);
    Puck.magOn();
    

    And where you know the door has changed state then you just update the information you advertise with setAdvertising - so for example the below updates battery level every minute AND also updates with the door open state.

    var advertise = {dr:0,bt:E.getBattery()};
    function updateAdvertising() {
      NRF.setAdvertising({},{
        showName:false,
        manufacturer:0x0590,
        manufacturerData:JSON.stringify(data)
      });
    }
    
    // update battery
    setInterval(function() {
      advertise.bt = E.getBattery();
      updateAdvertising();
    }, 60000); // update once a minute
    
    // update door
    var zero = Puck.mag();
    var doorOpen = false;
    function onMag(p) {
      p.x -= zero.x;
      p.y -= zero.y;
      p.z -= zero.z;
      var s = Math.sqrt(p.x*p.x + p.y*p.y + p.z*p.z);
      var open = s<1000;
      if (open!=doorOpen) {
        doorOpen = open;
        digitalPulse(open ? LED1 : LED2, 1,1000);
        advertise.door = open?1:0; // use numbers rather than true/false to save data length
        updateAdvertising();
      }
    }
    Puck.on('mag', onMag);
    Puck.magOn();
    

    Then on MQTT you should see the following:

    /ble/advertise/ma:c_:_a:dd:re:ss/dr -> 1/0 -> door open status
    /ble/advertise/ma:c_:_a:dd:re:ss/bt -> 0..100 -> battery level
    

    Hope that helps!

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Looking For Sample Code - button presses and sensors

Posted by Avatar for user108908 @user108908

Actions