Connecting to one of available WIFI's

Posted on
  • Hello,

    Please, could anyone help to solve implementation problem?!
    I'm making a thing which I want to take sometimes to another place. As a result, I have two different wifi's(one at home, one at an office), and want to check which is present and connect to one of them. Wifi.scan doesn't work on my ESP8266 (NodeMCU Amica), so I tried 'if' statement. But can't write it correctly.
    How is better to solve such task, maybe some of you have faced with it already.
    Thanks!!!

    var ssid2 = "WIFI2";
    var password2 = { password : "PASS2"};
    var ssid = "WIFI1";
    var password = { password : "PASS1"};
    
    var wifi;
    
    wifi = require("Wifi");
    wifi.connect(ssid, password, function(err) {
      if (err){
        console.log("Connection error: " + err);
        wifi.connect(ssid2, password2, function(err)){
          if (err){
            console.log("Connection error: " + err);
            return;
          }
          console.log("Connected! WIFI2");
      }
      console.log("Connected! WIFI1");
    });
    
  • Hi @Hansi,

    I tried a simple snippet and found that callback is not processed if SSID is not available.

    Created issue #1297

  • Does this Wifi scan snippet work for you?

    require('Wifi').scan(function(e) {
      console.log(e);
    });
    
    /*
    output:
    [
      { "rssi": -78, "channel": 1, "authMode": 4, "isHidden": false,
        "ssid": "SSID1",
        "mac": "b8:8d:12:68:24:3f"
       },
      { "rssi": -52, "channel": 1, "authMode": 4, "isHidden": false,
        "ssid": "SSID2",
        "mac": "a2:a0:bb:f2:04:59"
       }
     ]
    
    */
    
  • Hello @MaBe,
    Yes, this one has returned me local wifi ap (just for info I have 1.93v).
    Now I can write a workaround just parsing it passing the correct value in ssid and password. Will try it today.

    Thanks!

  • See attached file ServeHello3.post.js

    The first case

    var SSID1="faux";
    var key1= "a6a69";
    var SSID2="faux2";
    var key2= "a6a70";
    var SSID=SSID1;
    var key=key1;
    
     1v94 Copyright 2016 G.Williams
    >Start
    Start connection process
    end require
    end test 0
    =undefined
    Reset the ESP8266
    end reset
    Hello World
    Connecting to WiFi  faux
    end connect
    Hello World
    Hello World
    Hello World
    null
    IP=  192.168.1.9
    null
    Wi-Fi Connected
    

    And the other case

    var SSID1="faux";
    var key1= "a6a69";
    var SSID2="faux2";
    var key2= "a6a70";
    var SSID=SSID2;
    var key=key2;
    
     1v94 Copyright 2016 G.Williams
    >Start
    Start connection process
    end require
    end test 0
    =undefined
    Reset the ESP8266
    end reset
    Hello World
    Connecting to WiFi  faux2
    end connect
    Hello World
    Hello World
    Hello World
    Hello World
    Hello World
    Hello World
    Hello World
    Hello World
    Hello World
    WiFi connect failed: FAIL
    errr 1
    Hello World
    faux
    Start
    Start connection process
    end require
    end test 0
    Reset the ESP8266
    end reset
    Hello World
    Connecting to WiFi  faux
    end connect
    Hello World
    Hello World
    Hello World
    null
    IP=  192.168.1.9
    null
    Wi-Fi Connected
    > 
    

    1 Attachment

  • Cleaning it up a bit:

    The code

    //ServeHello4.js  7Jan2018
    //http//192.168.1.6:8080
    //espruino board with ESP8266
    //PICO  with ESP8266
    //var Hardware=0; //Espruino board
    var Hardware =1; //PICO
    // list of Wifi and passwords
    var hotspots=[
      {SSID:"faux",key:"a6a69"},
      {SSID:"faux2",key:"a6a70"}
    ];
    var hotspot=1;
    console.log(hotspots.length);
    var Serial;
    var Startagain=0;
    var myinterval;
    function test(){
    if(Hardware===1)Serial=Serial2;
    if(Hardware===0)Serial=Serial4;
    if(Hardware===1){
     digitalWrite(B9,1); // enable on Pico Shim V2
     Serial.setup(115200, { rx: A3, tx : A2 }); //Pico
    }
    if(Hardware===0)Serial.setup(115200, { rx: C11, tx : C10 }); 
    //espruino board
    console.log("Start connection process");
    var wifi = require("ESP8266WiFi_0v25").connect(Seri­al, function(err) {
     if (err){Startagain=1;return 1;}
     console.log("Reset the ESP8266");
     wifi.reset(function(err) {
      if (err){Startagain=1;return 1;}
      console.log("Try Connecting to WiFi ",hotspots[hotspot].SSID);
      wifi.connect(hotspots[hotspot].SSID,hots­pots[hotspot].key, function(err) {console.log(err);
       if (err){Startagain=1;return 1;
    }
        wifi.getIP(function(l,ip){
         if (err){Startagain=1;return;}
         console.log("IP= ",ip,"\n\r"+l);
         console.log("Wi-Fi Connected");
         clearInterval( myinterval);
         var http = require("http");
         //do a server
         http.createServer(function (req, res) {
          res.writeHead(200);
          res.end("Hello World");
         }).listen(8080); //end server
      });//end getIP
      console.log("end getIP");
     });//end connect
     console.log("end connect");
    });//end reset
     console.log("end reset");
    });//end require
      console.log("end require");
      return 0;
    }//end test
    function ccc(){
      var x=0;
    console.log("Start");
    x=test();
    console.log("end test",x);
    if(x!==0){
      console.log("Try again");//error seen
    }
    }//end cccc
    myinterval=setInterval(function () {
      console.log("Test for error");
      if(Startagain){
       Startagain=0;
       hotspot++;
       if(hotspot>=hotspots.length)hotspot=0;
       console.log(hotspots[hotspot].SSID);
       ccc();
      }//end of errr
    }, 2000);
    ccc();
    

    The left pane output, note faux2 is not in range, but faux is

    >2
    Start
    Start connection process
    end require
    end test 0
    =undefined
    Reset the ESP8266
    end reset
    Test for error
    Try Connecting to WiFi  faux2
    end connect
    Test for error
    Test for error
    Test for error
    Test for error
    Test for error
    Test for error
    Test for error
    Test for error
    WiFi connect failed: FAIL
    Test for error
    faux
    Start
    Start connection process
    end require
    end test 0
    Reset the ESP8266
    end reset
    Test for error
    Try Connecting to WiFi  faux
    end connect
    Test for error
    Test for error
    Test for error
    null
    end getIP
    IP=  192.168.1.9
    null
    Wi-Fi Connected
    

    1 Attachment

  • Listing the WiFI APs

    Using either a PICO or an Espruino Original Board connected via the serial port to and ESP8266.

    The code:

    //GetAPs.js  11Jan2018
    // ESP8266 connected via serial port to PICO or Espruino Board
    
    //espruino board with ESP8266
    //PICO  with ESP8266
    
    //var Hardware=0; //Espruino board
    var Hardware =1; //PICO
    
    var Serial;
    
    //setup serial port
    if(Hardware===1)Serial=Serial2;
    if(Hardware===0)Serial=Serial4;
    
    if(Hardware===1){
     digitalWrite(B9,1); // enable on Pico Shim V2
     Serial.setup(115200, { rx: A3, tx : A2 }); //Pico
    }
    if(Hardware===0)Serial.setup(115200, { rx: C11, tx : C10 }); 
    //espruino board
    
    var cmd="";
    Serial.on('data', function (data) { 
    //  console.log(data);
      cmd+=data;
      var idx = cmd.indexOf("\r");
      while (idx>=0) { 
        var line = cmd.substr(0,idx);
        cmd = cmd.substr(idx+1);
        print(line);
        idx = cmd.indexOf("\r");
      }
    });
    
    function sendAT(){
      Serial.print("AT");
    }//end sendAT
    
    
    sendAT();
    setTimeout(function () {
      console.log("Hello World1");
      Serial.write("+++");
    setTimeout(function () {
      console.log("Hello World2");
      Serial.write("AT+CWLAP\r\n");
    }, 5000);
    }, 5000);
    
    

    Sample Output

     1v94 Copyright 2016 G.Williams
    >
    =undefined
    Garbage Characters
    >
    Ai-Thinker Technology Co. Ltd.
    ready
    >
    WIFI CONNECTED
    >
    WIFI GOT IP
    Hello World1
    Hello World2
    >
    +++AT+CWLAP
    >
    >
    +CWLAP:(4,"faux2",-51,"04:4e:5a:94:24:e4­",1,-36,0)
    >
    +CWLAP:(0,"xfinitywifi",-87,"c6:27:95:38­:8b:48",1,-19,0)
    >
    +CWLAP:(3,"ATT3x349v4",-91,"cc:65:ad:7e:­e2:20",1,-22,0)
    >
    +CWLAP:(0,"xfinitywifi",-48,"16:4e:5a:94­:24:e4",1,32767,0)
    >
    +CWLAP:(4,"HOME220-2.4",-83,"70:54:d2:07­:7c:c0",6,-17,0)
    >
    +CWLAP:(1,"faux",-50,"20:4e:7f:03:87:cd"­,6,-24,0)
    >
    +CWLAP:(0,"xfinitywifi",-82,"70:54:d2:07­:7c:c2",6,-17,0)
    >
    +CWLAP:(3,"AMKUS146",-93,"9e:d3:6d:bb:a8­:5c",7,-17,0)
    >
    +CWLAP:(3,"Gearfam",-93,"78:f2:9e:0e:74:­c8",6,-26,0)
    >
    +CWLAP:(4,"ATT6UuI5FT",-88,"f8:18:97:b6:­71:32",9,50,0)
    >
    +CWLAP:(0,"xfinitywifi",-79,"0c:54:a5:64­:cf:c2",11,-36,0)
    >
    +CWLAP:(4,"HOME-24D1-2.4",-79,"0c:54:a5:­64:cf:c0",11,-34,0)
    >
    +CWLAP:(4,"ATT5wAn8b9",-80,"f8:18:97:d5:­63:06",11,51,0)
    >
    +CWLAP:(0,"xfinitywifi",-92,"0c:54:a5:6b­:f8:aa",11,-21,0)
    OK
    >
    
    

    1 Attachment

  • GetAps1 Using the functions in the WiFi module

    The Code

    //getAps1.js  11Jan2018
    //espruino board with ESP8266
    //PICO  with ESP8266
    
    //var Hardware=0; //Espruino board
    var Hardware =1; //PICO
    var Serial;
    var Startagain=0;
    var myinterval;
    
    function test(){
    if(Hardware===1)Serial=Serial2;
    if(Hardware===0)Serial=Serial4;
    
    if(Hardware===1){
     digitalWrite(B9,1); // enable on Pico Shim V2
     Serial.setup(115200, { rx: A3, tx : A2 }); //Pico
    }
    if(Hardware===0)Serial.setup(115200, { rx: C11, tx : C10 }); 
    //espruino board
    
    console.log("Start connection process");
    var wifi = require("ESP8266WiFi_0v25").connect(Seri­al, function(err) {
     if (err){Startagain=1;return;}
     console.log("Reset the ESP8266");
     wifi.reset(function(err) {
      if (err){Startagain=1;return;}
      wifi.getAPs(function(err,reply){console.­log(err,reply);
      wifi.getVersion(function(err,reply){cons­ole.log(err,reply);});
      });
    });//end reset
     console.log("end reset");
    });//end require
      console.log("end require");
    }//end test
    
    myinterval=setInterval(function () {
      console.log("Test for error");
      if(Startagain){
       Startagain=0;
       test();
      }//end of Startagain
    }, 2000);
    
    test();
    

    Sample Output

     1v94 Copyright 2016 G.Williams
    >Start connection process
    end require
    =undefined
    Reset the ESP8266
    end reset
    Test for error
    Test for error
    null [
      {
        "ssid": "ATT5wAn8b9",
        "enc": "wpa_wpa2_psk",
        "signal": -79,
        "mac": "f8:18:97:d5:63:06"
       },
      {
        "ssid": "ATT3x349v4",
        "enc": "wpa2_psk",
        "signal": -84,
        "mac": "cc:65:ad:7e:e2:20"
       },
      {
        "ssid": "xfinitywifi",
        "enc": "open",
        "signal": -81,
        "mac": "c6:27:95:38:8b:48"
       },
      {
        "ssid": "xfinitywifi",
        "enc": "open",
        "signal": -48,
        "mac": "16:4e:5a:94:24:e4"
       },
      {
        "ssid": "xfinitywifi",
        "enc": "open",
        "signal": -90,
        "mac": "c6:27:95:38:c5:20"
       },
      {
        "ssid": "BLACKHAWKS19",
        "enc": "wpa_wpa2_psk",
        "signal": -81,
        "mac": "c4:27:95:38:8b:46"
       },
      {
        "ssid": "NETGEAR60",
        "enc": "wpa2_psk",
        "signal": -85,
        "mac": "e4:f4:c6:17:27:50"
       },
      {
        "ssid": "NETGEAR-Guest",
        "enc": "wpa2_psk",
        "signal": -84,
        "mac": "e6:f4:c6:17:27:51"
       },
      {
        "ssid": "faux",
        "enc": "wep",
        "signal": -49,
        "mac": "20:4e:7f:03:87:cd"
       },
      {
        "ssid": "HOME220-2.4",
        "enc": "wpa_wpa2_psk",
        "signal": -82,
        "mac": "70:54:d2:07:7c:c0"
       },
      {
        "ssid": "xfinitywifi",
        "enc": "open",
        "signal": -84,
        "mac": "70:54:d2:07:7c:c2"
       },
      {
        "ssid": "AMKUS146",
        "enc": "wpa2_psk",
        "signal": -90,
        "mac": "9e:d3:6d:bb:a8:5c"
       },
      {
        "ssid": "ATT6UuI5FT",
        "enc": "wpa_wpa2_psk",
        "signal": -88,
        "mac": "f8:18:97:b6:71:32"
       },
      {
        "ssid": "HP-Print-1E-Officejet Pro 8610",
        "enc": "wpa2_psk",
        "signal": -88,
        "mac": "fc:15:b4:aa:a9:1e"
       },
      {
        "ssid": "HOME-24D1-2.4",
        "enc": "wpa_wpa2_psk",
        "signal": -83,
        "mac": "0c:54:a5:64:cf:c0"
       },
      {
        "ssid": "xfinitywifi",
        "enc": "open",
        "signal": -83,
        "mac": "0c:54:a5:64:cf:c2"
       }
     ]
    null AT version:0.60.0.0(Jan 29 2016 15:10:17)
    Test for error
    Test for error
    Test for error
    >reset();
    =undefined
    

    1 Attachment

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Connecting to one of available WIFI's

Posted by Avatar for Hansi @Hansi

Actions