Avatar for pthieu

pthieu

Member since Nov 2016 • Last active Nov 2016
  • 1 conversations
  • 4 comments

Most recent activity

  • in ESP8266
    Avatar for pthieu

    @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.

  • in ESP8266
    Avatar for pthieu

    @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)

  • in ESP8266
    Avatar for pthieu

    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.

Actions