Espruino Pico webserver

Posted on
Page
of 2
Prev
/ 2
  • The error during restart is because the server won't have been able to close instantly, but you are trying to recreate it on basically the next line. It's also possible the ESP8266 is taking a while to try and close all the active connections to the server...

    You could try:

    if (server) server.close();
    server=undefined;
    setTimeout(function() {
        server = http.createServer(pageHandler);
        ....
    }, 1000);
    
  • @Gordon Thanks for the tip, but it did not help.

    I still get this at server startup:

    Inactivity detected restarting webserver...
    Webserver restarted.
    >Uncaught Error: CIPSERVER failed (Timeout)
     at line 1 col 53
    throw Error("CIPSERVER failed ("+(a?a:"Timeout")+")");
                                                        ^
    in function called from system
    

    Here is the inactivity checker with 2 sec delay for server start:

    function checkInactivity() {
      if (Date.now() - lastPageHandled > 60000) {
        clearInterval(checkInactivityIntervalId)­;
        lastPageHandled = Date.now();
        // Close server and reopen it.
        console.log("Inactivity detected restarting webserver..."); 
        if (server) {
          server.close();
        }
        server=undefined;
        setTimeout(function() {
          server = http.createServer(pageHandler);
          if (server) {
            console.log("Webserver restarted.");
            checkInactivityIntervalId = setInterval(checkInactivity, 2000);
            server.listen(80);
          } else {
            console.log("ERROR createServer() returned false."); 
            console.log(err); 
            setTimeout(function() { reboot(); }, 5000);
            return;
          }
        }, 2000);
      }
    }
    
  • At each code upload from IDE to EspruinoWifi I get this error:

    ERROR: Prompt not detected - upload failed. Trying to recover...
    

    By the way is there any way to reset or cleanup the Wifi? Would that help?

  • Hmm, it looks like the ESP8266 is just too busy to respond - probably a reboot of the ESP8266 is required (server.close() followed by wifi.reset()).

    By the way: The ESP8266 is never contacted until .listen - so createServer will probably always be successful.

    It's a bit of a hack but you could use the uncaughtException event if you can't find another way of detecting issues : http://www.espruino.com/Reference#l_proc­ess_uncaughtException

  • @Gordon

    Uncaught Error: Function "reset" not found!
     at line 176 col 10
        wifi.reset();
    
  • same for wifi.close()..

    wifi.disconnect() worked. Let's see if that helps..

  • What happens if wifi.listen fails?

  • Ok, just do:

    server.close();
    wifi.disconnect();
    setTimeout(function() {
      wifi.connect(..., function() {
       // ...
      });
    }, 200);
    

    That'll actually power cycle the WiFi

  • @Gordon Now it recovers! Great! Thanks!

    Of course that does not resolve the root cause..

  • I'm actually having a lot of trouble reproducing this with your code. I uploaded it to the board and added the 'Access-Control-Allow-Origin': '*' header so I could access it from another file.

    Then I stuck your polling code in a html file set to call 5 times a second. Any more and it refused to work:

    <html>
    <body>
    <script src="https://code.jquery.com/jquery-1.12­.4.min.js"></script>
    <script>
    var xhr;
    function httpGetAsync(theUrl, jsonpcb, ok, err) {
        if (xhr && xhr.readyState != 4) {
            xhr.abort();
           }
        xhr = jQuery.ajax({
           type:'GET',
           url:theUrl,
           cache:false,
           timeout: (60000),
           crossDomain: true,
           error: err,
           success: ok
        });
    }
    
    setInterval(function() {
      httpGetAsync("http://192.168.1.164/", function(c) {
        console.log("JSON",c);
      }, function(c) {
        console.log("OK",c);
      }, function(c) {
        console.log("ERR",c);
      });
    }, 200);
    </script>
    </body>
    </html>
    

    And then loaded it in 4 windows - so that's 20 requests per second.

    After a bit I started getting the following:

    ERR Object {readyState: 0, status: 0, statusText: "abort"}

    But interspersed with the correctly returned page. After closing the extra windows the board immediately returned to working correctly. So I'd say the behaviour was perfect.

    So I'm thinking this is probably the computer(s) that are requesting data that are still keeping connections open. Which web browser are you using on them?

    When you have problems, could you run:

    global["\xff"].HttpSC.forEach(function(s­,i) {var x=s.svr;s.svr=0;print(i,s);s.svr=x;})
    

    and post the result? It's possible that connections are open and the HTTP header hasn't got through, so Espruino is just leaving sockets open waiting for a response. If so, it might be possible to add a timeout on Espruino's side that would close the connections if the header wasn't received within the first second.

    You could also try:

    global["\xff"].HttpSC.forEach(function(s­) {s.closeNow=true});
    

    That should force Espruino to close all server connections immediately.

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

Espruino Pico webserver

Posted by Avatar for ert4 @ert4

Actions