• var wifi = require("Wifi");
    var dht = require("DHT22").connect(D0); // GPIO4
    var lastDhtData ="none";
    
    // Init WiFi Networks
    wifi.setHostname("my1");
    wifi.connect("XXX", {password:"XXX"}, function(err){
      console.log("connected? err=", err, "info=", wifi.getIP());
    });
    wifi.stopAP();
    wifi.save();
    
    function init() {
      initServer();
      //setInterval(function(){ console.log(process.memory()); }, 100000);
      //require("ESP8266").setLog(2);
    }
    
    
    function initServer() {
      var http = require("http");
      console.log("Initalizing server instance...");
      // Create Http Answer
      http.createServer(function(req, res){
        var request = url.parse(req.url, true);
        var action = request.query;
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.write('<html><head></head><body>');
        res.write(lastDhtData);
        res.end('</body></html>');
      }).listen(80);
    }
    // Get data from sensor every 1 minut
    setInterval(function(){
      dht.read(function (a) {
        lastDhtData = a.temp.toString()+","+a.rh.toString();
      });
    },60000);
    
    // Send to server every 10 minut
    setInterval(function(){
       postToServer(lastDhtData); 
    },600000);
    
    function postToServer(data){
      var options = {
        host: 'MAINLINK',
        port: '80',
        path:'/event',
        method:'POST',
        headers: {
          "Content-Type":"application/json",
          "Content-Length":data.length 
        }
      };
    
      require("http").request(options, function(res){
        var d = "";
        res.on('data', function(data) { d+= data; });
        res.on('close', function(data) {
          console.log("Closed: "+d);
        });
      }).end(data);
    } 
    
    
    E.on('init', function() { 
      init();
    });
    
    /*
    setWatch(function(e){
      console.log(e.time - e.lastTime);
    }, D1, {repeat: true, edge:"both"});
    */
    
About

After reboot HTTP do not working need reflash from WEB IDE

Posted by Avatar for Aleksandrs @Aleksandrs

Actions