Earn Maker Points on Every Order! Learn More!

Blynk Program with Multiple WiFi Networks

Back to Blog
Blynk Program with Multiple WiFi Networks - Blynk Program with Multiple WiFi Networks - Blynk Program with Multiple WiFi Networks - Blynk Program with Multiple WiFi Networks

Blynk Program with Multiple WiFi Networks

Blynk is an IoT platform with drag and drop mobile Application. It is an Open-Source Cloud IoT platform to control appliances and monitor sensor data from anywhere in the World. You just need to upload the standard Blynk Program into it (No Coding skills needed) and rest is done by the App itself. This makes the IoT applications with Blynk are Standalone.

If you implemented a Home Automation Project with NodeMCU, After few days the password of the WiFi Router was changed or Not turning ON, the NodeMCU gets disconnected from WiFi. The NodeMCU is needed to be programmed again with new SSID and Password to make it work. If you have more than one WiFi Networks available in your home, then you can connect it to any one of them when one fails (Like a backup). The default Blynk program provides only one Wifi Network support. To solve this we need to create an SSID and Password array to store the WiFi Credentials in an array. Initially, all the networks are scanned and verify the SSID with the known networks then try connecting to it. The modified code for the Blynk is added below.

Modify the following code with your Authentication Token with different SSID and Password available in your Home then Upload it to your Board.

CODE:

/*************************************************************
Download latest Blynk library here:
https://github.com/blynkkk/blynk-library/releases/latest

Note: This requires ESP8266 support package:
https://github.com/esp8266/Arduino
*************************************************************/
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).

char auth[] = "YourAuthToken"; //Put Your Auth Token Here

//Array to store Network Credentials for different WiFi Networks
const char* KNOWN_SSID[] = {"Nucleus", "Factory Forward", "SSID3"}; //Put all Your WiFi Network Names
const char* KNOWN_PASSWORD[] = {"", "1234567809", "Pass3"}; //Put the WiFi Passwords in same order. For Open networks leave the password blank inside the double quotes.

const int KNOWN_SSID_COUNT = sizeof(KNOWN_SSID) / sizeof(KNOWN_SSID[0]); // number of known networks

void setup() {
boolean wifiFound = false;
int i, n;

Serial.begin(9600);

// ----------------------------------------------------------------
// Set WiFi to station mode and disconnect from an AP if it was previously connected
// ----------------------------------------------------------------
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
Serial.println("Setup done");

// ----------------------------------------------------------------
// WiFi.scanNetworks will return the number of networks found
// ----------------------------------------------------------------
Serial.println(F("Scan start"));
int nbVisibleNetworks = WiFi.scanNetworks();
Serial.println(F("Scan Completed"));
if (nbVisibleNetworks == 0) {
Serial.println(F("No networks found. Reset to try again"));
while (true); // no need to go further, hang in there, will auto launch the Soft WDT reset
}

// ----------------------------------------------------------------
// if you arrive here at least some networks are visible
// ----------------------------------------------------------------
Serial.print(nbVisibleNetworks);
Serial.println(" network(s) found");

// ----------------------------------------------------------------
// check if we recognize one by comparing the visible networks
// one by one with our list of known networks
// ----------------------------------------------------------------

for (i = 0; i < nbVisibleNetworks; ++i) {
Serial.println(WiFi.SSID(i)); // Print current SSID
for (n = 0; n < KNOWN_SSID_COUNT; n++) { // walk through the list of known SSID and check for a match
if (strcmp(KNOWN_SSID[n], WiFi.SSID(i).c_str())) {
Serial.print(F("\tNot matching "));
Serial.println(KNOWN_SSID[n]);
} else { // we got a match
wifiFound = true;
break; // n is the network index we found
}
} // end for each known wifi SSID
if (wifiFound) break; // break from the "for each visible network" loop
}

if (!wifiFound) {
Serial.println(F("No Known Network identified. Reset to try again"));
while (true);

}

const char* ssid = (KNOWN_SSID[n]);
const char* pass = (KNOWN_PASSWORD[n]);
Serial.println(WiFi.localIP());
Blynk.begin(auth, ssid, pass);
Serial.println("Blynk Connected"); //Connected and Authenticated with Blynk Server
}

void loop()
{
Blynk.run();
}

 

Once you uploaded the Code it will start scanning for Available networks and connecting to the Known Network. You can open the Serial Monitor and check the status of the complete process. It will also display the network it is connected right now. Keep Blynking!

InkedBlynk with Multi Network_LI

Share this post

Comments (3)

  • Temosan Technologies

    good day Tutor, i currently have a project at hand. home automation using Blynk and nodeMCU. please, i will need a total quidiance on this project including circuit diagram and workable code. many thanks in anticipation.

    May 19, 2019 at 10:09 PM
  • jakimi2325

    Hey , nice tutorial tried it , only issue i have is all my WiFi SSID’s are hidden and this doesn’t work with hidden SSID’s .I’ve tried giving overload function WiFi.scanNetworks(async, show_hidden) like the one mentioned in this doc https://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/scan-class.html but still it doesn’t connect .it always loops in a wifi search .

    I’ve tried searching for the solution but couldn’t find any working solutions for it , It would be so grateful of you if you could help me.

    February 21, 2020 at 8:43 AM

Leave a Reply

Back to Blog