• (As another user)
    Did you load the program into the device and then in the left pane of the WebIDE type in
    save();
    ?
    http://www.espruino.com/Reference#l__glo­bal_save

    http://forum.espruino.com/conversations/­278526/

  • @Aleksandrs, for what ever reason the conversation about After reboot HTTP do not working need reflash from WEB IDE is closed. It shows a closed pad lock next to the conversation title, and therefore nobody can respond to it. Furthermore, it has no explaining text about the steps you took to get to where you are.

    Because @ClearMemory041063 wanted to respond, he 'recreated' the conversation. To include everything in this conversation, I'm replicating the code. @Aleksandrs, please provide context - board, wiring, etc,... and steps you took.

    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"});
    */
    
  • My apologies i rising this problem... need start HTTP server after connecting to WiFi

  • No problem, that's why we are all out here on the forum to help each other going and having fun... all this easy to use IoT: a new world! and especially with the easy and ready to use Espruino!

  • Yes, need Http server run after WiFi getting IP address

    // Init WiFi Networks
    wifi.setHostname("my1");
    wifi.connect("xxx", {password:"xxx"}, function(err){
      setTimeout(function(){
        console.log("connected? err=", err, "info=", wifi.getIP());
        // Init Your Http Server
        Init();
      },5000);
    
    });
    wifi.stopAP();
    wifi.save();
    
  • Hi @Aleksandrs see the attached file:
    I hope it gets you started.

    >echo(0);
    To run in left pane enter test();
    To save for boot and run enter save(); in left pane
    =undefined
    >save();
    =undefined
    Erasing Flash.....
    Writing..................
    Compressed 81600 bytes to 16091
    Checking...
    Done!
    Start
    Start connection process
    Try again
    Reset the ESP8266
    Connecting to WiFi
    IP=  192.168.1.3
    null
    Get
    Get
    

    The code:

    //alex2.js
    //1 Oct 2016
    //var dht = require("DHT22").connect(D0); // GPIO4
    var lastDhtData ="DHT reading goes here";
    var testcount=0;
    //espruino board with ESP8266
    //PICO  with ESP8266
    //IP=192.168.1.4
    //Use the following in the browser
    //http://192.168.1.4:8080/
    //IP=192.168.1.3
    //Use the following in the browser
    //http://192.168.1.3:8080/
    
    //var Hardware=0; //Espruino board
    var Hardware =1; //PICO
    
    var SSID="ssid";
    var key= "passcode";
    
    var Serial;
    
    function test(){
    if(Hardware===1)Serial=Serial2;
    if(Hardware===0)Serial=Serial4;
    
    if(Hardware===1){
     digitalWrite(B9,1); // enable on Pico Shim V2
     Serial.setup(115200, { rx: A3, tx : A2 }); //Pico
    }
    if(Hardware===0)Serial.setup(115200, { rx: C11, tx : C10 }); 
    //espruino board
    
    console.log("Start connection process");
    var wifi = require("ESP8266WiFi_0v25").connect(Seri­al, function(err) {
      if (err)return 1;// throw err;
    console.log("Reset the ESP8266");
      wifi.reset(function(err) {
        if (err)return 1;// throw err;
        console.log("Connecting to WiFi");
        wifi.connect(SSID,key, function(err) {
          if (err)return 1;//throw err;
          wifi.getIP(function(l,ip){
            console.log("IP= ",ip,"\n\r"+l);
    // Now you can do something, 
    ////////////////////////////////////////­////////////////////
     serveHTML();
    ////////////////////////////////////////­////////////////////
         });//end getIP
        });//end connect SSID
      });//end reset
    });//end conect serial
    }//end test
    ////////////////////////////////////////­////////////////////
    
    function serveHTML(){
    var http=require("http").createServer(onPage­Request).listen(8080);
    }//end serveFile
    
    var pdata="";
    function onPageRequest(req, res) { 
    //console.log("Req= ",req);
    //console.log("Header",req.headers);
    if (req.method=="POST") {
      console.log("Post");
      doPost(req,res);
    }else{
      doGet(req,res);
    }//endif
    }//end on PageRequest
    
    var KMtext1="<html><body style=\"text-align:center;margin-left:au­to;margin-right:auto;\"> \r\n<h1>";
    var KMtext2="</h1>\r\n</body> \r\n</html>  \r\n";
    
    function doGet(req,res){
      var a = url.parse(req.url, true);
      console.log("Get");
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.write(KMtext1);
        res.write(lastDhtData);
        res.write(KMtext2);
        res.end("");
    }
    
    function doPost1(req,res){
    //todo read posted data, compose reply and send it
    }
    
    ////////////////////////////////////////­////////////////////
    
    // Get data from sensor every 1 minute
    setInterval(function(){
    /*  dht.read(function (a) {
        lastDhtData = a.temp.toString()+","+a.rh.toString();
      });
    */
     testcount++;
     lastDhtData= "data= "+testcount;
    },1000); //60000);
    
    
    E.on('init', function() { 
    var x;
    console.log("Start");
    x=test();
    if(x!==0)console.log("Try again");//error seen
    
    if(x===0) console.log("Exit Seen");
    });
    
    console.log("To run in left pane enter test();");
    console.log("To save for boot and run enter save(); in left pane");
    
  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

After reboot HTTP do not working need reflash from WEB IDE Reply link is missing

Posted by Avatar for ClearMemory041063 @ClearMemory041063

Actions