This will attempt to read the files from SD and on failure default to SPIFFS, if the SD & files are present offer to copy the data to SPIFFS.......
/*
Connect the SDCard to the following pins on the esp8266:
| GPIO NodeMCU Name |
===========================
| 15 D8 SS |
| 13 D7 MOSI |
| 12 D6 MISO |
| 14 D5 SCK |
===========================
Files Required on SDCard :
ssid.txt Maximum length 24 Chars, increase if more Chars needed !!! ssid[24]
wifipsk.txt Maximum length 32 Chars, increase if more Chars needed !!! wifipsk[32]
Flash and SDCard config storage and retrieval
*/
#include <ESP8266WiFi.h>
#include <SPI.h>
#include <SD.h>
// Solution to FS/SD coexistance.....https://github.com/DedeHai
#define FS_NO_GLOBALS //allow spiffs to coexist with SD card, define BEFORE including FS.h
// declare as fs:: before command e.g.
// fs::Dir dir = SPIFFS.openDir("/");
// fs::File f = SPIFFS.open("/ssid.txt", "w");
#include <FS.h> //spiff file system
char ssid[24]; // Storage for your network SSID (name) from SDCard/SPIFFS
char pass[32]; // Strorage for your network password from SDCard/SPIFFS
const int buttonPin = 0; // Using Flash Buttom found in most circuits......
int buttonState;
String tmsg,tmsg1;
//format bytes
String formatBytes(size_t bytes){
if (bytes < 1024){
return String(bytes)+"B";
} else if(bytes < (1024 * 1024)){
return String(bytes/1024.0)+"KB";
} else if(bytes < (1024 * 1024 * 1024)){
return String(bytes/1024.0/1024.0)+"MB";
} else {
return String(bytes/1024.0/1024.0/1024.0)+"GB";
}
}
void setup() {
// put your setup code here, to run once:
pinMode(buttonPin, INPUT);
Serial.begin(115200);
Serial.println();
Serial.println();
Serial.print(F("Initializing SPIFFS......."));
// initialize the SPIFFS
if (!SPIFFS.begin()) {
Serial.println(F("initialization failed!"));
while(1);
}
Serial.println(F("initialization done."));
// clear any old data....
// SPIFFS.format();
// SPIFFS.remove("/humidlog.CSV");
/* delay(200); // Use these lines to write a default to FS............
fs::File f = SPIFFS.open("/ssid.txt", "w");
f.print("default_SSID");
f.close();
delay(50);
fs::File f1 = SPIFFS.open("/wifipsk.txt", "w");
f1.print("default_Password");
f1.close();*/
delay(50);
Serial.println(F("SPIFFS Contents....."));
fs::Dir dir = SPIFFS.openDir("/");
while (dir.next()) {
String fileName = dir.fileName();
size_t fileSize = dir.fileSize();
Serial.printf(" %s, size: %s\n", fileName.c_str(), formatBytes(fileSize).c_str());
}
Serial.println(" ");
Serial.print(F("Initializing SD card..."));
if (!SD.begin(15)) {
Serial.println(F("initialization failed! Card Not Present!"));
Serial.println(F("Loading Config From SPIFFS......."));
fs::File myFile0 = SPIFFS.open("/ssid.txt", "r");
if (myFile0) {
tmsg="";
Serial.println("ssid.txt:");
// read from the file until there's nothing else in it:
while (myFile0.available()) {
tmsg=myFile0.readString();
}
tmsg.trim();
int len = tmsg.length()+1;
ssid[len];
tmsg.toCharArray(ssid, len);
// close the file:
myFile0.close();
Serial.println(ssid);
}
fs::File myFile1 = SPIFFS.open("/wifipsk.txt", "r");
if (myFile1) {
tmsg1="";
Serial.println("wifipsk.txt:");
// read from the file until there's nothing else in it:
while (myFile1.available()) {
tmsg1=myFile1.readString();
}
tmsg1.trim();
int len1 = tmsg1.length()+1;
pass[len1];
tmsg1.toCharArray(pass, len1);
// close the file:
myFile1.close();
Serial.println(pass);
}
}else{
Serial.println(F("initialization done."));
File myFile0 = SD.open("ssid.txt");
if (myFile0) {
tmsg="";
Serial.println("ssid.txt:");
// read from the file until there's nothing else in it:
while (myFile0.available()) {
tmsg=myFile0.readString();
}
tmsg.trim();
int len = tmsg.length()+1;
ssid[len];
tmsg.toCharArray(ssid, len);
// close the file:
myFile0.close();
Serial.println(ssid);
}
File myFile1 = SD.open("wifipsk.txt");
if (myFile1) {
tmsg1="";
Serial.println("wifipsk.txt:");
// read from the file until there's nothing else in it:
while (myFile1.available()) {
tmsg1=myFile1.readString();
}
tmsg1.trim();
int len1 = tmsg1.length()+1;
pass[len1];
tmsg1.toCharArray(pass, len1);
// close the file:
myFile1.close();
Serial.println(pass);
// Use these lines to write a default to FS............
Serial.println("Press the Flash button for 1 second to update FS Config.....(within 20 Secs....)");
for(int i=0;i<20;i++){
if(i==10){Serial.println("");}
Serial.print("...." + String(20-i));
delay(1000);
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
Serial.println("\r\nUpdating FS /wifipsk.txt");
delay(50);
fs::File f1 = SPIFFS.open("/wifipsk.txt", "w");
f1.print(tmsg1);
f1.close();
delay(200);
Serial.println("Updating FS /ssid.txt");
delay(50);
fs::File f = SPIFFS.open("/ssid.txt", "w");
f.print(tmsg);
f.close();
i=21;
}
}
}
}
Serial.print(F("\r\n\r\nConnecting to "));
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(F("."));
}
Serial.println(F(""));
Serial.println(F("WiFi connected"));
Serial.println(F("IP address: "));
Serial.println(WiFi.localIP());
}
void loop() {
// put your main code here, to run repeatedly:
}