You are reading a single comment by @Schweigi and its replies. Click here to read the full conversation.
  • I checked. The Connection: close header is also added on the .request call by default. I think the issue is related to the content length. If I send too much data then I get the same behavior as with the Mandrill API also with the Requestbin API example.

    var CONTENT_LENGTH = 10; //<== always ok
    //var CONTENT_LENGTH = 250-300; //<== request is sent but no answer received
    //var CONTENT_LENGTH = 800; //<== request is not sent
    
    Serial1.setup(9600, { rx: B7, tx : B6 });
    var wifi = require("ESP8266WiFi").connect(Serial1, function(err) {
      if (err) throw err;
      wifi.reset(function(err) {
        if (err) throw err;
        wifi.connect("<WIFI_SSID>", "<WIFI_PW>", function(err) {
          if (err) throw err;
          
          var content = "";
          for(var i=0; i < CONTENT_LENGTH; i++) {
            content += "x";
          }
          
          var options = { 
            host: "requestb.in", 
            port: 80, 
            path: "/1lxvhjs1", 
            method: "POST",
            headers: {
              "Content-Type": "text/plain",
              "Content-Length": content.length
            }
          };
          
          wifi.at.debug();
          var req = require("http").request(options, function(res)  {
              console.log("status: " + res.statusCode);
          });
          req.end(content);
          console.log("Request sent (length: " + content.length + ")");
        });
      });
    });
    

    While debugging I also found three other issues. I can reproduce those with a simple GET call. Just attach a ESP8266WiFi to your Pico and press the build-in button to trigger a request.

    1. The first issue is, that when I do multiple requests all but the first produce an error (see AT debug msgs). I event tried to give it a couple of minutes between the button pushes to be sure the prior connection is really dead but it does't help. I never managed to make more than one successful http request without restarting the Pico in-between.
    2. The second issue is, that if there is no res.on('data') event handler, then res.on('close') isn't called either (also CIPCLOSE is never called).
    3. The third issue is, that for some hosts (or probably servers) the connection isn't closed after the GET is done but only after the server closes it. Thus it can't take a long time until the data is available to read - see BBC example.

      var URL = "http://www.pur3.co.uk/hello.txt";
      
      /*
      ** ISSUE 3 **:
      Call to the BBC page closes only 60s after request has been started.
      The AT debug messages show that the response is received immediately but 
      the call is only closed after a timeout.
        
      wifi.at.debug() output after 60s:
      ] "\r" <--- "\r"
      ] "\nE" <--- "\nE"
      ] "ERR" <--- "RR"
      ] "ERRO" <--- "O"
      ] "ERROR\r" <--- "R\r"
      ] "\nUn" <--- "\nUn"
      ] "Unli" <--- "li"
      ] "Unlink" <--- "nk"
      ] "Unlink\r" <--- "\r"
      ] "\n" <--- "\n"
      ["AT+CIPCLOSE=0\r\n"
        
      var URL = "http://open.live.bbc.co.uk/weather/feed­s/en/2647937/observations.rss";
      */
      
      Serial1.setup(9600, { rx: B7, tx : B6 });
      var wifi = require("ESP8266WiFi").connect(Serial1, function(err) {
      if (err) throw err;
      wifi.reset(function(err) {
      if (err) throw err;
      wifi.connect("<WIFI_SSID>", "<WIFI_PW>", function(err) {
        if (err) throw err;
      
        console.log("Press button to trigger a request");
        wifi.at.debug();
            
        setWatch(function(e) {
          var req = require("http").get(URL, function(res)  {
            console.log("Connection status: " + res.statusCode);
                
            var content = "";
            /* 
              ** ISSUE 2 **
              If the res.on('data') event handler is removed 
              then "AT+CIPCLOSE=0" is never executed and 
              res.on('close') is never called.
            */  
            res.on('data', function(data) {
              content += data;
            });
                
            res.on('close', function() {
              console.log("Connection data: " + content);
              console.log("Connection closed");
            });
          });
              
          console.log('Request sent');
        }, BTN, { repeat: true, debounce : 50, edge: "rising" });
      });
      });
      });
      

    Btw my Pico firmware has 1v78 and the ESP8266 has 0018000902-AI03.

About

Avatar for Schweigi @Schweigi started