Where to store permanent data?
Posted: Thu Mar 16, 2017 12:35 pm
How do I store variables into non-volatile memory? I'd like to permanently store a configured SSID and password, for example.
-->
Open Community Forum for ESP8266, Come share Arduino and IoT (Internet of Things)
https://www.esp8266.com/
bool WiFiConnect(String ssid = "", String pw = "", IPAddress ip = STAip, IPAddress sn = STAsubnet, IPAddress gw = STAgateway) {
/*
Try connect to a WiFi network
if sucessful, store configuration in the EEPROM
Give up after 10 seconds
if not sucessful, revert to previous configuration if you were connected when you entered this function
Returns:
Returns a 0 if failed to connect or reconnect to original network
Returns a 1 if sucessfully connected to new network
Returns a 2 if failed to connect but sucessfully returns to original network
*/
//Trim whitespace from the beg and end of the strings in case someone added a space or copied ans pasted from a text file or something
ssid.trim();
pw.trim();
//declare some variables
int i = 0; //iterator
String s; s.reserve(128);
bool wasConnected = false;
//Store the current configuration in case the network you are going to connect to doesn't work and you need to reconnect
String _ssid = WiFi.SSID();
String _pw = WiFi.psk();
IPAddress _ip = WiFi.localIP();
IPAddress _sn = WiFi.subnetMask();
IPAddress _gw = WiFi.gatewayIP();
//WiFi was already connected remember variables for the current configuration in case it doesn't work
if (WiFi.status() == WL_CONNECTED) {
wasConnected = true;
wifiDisconnect(); //disconnect from any wifi
}
//Enable STation mode
WiFi.enableSTA(1); //doesn't work unless you put this here for some reason
//try to connect to new WiFi
if (!AutoDHCP) WiFi.config(ip, gw, sn); //If static IP Address is selected set ip, sn, and gw
if (ssid == "") { //passed in parameters are blank try to connect using stored credentials on the ESP
s = F("Connecting to WiFi network using stored credentials please wait.");
WiFi.begin();//initiate the connection
}
else { //an SSID and password were passed in, try to connect using these
s = F("Connecting to WiFi network <"); s += ssid; s += F("> please wait.");
WiFi.begin(ssid.c_str(), pw.c_str());//initiate the connection
}
Serial.print(s); TelnetPrintAllClients(s);
while (i < 20) {
if (WiFi.status() == WL_CONNECTED) {
s = F("connected to: "); s += WiFi.SSID(); s += F("\r\n"); s += wifiGetStatus();
Serial.print(s); TelnetPrintAllClients(s);
Serial.flush();//delete all of the incoming serial buffer because we havn't been monitoring it while connecting and we don't want any commands that are queued up to be immediatly processed
return true; //return a success!
}
delay(500);
s = (".");
Serial.print(s); TelnetPrintAllClients(s);
i++;
}
//You failed to connet to the WiFi...that stinks
s = F("FAILED to connect to network\r\n");
Serial.print(s);// TelnetPrintAllClients(s);
//If you were connected when you came into this routine, load those credentials back into the ESP and restart
if (wasConnected == true) {
WiFi.begin(_ssid.c_str(), _pw.c_str());//initiate the connection to the previous network
Restart();
}
return false;
//You failed to connect to a network, just return
}//===============================================================================================
void wifiDisconnect() {
String s; s.reserve(64);
//disconnect from current wifi if applicable, and ensure a disconnection
s = F("Disconnecting from current WiFi\r\n");
Serial.print(s); TelnetPrintAllClients(s);
WiFi.enableSTA(0);
WiFi.disconnect(); //disconnect
while (WiFi.status() == WL_CONNECTED) {//Not sure if this is needed but I think I saw it once where I disconnected then immeidatly tryied to evaluate status and it still came back connected so.....its here
delay(1);
}
}//===============================================================================================