So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By Bootalito
#63801 You could use a Json file in SPIFFS, but this is insecure, and not necessary. There is a section of flash that the ESP stores the last wifi credentials passed into it, whether it succeeds or not, and is stored there permanently! In fact the only way to get rid of it is to run ESP.eraseConfig().
Therefore if you download a project with the credentials hard coded (like in every example sketch on the interwebs) it will save these credentials automatically to the section of flash memory permanently. Then you can download another sketch and simply use WiFi.begin();(with no parameters) instead of WiFi.begin(const char * SSID, const char * password); like in all of the example sketches
(Note the above commands are using the ESP8266 Arduino core)
Here is my WiFiConnect function that I use. See the section with the comment "//try to connect to new WiFi:
Code: Select allbool 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
}//===============================================================================================


And the function referenced in the above example:
Code: Select allvoid 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);
   }
}//===============================================================================================


These are from my project: https://github.com/terryjmyers/_ESP8266Template.git
User avatar
By Hein du Plessis
#63811 Oh thanks - I was just giving an example, I mean to store other config values, such as credentials and pin numbers. In another project I also want to store the last 5 analogue readings onboard and retrieve it later, even if the unit has lost power in the mean time.

Is it possible?