• Give this a shot:

     var oWifi = require('Wifi');
    oWifi.scan(function(aAp) {
      aAp.sort(function(a, b) {
        var r = a.rssi === b.rssi ? 0 : (a.rssi < b.rssi ? 1 : -1);
        return r;
      });
      console.log(aAp);
      var i=0, k, foundMy;
      while (!foundMy && i<aAp.length) {
        k = 0; // <---- restart k IN outer loop... only once outside is not enough
        while (!foundMy && k<aMyAp.length) {
          if (aAp[i].ssid === aMyAp[k].ssid) { // <---- compare [i] w/ [k]
            foundMy = aMyAp[k];                //          ...and not [i] w/ [i] 
          }
          k++;
        }
        i++;
      }
      if (foundMy) {
        oWifi.connect(foundMy.ssid, {password: foundMy.pwd}, function(err) {
          if (err===null) {
             console.log('Connection success to:', foundMy.ssid);
          }
        });
      } else {
        console.log('no Ap found');
      }
    });
    

    ...there were obviously also some other issues... - see // <---- comments - were there?

    What is the sort helping with? ...do you have duplicate ssid in scan result and want only the one to match w/ highest rssi value?

    (Since labels are bad in general and worst in compiler free environment - as Espruino - in regard of nested control management, labels are not implemented in Espruino. Why though the simple, one-level break is not working... I don't know).

  • Thanks for your tips, you are right there are some other issues. I came not across this yet because I got this compilation error which was irritating me. The mentioned error message does not go away, even with the corrected code. Of course I can remove the the breaks, but the code is not efficient because it does not stop looping when the correct record is found.

About

Avatar for fanThomas @fanThomas started