You are reading a single comment by @allObjects and its replies. Click here to read the full conversation.
  • ...Ic

    And you may have just found some interesting thing, may be an issue in the .parse(...).

    I did some deep digging in the example, because it fooled me nicely...

    Below my 'console talkative', working code. The main changes and detections are:

    • I made the method in the form submit a upper-case post... that solves the problem of asking the request req variable for the method before gong after the parsed request, which is called r. (Side note: I find it odd - to say the least - that lower-case url is a global that comes into play without definition... @Gordon?). But making that change still did not work, so I wanted to know and added all this console outputs, also did some renaming: upd - Url ParseD - for r... and - when 'consoling' that .parse(... return object, I got 'consoled' by the detection of the issue that, even though the request says it is a POST, the parsed url or request or what it is claims the request.POST as a GET. - See console output after the code...

    • I run it on a Espruino-WiFi... so some lines got commented and a few got added to take care of that...

      // bp246WifiESP8266.js
      // Test ESP8266 / Test Espruino-Wifi (book p. 246)
      var WIFI_NAME = "MYSSID";
      var WIFI_KEY =  "WIFIPWD";
      WIFI_NAME="ssid"; WIFI_KEY ="pw";
      var wifi;
      var json;
      var homepage = '<html><body>'+
      '<h1>My Espruino</h1>'+
      '<a href="/getTemp">Temperature</a></br></br­>'+
      '<form action="/?led=0" method="POST">'+
      '<input type="submit" value="LEDS OFF"/></form>'+
      '<form action="/?led=1" method="POST">'+
      '<input type="submit" value="LED RED ON"/></form>'+
      '<form action="/?led=2" method="POST">'+
      '<input type="submit" value="LED GREEN ON"/></form>'+
      '<form action="/?led=3" method="POST">'+
      '<input type="submit" value="LED RED+GREEN ON"/></form>'+
      '</body></html>';
      function onInit() {
      USB.setConsole(true);
      //   Serial1.setup(115200, { tx:B6, rx: B7});
      //   wifi = require("ESP8266WiFi_0v25").connect(Seri­al1, 
      // function(err) {
      //    if(err) throw err;
      //    wifi.connect(WIFI_NAME, WIFI_KEY, function(err) {
      var WIFI_OPTIONS = { password: WIFI_KEY };
      wifi = require("EspruinoWiFi");
      console.log("Connecting to WiFi...");
      //    wifi.connect(WIFI_NAME, WIFI_KEY, function(err) {
      wifi.connect(WIFI_NAME, WIFI_OPTIONS, function(err) {
        if(err) {
          console.log("Connection error: "+err);
          return;
        }
        onConnected();
      });
      //  });
      }
      function onConnected() {
      console.log("Connected");
      require("http").createServer(onPageReque­st).listen(80);
      //   wifi.getIP(function(err,ip){
      //     console.log("Your IP address is http://" + ip);
      wifi.getIP(function(err,ipInfo){
      console.log("ip info: " + JSON.stringify(ipInfo));
      });
      }
      function onPageRequest(req, res) {
      console.log(" 1) Serving:         " + req.url);
      console.log(" 2) method:          " + req.method);
      console.log(" 3) method:          " + JSON.stringify(req.method));
      console.log(" 4) typeof method:   " + (typeof req.method));
      var upd = url.parse(req.url, true);
      console.log(" 5) upd (url parsed:");
        console.log(upd);
      console.log(" 6) query:           " + JSON.stringify(upd.query));
      console.log(" 7) upd.query b:     " + ((upd.query) ? "true" : "false"));
      if (upd.query) {
      console.log(" 8) upd.query.led b: " + ((upd.query.led) ? "true" : "false"));
      }
      console.log(' 9) (req.method == "POST"): ');
      console.log("      " + ((req.method == "POST") ? "true" : "false"));
      console.log('10) (req.method === "POST"): ');
      console.log("      " + ((req.method == "POST") ? "true" : "false"));
      console.log('11) (req.method === "POST" && upd.query && upd.query.led): ');
      console.log("      "  + ((req.method == "POST" && upd.query && upd.query.led) ? "true" : "false"));
      if (upd.pathname == "/") {
      // controllo passaggio parametri
      if (req.method == "POST" && upd.query && upd.query.led) {
      //       digitalWrite([LED2,LED1], r.query.led);
        digitalWrite([LED2,LED1], upd.query.led);
      //      digitalWrite([LED2,LED1], parseInt(upd.query.led));
      }      
      // Onora la richiesta della pagina
      res.writeHead(200, {"Content-Type!": "text/html"});
      res.end(homepage);
      } else if (upd.pathname == "/getTemp"){
      res.writeHead(200, {"Content-Type!": "text/html"});
      res.end('<html><head>'+
              '<meta http-equiv="refresh" content="2">'+
              '</head><body>'+E.getTemperature().toFix­ed(2)+
              '</body></html>');
      } else {
      res.writeHead(404);
      res.end("404 - Not Found");
      }
      }
      
      // remove this and next line line before saving code to flash
      setTimeout(onInit,1000);
      

    In the next post is the 'chatty' console output, that shows that...

About

Avatar for allObjects @allObjects started