You are reading a single comment by @th and its replies. Click here to read the full conversation.
  • If it is the DHT11 then it seems weird that it works once clearWatch() causes error. Also if I copy in the DHT11 code (slightly modified so it runs) then it consistently gets correct data from the start. So I doubt it is the DHT11 hardware. (Though now it crashes later on due to "New interpreter error: MEMORY")

    var ht={pin: D27};
    
    function DHT11read(cb, n) {
      if (!n) n=10;
      var d = "";
    //  var ht = this;
      digitalWrite(ht.pin, 0);
      pinMode(ht.pin,"output"); // force pin state to output
      // start watching for state change
      this.watch = setWatch(function(t) {
        d+=0|(t.time-t.lastTime>0.00005);
      }, ht.pin, {edge:'falling',repeat:true} );
      // raise pulse after 1ms
      setTimeout(function() {pinMode(ht.pin,'input_pullup');pinMode(­ht.pin);},20);
      // stop looking after 50ms
      setTimeout(function() {
        if(ht.watch){ ht.watch = clearWatch(ht.watch); }
        var cks =
            parseInt(d.substr(2,8),2)+
            parseInt(d.substr(10,8),2)+
            parseInt(d.substr(18,8),2)+
            parseInt(d.substr(26,8),2);
        if (cks&&((cks&0xFF)==parseInt(d.substr(34,­8),2))) {
          cb({
            raw : d,
            rh : parseInt(d.substr(2,8),2),
            temp : parseInt(d.substr(18,8),2)
          });
        } else {
          //if (n>1) setTimeout(function() {ht.read(cb,--n);},500);
          if (n>1) setTimeout(function() {DHT11read(cb,--n);},500);
          else cb({err:true, checksumError:cks>0, raw:d, temp:-1, rh:-1});
        }
      }, 50);
    }
    
    var displayReady=false;
    //var dht = require("DHT11").connect(D27);
    
    var spi = new SPI();
    spi.setup({mosi:D23 /* sda */, sck:D22 /* scl */});
    var g = require("ILI9163").connect(spi, D21 /* DC */, D18 /* CE */, D19 /* RST */, function() { 
      displayReady = true; });
    require("Font8x12").add(Graphics);
    
    var temp = "N/A";
    var rh = "N/A";
    
    function updateDisplay() {
      return;
      if (!displayReady)
        return;
        console.log("ILI9163");
        g.clear();
        g.setColor(1,1,1);
        //g.setFont8x12();
        g.setFontVector(8);
        g.setRotation(1);
        g.drawString("Mensa Norge Landstreff FTW!",0,0);
        
        g.setFontVector(20);
      
        g.setColor(1,0.5,1);
        g.drawString("Temp: " + temp.toString(),0,10);
        
        g.setColor(0,1,1);
        g.drawString("Humid: " + rh.toString(),0,40);
         
    }
    
    function readSensor() {
        console.log("DHT11 pre read");
      //dht.read(
      DHT11read(
        function (a) {
        console.log("DHT11 post read");
        temp = a.temp;
        rh = a.rh;
        console.log(a);
        updateDisplay();
      });
    }
    
    
    setInterval(function() {
      readSensor();
    }, 10000);
    
    

    Result:


    | |_ ___ ___ _ ||___ ___
    | |_ -| . | _| | | | | . |
    |
    |_| || |_|||_|_|

         |_| espruino.com
    

    2v04 (c) 2019 G.Williams
    Espruino is Open Source. Our work is supported
    only by sales of official boards and donations:
    http://espruino.com/Donate

    DHT11 pre read
    DHT11 post read
    {
    "raw": "010011001000000000000110000000010001001­110",
    "rh": 50, "temp": 24 }
    DHT11 pre read
    DHT11 post read
    {
    "raw": "010011010000000000000101110000100101010­100",
    "rh": 52, "temp": 23 }
    DHT11 pre read
    DHT11 post read
    {
    "raw": "010011001000000000000101110000100101010­010",
    "rh": 50, "temp": 23 }
    DHT11 pre read
    DHT11 post read
    {
    "raw": "010011000100000000000110010000100101010­011",
    "rh": 49, "temp": 25 }
    DHT11 pre read
    DHT11 post read
    {
    "raw": "010011000100000000000110000000000001001­001",
    "rh": 49, "temp": 24 }
    DHT11 pre read
    DHT11 post read
    {
    "raw": "010011000000000000000110000000000001001­000",
    "rh": 48, "temp": 24 }
    DHT11 pre read
    DHT11 post read
    {
    "raw": "010011000000000000000110000000000001001­000",
    "rh": 48, "temp": 24 }
    DHT11 pre read
    DHT11 post read
    {
    "raw": "010011000100000000000110000000000001001­001",
    "rh": 49, "temp": 24 }
    DHT11 pre read
    DHT11 post read
    { "err": true, "checksumError": false,
    "raw": "0",
    "temp": -1, "rh": -1 }
    DHT11 pre read
    DHT11 post read
    { "err": true, "checksumError": false,
    "raw": "0",
    "temp": -1, "rh": -1 }
    DHT11 pre read
    Execution Interrupted during event processing.
    New interpreter error: MEMORY
    DHT11 pre read
    ERROR: Ctrl-C while processing interval - removing it.
    Execution Interrupted during event processing.
    New interpreter error: CALLBACK

About

Avatar for th @th started