• Want to invoke http.createServer outside of connecting to a network or creating access point. But I get this error:

    Error: Not connected to the internet

    My code follows this logic in event loop:

    1. Start AP
    2. Try to connect to network
    3. Start web server
    4. Stop AP when successfully connected to a network

    i.e.

    E.on('init', function() {
        APCheck = enableWifiScan();
        startAP();
      // If we try to connect to wifi at the same time we scan, there will be
      // conflicts and scan will return nothing
      setTimeout(function() {
        connectToWifi(); // If successful, stops AP
      }, 2000);
    
      // Create server anyway as we'll allow for control of the MCU through AP
      // if it has not connected to a network
      http.createServer(getPage).listen(80);
    });
    

    The reason for this is I want to be able to have the MCU serve a configuration page which will allow user to configure the network to connect to, making the device portable without hardcoding a network.

    I could probably start the server after access point has been created, add a check to see if a server has been instantiated after connecting to a network, but wondering if there's an easier way.

  • I'm pretty sure the problem is that JavaScript executes asynchronously. Obviously I don't know what is in your startAP/enableWifiScan functions, but usually when you execute a command startAP, it returns immediately and calls a callback when the AP is connected.

    For instance for Espruino Wifi you need:

    wifi.startAP(ssid, options, function() { 
      // this is executed when connected and should start the server
    });
    // this code is executed before it is connected
    

    Also, which device are you using the Wifi on?

  • @Gordon:

    ESP8266 NodeMCU 1.0 (ESP-12E)

    Yes, the issue is the event-driven nature of JS. I was looking for a way to write simpler code since there isn't a reliable async or Promise flow control library to use; I'll have to work with flags then. Some questions:

    1. Do you know if http.createServer() returns a new instance with a listener every time it's invoked like node.js? Or is there code beneath the hood that will check for an existence of a http.Server object? I did a quick test to invoke http.createServer() twice in succession and there was no namespace conflicts or EADDRINUSE errors, nor did the callback trigger twice upon a POST to my ESP8266.

    2. Does the Wifi class trigger an event when it has connected to a network? Something like Wifi.on('connected', function(){...}) -- Nevermind, found the docs: http://www.espruino.com/Reference#t_l_Wi­fi_connected

    3. Is there a way to save a key/value pair to flash? (or just any variable)

  • There is a promise library, at least in later versions of Espruino. createServer will create a second server in JS which will confuse matters, but presumably the ESP8266 doesn't check for overlapping sockets, or you're not calling .listen twice.

    Moving this to the ESP8266 forum though - folks there might know what happens with connected.

  • Gordon, pthieu,

    I seem to have a related problem. I am trying to get a web server running on boot up, I've used samples from the forums to test with. Here's the code I'm using:

    >dump()

    E.on("init", function () {
      var http = require("http");
                       http.createServer(function (req, res) {
                       res.writeHead(200);
                       res.end("Hello World");
                       }).listen(8080);
    });
    

    As you can see, the code was saved(). I've also saved the wifi settings (which do work btw)! However, when booting I get:

    Loading 1836 bytes from flash...
    ERROR: Not connected to the internet

    And, when I try to browse to port 8080 on the IP address of my ESP8266, the web browser can't load the page, however, if I type:

    >load()
    =undefined
    Loading 1836 bytes from flash..
    .

    Voila, it starts working! I am assuming that the http server is starting before the wifi interface, thus producing the Not connected to the internet error and when I load the code manually after the ESP8266 has finished booting, it works. This isn't very handy!

    Any ideas chaps? Oh and pthieu, I think I have the same ESP8266 as you...

    Julian

  • @julian: Yes, it will take some time for the network interface to establish a connection with the access point; the E.init will trigger immediately after a power cycle (or save() invocation).

    Change your code to this:

    var Wifi = require('Wifi');
    E.on("init", function () {
      Wifi.on('connected', function(){
        var http = require("http");
        http.createServer(function (req, res) {
        res.writeHead(200);
        res.end("Hello World");
        }).listen(8080);
      });
    });
    

    The doc for the Wifi class has a number of events that are emitted on specific situations that you can utilize to your advantage.

  • Hi pthieu,

    Wowzers, yes that works! The error has gone and the http server is running after a hard reboot. That link makes sense to me now, good find!

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

`Error: Not connected to the internet` when invoking http.createServer

Posted by Avatar for pthieu @pthieu

Actions