You are reading a single comment by @Eric and its replies. Click here to read the full conversation.
  • @Eric to get a server, you just replace the require("http").get(... line with require("http").createServer. There's an example here and a bit more info on the internet page.

    The actual code should look a bit like this though:

    var WIFI_NAME = "wifi_name";
    var WIFI_PASS = "wifi_key";
    
    function pageHandler(req, res) {
        res.writeHead(200);
        res.end("Hello World");
    }
    
    Serial2.setup(9600, { rx: A3, tx : A2 });
    var wifi = require("ESP8266WiFi").connect(Serial2, function(err) {
      if (err) throw err;
      wifi.reset(function(err) {
        if (err) throw err;
        console.log("Connecting to WiFi");
        wifi.connect(WIFI_NAME, WIFI_PASS, function(err) {
          if (err) throw err;
          console.log("Connected");
          // print IP address
          wifi.getIP(console.log);
          // Create a server
          require("http").createServer(pageHandler­).listen(80);
        });
      });
    });
    

    @Stevie, yes the ESP8266 is a little unreliable as a server at the moment (I'm surprised about the 300 requests for a client though - I've left one going for days without problems). I'm hoping I can do something in the driver to make the server more reliable - but it may be that I have to come up with a way to update the firmware on the ESP8266...

  • Hello Gordon,

    I can't believe how helpful you have all been. DrAzzy and yourself have been great in responding so fast. Thank you for the trimmed down version it makes it perfectly clear now.

    One small question more I have is "Is it possible to take the wifi.getIP() function and make a request to a server passing the IP address as a part of the query string?"

    For example http://192.168.0.2/get_pico_ip.php?picoi­d=pico001&ip=192.168.0.171

    So in other words, I have have my Pi listening out for requests from Pico's making connections to network. The request is an announcement what the IP address is of the Pico after connection. This get's logged as a dynamic IP and can then be used by the Pi server to remotely connect and make calls, set values etc just like in your interactive web page.

    If not, I would need to set up my router to issue the same IP address for the MAC ID following initial connection, which is OK, but a dynamic approach is better I think.

About

Avatar for Eric @Eric started