• Thanks for the help. It does seem that the wifi.on('connected' event is only triggered once, and only on wifi.connect and not wifi.startAP (based on login inside the event handler).

    Just from a design perspective, it does make sense to do certain things if and only if the wifi actually made a successful connection, so that's why I've stuck with keeping things inside the handler. Also, now with the into delays, everything has been working 100% of the time.

    Here's what I finally settled on (relevant functions only):

    function onInit() {
    
      SPI1.setup({mosi:A7, miso:A6, sck:A5});
      bme = require("BME280").connectSPI(SPI1, B1);
    
      setTimeout(startApAndStation, 2000);
    
      wifi.on('disconnected', () => {
        mqtt.disconnect();
        wifi.stopAP();
        clearInterval(timer);
      });
    
      wifi.on('connected', () => {
        setTimeout(() => { require("http").createServer(pageReq).li­sten(8080); }, 2000);
        setTimeout(() => { mqtt = require("MQTT").connect({host: "xxx.xxx.xxx.xxx"}); }, 4000);
        setTimeout (() => { dataInterval(); }, 8000);
      });
    }
    
    function startApAndStation() {
      wifi.startAP('AgilatechESP', { password: 'xxxxxxxx', authMode: 'wpa2' }, (err) => {
        if (err) throw err;
        else wifi.setAPIP({ip:"10.0.132.1", gw:"10.0.132.1", netmask:"255.255.255.0"}, (err) => {
          if (err) throw err;
          else wifi.connect("HeartJS", {password : "xxxxxxxxxx"}, (err) => {
            if (err) throw err;
          });
        });
      });
    }
    
    function dataInterval() {
      timer = setInterval( () => {
        loadData();
        mqtt.publish("agt/sensor/bme280/temp", temp);
        mqtt.publish("agt/sensor/bme280/pressure­", pressure);
        mqtt.publish("agt/sensor/bme280/humidity­", humidity);
      }, 10000);
    }
    
  • Mon 2020.01.20

    @ScottyMT,

    I like the improved, reordered layout of the code block in revison at #9 post. Nicely done example for others requiring MQTT.

    ref last pp. of post #7

    It should be noted that, L6 and L15 will execute within microseconds of each other, not 2 seconds apart, attempting to force Espruino into potential WiFi setup conflict. This can be proven using the debugger and WebIDE to reveal this fact.

    EDIT: Tue 2020.01.21 clarification regarding Gordon's comment in #11 at least one person is actually reading and following along ;-)     Edits crossed paths and left out the qualifier: 'should the 'connected' event L15 occur near simultaneously after the first L6 setTimeout() interval has started' This would then be attempting to set up the AP at the approximate same time the server is attempted to be set up. A lot to do with WiFi simultaneously! Although that reference wasn't the perfect example, my point shouldn't be overlooked. Two seconds is not passing waiting for L8 and then L14. During development, I get around this by using prime numbers with trailing '000' so they most likely won't overlap and as to more easily track which timer trips and in which order. Having identical interval values will cloud that understanding.

    The code block suggestion that @Gordon graciously whipped together in post #8 was what I implied in the pseudo code reference in 2nd to last pp. in #7 post.

    Here is an excellent explanation:

    https://johnresig.com/blog/how-javascrip­t-timers-work/

    Although your setup may work satisfactory, using the logic layout in Gordon's code block and tweaking the timeouts, should allow for a solid working state in around a second or so, and not the ten seconds currently required.

About

Avatar for Robin @Robin started