WARNING: Module "DHT22" not found

Posted on
  • Today has stopped working DHT22 module. What could be the problem? Yesterday everything worked.

    1v93 Copyright 2016 G.Williams
    Espruino is Open Source. Our work is supported
    only by sales of official boards and donations:
    http://espruino.com/Donate
    Flash map 4MB:512/512, manuf 0xe0 chip 0x4016

    WARNING: Module "DHT22" not found
    Uncaught Error: Field or method "connect" does not already exist, and can't create it on undefined
    at line 3 col 27

    dht = require("DHT22").connect(D14),
                          ^
    

    =undefined

    Found the problem:
    If you are using the Web IDE as is, the modules will be loaded from http://www.espruino.com/modules/. This URL can be changed in Web IDE settings and There is no problem.
    The url was https://.

  • Might you have had a dodgy (or broken) internet connection when you tried the upload?

  • Can be. It is possible to move the modules to the computer?

  • Yes - copy them into modules folder in your project folder

  • Can be more of where and what to lay down
    I have here are modules:
    require("ESP8266"),
    require("Wifi"),
    require("DHT22").connect(D14),
    require("http").request(options, function(res)

    I downloaded the files from http://www.espruino.com/modules/ .If you just put the files in the project folder it does not work.
    Prescribed path to the file C:\Users\a_morozov\Documents\M.
    Or was not necessary?
    It does not work.

    EspruinoWiFi.min
    EspruinoWiFi
    ESP8266WiFi_0v25.min
    ESP8266WiFi_0v25
    ESP8266WiFi.min
    ESP8266WiFi
    DHT22.min
    DHT22

    What are you doing wrong?

  • Working with ESPRUINO WEB IDE will setup a folder structure

    On ESP8266 boards ESP8266 an Wifi is build in, so just download DHT22

  • Or you can turn on 'offline mode' in the Web IDE's settings - which might be easier :)

  • Modules on local are - have to be - in the modules folder, a peer folder of projects folder.

  • Did you get it to work, I tried all options above, also included path like require("http://www.espruino.com/modules/DHT22.js");
    still get the error:

    dht = require("DHT22").connect(D4);
    Uncaught Error: Module "DHT22" not found

  • ...yep, np what so ever. Monitored temperature in my upstairs office - more like a sauna - over the CA heat wave recent weeks...

    ...and I did not need to store the module local.

    @Alexander's struggle got me going and made my get a sensor and run the examples, even added a display and a Web server connecting to my home wireless so I had access to the sensor information from anywhere in the house with my phone over Wifi.

  • @allObjects thanks. nice project! Got it working, beginners error :-) I usually type pieces of code in the left side of WEB IDE for syntax errors, only this does not work for require() and external modules. Once I put it on the right side and uploaded it, it worked, not so hot here 22C (Netherlands). Now adding a PIR and LDR and MOSFET, to make it a smart sensor and dim my LED lights.

  • @PeterS, you get the syntax error info also on the right hand side - in the editor pane. I guess you noticed that by now. Sorry that we did not think of the issue entering the example in the console (right hand side). What you also may have found out is that you can enter everything on the left side, it is just not as convenient. For given DHTH22 sensor example, you would have to enter on the left side following code before you enter the example code (with lines 3..40 being the non-minified, source code of the DHTD22 module from http://www.espruino.com/modules/DHT22.js) :

    Modules.addCached("DHTD22",function(){
    
    function DHT22(pin) {
      this.pin = pin;
    }
    
    DHT22.prototype.read = function (cb, n) {
      if (!n) n=10;
      var d = ""; 
      var ht = this;
      pinMode(ht.pin); // set pin state to automatic
      digitalWrite(ht.pin, 0);
      this.watch = setWatch(function(t) {
        d+=0|(t.time-t.lastTime>0.00005);
      }, ht.pin, {edge:'falling',repeat:true} );
      setTimeout(function() {pinMode(ht.pin,'input_pullup');},1);
      setTimeout(function() {
        clearWatch(ht.watch);
        delete 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,16),2)*0.1,
            temp : parseInt(d.substr(19,15),2)*0.2*(0.5-d[18])
          });
        } else {
          if (n>1) setTimeout(function() {ht.read(cb,--n);},500);
          else cb({err:true, checksumError:cks>0, raw:d, temp:-1, rh:-1});
        }
      }, 50);
    };
    
    exports.connect = function(pin) {
        return new DHT22(pin);
    };
    
    });
    

    ...because something like this happens when you upload your code on the right hand side with the Upload to Board button:

    The upload looks *FIRST* through your code (with regular expression) to find all require.("...") patterns. For each "...." module reference, the code is pulling from internet or local modules folder and 'shoved the down the throat' of Espruino over the same channel as when you enter stuff on the left side. As *SECOND AND LAST*, the actual application code is uploaded - again - over the same channel.

    You just cannot obviously see that because before sending any code, the uploader turns echo in console of and turns it on again after completion of upload. But when you use the Up Arrow key in the console (left hand side) after an upload just happened, you see the last complete JavaScript expression/line that was uploaded... which proves, that the uploader just did what I described above.

    (You may have read post #8 and post #18 of the conversation about simple explanation how to save code that espruino run on start? which explains these things in more detail. - Luckily, the complexity of sequencing of nonblocking/asynchronous thins or callback-hell where Espruino moves on in the code before things are done is made easy: [Promise]()s is available on Espruino and makes writing event driven applications with Espruino even more a breeze.)

    To complete the exercise of this post - proof that we can enter all in the left side and succeed - we paste now the example in the left hand side (for convenience the example code is repeated here):

    var ow = new OneWire(A1);
    var sensor = require("DS18B20").connect(ow);
    setInterval(function() {
      sensor.getTemp(function (temp) {
        console.log("Temp is "+temp+"°C"); 
      });
    }, 1000);
    

    There is no real mystery, just pur(e)3.co.uk smartness... 'pure smartness' - I just made that up. No clue where pur in http://pur3.co.uk stands for... may be IT IS pure - with the E facing backwards as some do when obfuscating their password or user id... Cats puRR, may be in 3(D)...)... only @Gordon may shed some light on pur3... if he thinks we can handle it... sorry: ...if I can handle it... ;-)

  • You can embed the module in your code like this (and there is no need for require anymore):

    function DHT22(pin) {
      this.pin = pin;
    }
    
    DHT22.prototype.read = function (cb, n) {
      if (!n) n = 10;
      var d = '';
      var ht = this;
      digitalWrite(ht.pin, 0);
      pinMode(ht.pin, 'output');
      this.watch = setWatch(
        function (t) {
          d += 0 | (t.time - t.lastTime > 0.00005);
        },
        ht.pin,
        { edge: 'falling', repeat: true }
      );
      setTimeout(function () {
        pinMode(ht.pin, 'input_pullup');
        pinMode(ht.pin);
      }, 1);
      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, 16), 2) * 0.1,
            temp: parseInt(d.substr(19, 15), 2) * 0.2 * (0.5 - d[18]),
          });
        } else {
          if (n > 1)
            setTimeout(function () {
              ht.read(cb, --n);
            }, 500);
          else cb({ err: true, checksumError: cks > 0, raw: d, temp: -1, rh: -1 });
        }
      }, 50);
    };
    
    const dht22 = new DHT22(NodeMCU.D5);
    
    const readDHT22Sensor = () => {
      digitalWrite(D2, false);
      dht22.read(e => {
        console.log(
          '[DHT22 sensor]  ' +
            new Date().toString() +
            ' temperature: ' +
            e.temp.toString() +
            '°C, humidity: ' +
            e.rh.toString() +
            '%'
        );
        digitalWrite(D2, true);
      });
    };
    
    readDHT22Sensor();
    setInterval(() => readDHT22Sensor(), 1000);
    
  • @Jurand

    sure... but the intention w/ modules in the end is to have them accessed and used like modules. For development it is nice to have them inline. Putting the 'module-frame' around keeps the code as close as possible to the module require() and use pattern.

  • I found out, that it works for me only when I use WebIDE: https://www.espruino.com/ide/
    When I use Chrome Extension or local IDE for some reason it does not add modules.
    Maybe it is my VPN, maybe there is some other problem.
    But I am glad that WebIDE works at least.

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

WARNING: Module "DHT22" not found

Posted by Avatar for Alexander @Alexander

Actions