• 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);
        return;
      }
      var o = eventsRecorded*EVENT_SIZE;
      events.setUint32(o+0,Date.now()/1000);
      events.setInt8(o+4,E.getTemperature());
      eventsRecorded++;  
    }
    
    function get() {
      console.log("============= START");
      console.log("Time,Temperature");
      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(event.time.toString()+","+ev­ent.temp);
      }
      console.log("============= END");
      // delete all events
      eventsRecorded=0;
    }
    
    // Call at boot time
    function onInit() {
      // power on magnetometer
      Puck.magOn();
      // ensure that we reset 'zero' at power-on time
      zero = Puck.mag();
    }
    
    // Start scanning door opening events
    onInit();
    

    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.

About

Avatar for Gordon @Gordon started