• Hi

    Unfortunately, I am not very computer literate.
    Congratulations on a great product and what you have developed. It has introduced me to a new world of tinkering and having fun making stuff.

    For my own use, I would like to modify some of your tutorials - I would like to use the Puck.js door opening and closing tutorial, but in addition to having the green and red LEDs light up when opening or closing the door I would like the following:
    1) I would like a timestamp of when each opening occurred (if I can have the temperature at the time as well, that would be a bonus).
    2) I would like how many times a day in total the door is opened
    3) I would like this to be stored and downloaded when I connect with the Puck.js
    4) eventually I would like this information to download to my phone seamlessly and have all the information stored on something like an Influx Database website. Ideally I could know how times a day it was opened over several months, and how often.

    I have spent many hours going through your web material and I feel like I’m on the edge of knowing what to use from different bits but I don’t have the skill

    Kind regards

  • No problem! For anyone else looking, this is the tutorial: http://www.espruino.com/Puck.js+Door+Lig­ht

    If you go into the Web IDE communications settings and ensure 'Set Current Time' down the bottom is enabled, then upload the following to your Puck.js:

    // Our 'zero' magnetometer reading
    var zero;
    // Is door currently open?
    var doorOpen = false;
    // size in bytes of each event
    const EVENT_SIZE = 5;
    /* Each event will be:
     byte 0-3 : # of seconds since 1970 (good up to 2106)
     byte 4   : temperature in C
    // How many events will be store
    const EVENT_COUNT = 1000;
    // The Data we're saving
    var events = new DataView(new ArrayBuffer(EVENT_SIZE*EVENT_COUNT));
    // How many events are recorded on the device
    var eventsRecorded = 0;
    // Called for each magnetometer reading
    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 ? LED3 : LED2, 1,200);
        if (doorOpen) onDoorOpen();
    Puck.on('mag', onMag);
    // Called when door is opened
    function onDoorOpen() {
      if (eventsRecorded>=EVENT_COUNT) {
        // too many events, flash the Red LED and return
        digitalPulse(LED1, 1,2000);
      var o = eventsRecorded*EVENT_SIZE;
    function get() {
      console.log("============= START");
      for (var e=0;e<eventsRecorded;e++) {
        var o = e*EVENT_SIZE;
        var event = {
          time : new Date(events.getUint32(o+0)*1000),
          temp : events.getInt8(o+4)
        };                      ;
      console.log("============= END");
      // delete all events
    // Call at boot time
    function onInit() {
      // power on magnetometer
      // ensure that we reset 'zero' at power-on time
      zero = Puck.mag();
    // Start scanning door opening events

    Then the Puck will start logging the time and temperature of all door openings (when the blue LED flashes).

    All you need to do is set it up as it was in the tutorial and then come back to it some time later with a laptop with the Web IDE. You can then connect and type get() on the left-hand side of the IDE and it'll output all the times the door was opened and closed.

    It doesn't store how many times a day, but that should be easy to work out by looking at the amount of events in each day.

    On the phone side, the easiest solution would be to use some kind of web service that you could push the data to (do you have one that you've used before?), and then have a Web Bluetooth site.

    It wouldn't be totally seamless (requiring maybe 3 taps of the screen) but it is so much easier to implement because otherwise you'd be looking at a custom phone app. Does the web bluetooth site sound good as a starting point?

    Your other option is to use another Espruino device with Bluetooth and WiFi as a base station, but again that's more cost & work than the site.

  • https://www.espruino.com/Data+Collection­

    This is where I learnt how to leverage the puck and web bluetooth to do data collection. In addition to that I added a nodejs process that takes the data from the custom downloading site in that article and puts into a file in my computer for convenience. I found that the best way to actually get the data in a practical way.

    I understand that you are not computer literate and that maybe you already saw that article but if you are feeling brave and wanna dive deeper I am here to help :)

  • Thanks! I should have pointed to that link originally :)

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

Addition of timestamps, data collection and additional data to door tutorial Puck.js

Posted by Avatar for jp @jp