You are reading a single comment by @dave_irvine and its replies. Click here to read the full conversation.
  • So this whole debacle is at least mostly my fault. A while ago I had to reflash the ESP8266 firmware and ended up with 0.50 AT command set, which as we discussed here: http://forum.espruino.com/conversations/­304736/?offset=25#13630588 has a longer boot preamble than the 0.40 command set, which seems to confuse the FIFO buffer and therefore the standard EspruinoWiFi module.

    To work around it I've been doing my own hack to reset the ESP8266 and then hand over control to the EspruinoWiFi module.

    What I didn't realise is that this causes two .on('data') event handlers to be registered (my one is never freed), and this causes many, many, many issues further down the stack. It also explains why everything WiFi has been so slow, the data event handler is being run twice for everything!

    @Gordon as I am stuck with having to use my workaround to boot the 0.50 firmware, could you possibly provide a disconnect function in the AT library to release the event handlers?

    var wifi;
    
    function onInit() {
      var doPreambleReset = true;
      
      wifi = require("EspruinoWiFi");
      
      var connectGetIPAndFetchData = function() {
        wifi.connect(WIFI_NAME, { password : WIFI_KEY }, function(err) {
          if (err) {
            console.log("Connection error: "+err);
            return;
          }
    
          console.log("Connected!");
    
          wifi.getIP(function(err,ip) {
            console.log("IP address is: "+ip.ip);
    
            var http = require("http");
            var fetched = 0;
    
            http.get("http://www.espruino.com/press/­logo_square_blur.png", function(res) {
              res.on('data', function(data) {
                fetched += data.length;
                console.log("Got "+data.length+" bytes");
                console.log(fetched + ' bytes so far');
              });
    
              res.on('close', function() {
                console.log("Closed");
                console.log(fetched + ' bytes total');
              });
            });
          });
        });
      };
      
      if (doPreambleReset) {
        initWifi(connectGetIPAndFetchData);
      } else {
        connectGetIPAndFetchData();
      }
    }
    
    /* Perform a manual reset of the WiFi chip, sometimes the preamble from a hardware reboot
     * exceeds the FIFO and confuses EspruinoWiFi module. If we do a software reboot,
     * the preamble is smaller.
     */
    function initWifi(callback) {
      digitalWrite(A14, 0);
    
      Serial2.setup(115200, { rx: A3, tx : A2 });
      
      /* Herein lies the problem. The AT module has a 'connect' function, but no 'disconnect'
       * In the 'connect' function it registers an event handler on the 'data' event of the
       * Serial port, and this never gets released. When we use EspruinoWiFi.connect later,
       * this registers a second event handler, and this causes something to screw up.
       */
      at = require('AT').connect(Serial2);
    
      at.cmd('\r\nAT+RST\r\n', 10000, function(data) {
        setTimeout(callback, 10);
      });
    
      digitalWrite(A13, 1);
      digitalWrite(A14, 1);
    }
    
About

Avatar for dave_irvine @dave_irvine started