• Thanks for the responses. I'll definitely look into the output buffer limitations. How can I find out about the size?
    The device is an Espruino WiFi on 1.99.
    Here is the part of the code that matters:

      // measures and counts, counter reset ff below threshold
      meas() {
        const v = Math.round(analogRead(this.pin) * this.rng - this.offs)
        if (this.vals.length === this.nmax) this.vals.shift()
        this.vals.push(v)
        if (this.both === true) v = Math.abs(v)  // zero based, both too low and too high considered
        if (v < this.thr) {
          return this.cnt = 0
        } else {
          if (++this.cnt > this.tcnt) this.cnt = this.tcnt
          return this.cnt
        }
      }
    
      // call meas, evaluate and call back
      cfrm() {
        const c = this.meas()
        if (c === 0) return this.cb(this.val = 0)
        if (c === this.tcnt) return this.cb(this.val = 1)
        if (this.tcfm === 0) {
          // this.cb(this.val = 0)  // not yet confirmed, do nothing (?)
        } else {
          setTimeout(this.meas.bind(this), this.tcfm)
        }
      }
    
      start(cb) {
        if (cb === undefined) cb = this.cb || print
        this.cb = cb
        this.iv = setInterval(this.cfrm.bind(this), this.tupd)
        this.cfrm()
      }
    

    There are 2 sensor instances running, one with an interval (this.tupd) of 400 ms, the other one 2 s.
    'this.cb' is a callback to little instance that keeps track of the 0/1 values sent and eventually switches a light through a MOSFET, along with a TPC notification to the controlling server:

      // called by switching (presence) sensor
      cbsnsr(v) {
        this.snsr = v  // just for information
        if (this.ovrd === 1 || this.toff !== undefined) return
        // act if all conditions are met
        if (v === 1 && this.enbl === 1) this.act()
      }
    
      act() {
        this.tend = Date.now() + this.drtn      // just for information
        if (this.toon) clearTimeout(this.toon)  // to re-set timeout while already running
        const self = this
        this.toon = setTimeout(function() {
          self.toon = undefined
          // if (self.toff) clearTimeout(self.toff)
          // self.toff = setTimeout(function() {self.toff = undefined}, self.dblk)
          self.actr(0)
        }, this.drtn)
        if (this.val !== 1) this.actr(1) // switch on
      }
    
      actr(v) {
        digitalWrite(this.pin, this.val = v)
        this.report('')  // report enbl, snsr, val in an object
      }
    
      report(d) {
        if (this.rurl === undefined) return
        var req = http.get(this.rurl + JSON.stringify(this.getValue(d)))
        // req.on('error', function(){})
        return req  // for debug
      }
    
      getValue(d) {
        if (d === undefined) return this.val
        return {
          ovrd: this.ovrd, trest: this.trest(), enbl: this.enbl,
          snsr: this.snsr, value: this.val
        }
      }
    

    The getValue() method is the one that gets called every 5 minutes via USB or TCP. The call is

    \necho(0)\nprint({r: JSON.stringify(swch.getValue()) }, '@@\n')\n
    

    The TCP console is attached like this:

      wifi.connect('XXX',{password:'YYY'}, function(err) {
        // console.log(err);
        if (err) return;
        wifi.turbo(true, function(err) {
          net.createServer(function(conn) {
            global.conn = conn;
            conn.on('close', function() {USB.setConsole()});
            conn.pipe(LoopbackA);
            LoopbackA.pipe(conn);
            try {
              LoopbackB.setConsole();  // setDeviceClockCmd: Unknown Device 720
            } catch(e) {};
          }).listen(23);
        });
      });
    
About

Avatar for Steffen @Steffen started