Announcement

Collapse
No announcement yet.

Just storing this here - for future post in the OPEN hardware section

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Just storing this here - for future post in the OPEN hardware section


    I have (found) some code (untested -- they don't like this stuff where I work) that allows an ESP8266 to be able to store multiple credentials for wifi.
    This allows the ESP to log on to the networks that you have defined (in code or eeprom - and possibly other storage [SD..)
    Is there an AD similar example? (with or without OTA support)
    Note: this code example is for ESP8266 and uses the ArduinoOTA to allow reprogramming over wifi.


    Code:
    //Scan and connect to known network
    
    // This is for a NodeMCU ESP12 / ESP12E module in "Arduino mode"
    // simple example, does not support 2 SSIDs with same name, different pwd.
    
    #include <ArduinoOTA.h>
    #include "ESP8266WiFi.h"
    #include <ESP8266mDNS.h>
    #include <WiFiUdp.h>
    #include <WiFiClient.h>
    
    // DEFINE HERE THE KNOWN NETWORKS
    const char* KNOWN_SSID[] = {"SSID0", "SSID1", "SSID2", "SSID3"};
    const char* KNOWN_PASSWORD[] = {"PassW0rd", "PassW1rd", "Passw2rd", "PassW3rd"};
    
    const int KNOWN_SSID_COUNT = sizeof(KNOWN_SSID) / sizeof(KNOWN_SSID[0]); // number of known networks
    const uint8_t ledPin = 0;
    const uint8_t lifeBlip = 10;
    
    void setup() {
    boolean wifiFound = false;
    int i, n;
    
    Serial.begin(115200);
    
    // ----------------------------------------------------------------
    // 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 done"));
    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
    } // end for each visible network
    
    if (!wifiFound) {
    Serial.println(F("no Known network identified. 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 you found 1 known SSID
    // ----------------------------------------------------------------
    Serial.print(F("\nConnecting to "));
    Serial.println(KNOWN_SSID[n]);
    
    // ----------------------------------------------------------------
    // We try to connect to the WiFi network we found
    // ----------------------------------------------------------------
    WiFi.begin(KNOWN_SSID[n], KNOWN_PASSWORD[n]);
    
    while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    }
    Serial.println("");
    
    // ----------------------------------------------------------------
    // SUCCESS, you are connected to the known WiFi network
    // ----------------------------------------------------------------
    Serial.println(F("WiFi connected, your IP address is "));
    Serial.println(WiFi.localIP());
    ArduinoOTA.onStart([]() {
    String type;
    if (ArduinoOTA.getCommand() == U_FLASH) {
    type = "sketch";
    } else { // U_FS
    type = "filesystem";
    }
    
    // NOTE: if updating FS this would be the place to unmount FS using FS.end()
    Serial.println("Start updating " + type);
    });
    ArduinoOTA.onEnd([]() {
    Serial.println("\nEnd");
    });
    ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
    Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
    });
    ArduinoOTA.onError([](ota_error_t error) {
    Serial.printf("Error[%u]: ", error);
    if (error == OTA_AUTH_ERROR) {
    Serial.println("Auth Failed");
    } else if (error == OTA_BEGIN_ERROR) {
    Serial.println("Begin Failed");
    } else if (error == OTA_CONNECT_ERROR) {
    Serial.println("Connect Failed");
    } else if (error == OTA_RECEIVE_ERROR) {
    Serial.println("Receive Failed");
    } else if (error == OTA_END_ERROR) {
    Serial.println("End Failed");
    }
    });
    ArduinoOTA.begin();
    Serial.println("Ready");
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
    pinMode(ledPin, OUTPUT);
    //the led pin is connected to a button and led, so it is pulled high
    //we pull it low to activate the red (onboard) LED
    digitalWrite(ledPin, HIGH);
    for (i = 0; i < lifeBlip; i++) {
    digitalWrite(ledPin, LOW);
    delay(10);
    digitalWrite(ledPin, HIGH);
    delay(250);
    };
    
    }
    
    void loop() {
    ArduinoOTA.handle();
    }
Working...
X