-->
Page 1 of 1

SSID & PASS from SDCard & Copy to SPIFFS

PostPosted: Fri Jan 13, 2017 9:10 am
by GengusKahn
This is a basic example to demonstrate retrieving the WiFi configuration settings from/to the SPIFFS & an SDCard breakout.

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.......

Code: Select all/* 
   
   
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:

}

Re: SSID & PASS from SDCard & Copy to SPIFFS

PostPosted: Sat Jan 14, 2017 10:19 am
by rudy
I got a compile error. I am using Arduino 1.6.13 and current stable ESP8266.


SD-SPIFFS-init:135: error: 'File' was not declared in this scope

File myFile0 = SD.open("ssid.txt");


C:\Users\Rudy\Documents\Arduino\esp8266\SDcard\SD-SPIFFS-init\SD-SPIFFS-init.ino:135:3: note: suggested alternative:

In file included from C:\Users\Rudy\Documents\Arduino\esp8266\SDcard\SD-SPIFFS-init\SD-SPIFFS-init.ino:37:0:

C:\Users\Rudy\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/FS.h:48:7: note: 'fs::File'

class File : public Stream

^

SD-SPIFFS-init:135: error: expected ';' before 'myFile0'

File myFile0 = SD.open("ssid.txt");

Re: SSID & PASS from SDCard & Copy to SPIFFS

PostPosted: Sat Jan 14, 2017 12:22 pm
by GengusKahn
This will be due to the version of SD Library or Libraries included in the Board manager versions....

Follow the instructions to install the Latest versions using GIT...Instructions here...
https://github.com/esp8266/arduino#using-git-version

I am using 1.6.13 I have installed the ESP8266 FS Library as part of the GIT operation.
Version...

# SPIFFS (SPI Flash File System)
**V0.3.4**

/arduino-1.6.13/hardware/esp8266com/esp8266/cores/esp8266/spiffs


The SD is the EPS8266 provided library in the same supplied install (V1.05 based on Sparkfun....)

name=SD(esp8266)
version=1.0.5
author=Arduino, SparkFun

/arduino-1.6.13/hardware/esp8266com/esp8266/libraries/SD

There is nothing other than the removal of the Global value from SPIFFS using #define "FS_NO_GLOBALS ".

This is being used on an ESP -201, -12 & -12E.........

Remove any other SD Libraries, restart and try again.....