• Hi there, I am playing around with my DHT11 temperature sensor and my websocket server, I am using the Wemos d1 mini.

    Here is my code:

    var dht = require("DHT11").connect(D4);
    var WebSocket = require("ws");
    var wifi = require("Wifi");
    
    var WIFI_NAME = "MY WIFI NAME";
    var WIFI_OPTIONS = { password : "MY WIFI PASSWORD" };
    
    wifi.connect(WIFI_NAME, WIFI_OPTIONS, function(err) {
      if (err) {
        console.log("Connection error: "+err);
        return;
      }
      console.log("Connected!");
      
      var host = "MY IP";
      var ws = new WebSocket(host,
        {
          path: '/',
          port: 80, // default is 80
          protocol : "echo-protocol", // websocket protocol name (default is none)
          protocolVersion: 13, // websocket protocol version, default is 13
          origin: 'Espruino',
          keepAlive: 600,
    (default is none)
        }
      );
    
      ws.on('open', function() {
        console.log("Connected to server");
    
    
        setInterval(function(){
          dht.read(function (a) {
            ws.send('{"temp":"'+a.temp.toString()+'","rh":"'+a.rh.toString()+'"}');
          });
        },10000);
      });
    });
    

    Note: I have obviously hidden my wifi ssid, password and my IP in the code, just in case :)

    If I upload this to the Wemos it will work as it should, my websocket server is receiving the messages and will display the temperature and the humidity every 10 second the message is sent.

    But then once i save() this code onto the Wemos and reboot it, it starts showing this message every 10 second:

    Uncaught Error: This socket is closed.
     at line 2 col 26
    b,c+(this.masking?128:0)));126==c&&(this.socket.write(h(a.le...
                             ^
    in function "send" called from line 1 col 69
    ...rh":"'+b.rh.toString()+'"}');
                                  ^
    in function "c" called from line 2 col 173
    ...or:0<e,raw:b,temp:-1,rh:-1})
                                  ^
    in function called from system
    

    What causes this problem? Seems like it has to do with the wifi module?
    I have tried restarting the server and also the Wemos but the problem persists.

    Thanks

About

Avatar for Hapseleg @Hapseleg started