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

User avatar
By GengusKahn
#61911 Hi there, put your Secure Details in the SPIFFS or on an SDCARD....

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;
   }
  }
 }
}

// If needed set error detection on file contents by if(ssid==""){Do something}

  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:

}
User avatar
By jankop
#61920 Hi,
sketch without plain text? Hm... Yes! It is really very easy.
1. Upload your sketch to module with SSID and password

Code: Select allconst char* ssid =     "YourSSID";     // Set your router SSID
const char* password = "YourPassword"; // Set your router password
void setup() {
  // put your setup code here, to run once:
 WiFi.begin(ssid, password);
}
void loop() {
  // put your main code here, to run repeatedly:
}

2. Restart module. Test connection to router. Is it OK? Fain!
3. Upload your sketch without SSID, without password and command WiFi.begin() without parameters.
Code: Select allvoid setup() {
  // put your setup code here, to run once:
 WiFi.begin();
}
void loop() {
  // put your main code here, to run repeatedly:
}


It's simple and it works.
User avatar
By jeffas
#61972 That's very interesting, as I've been trying to solve the same problem myself.
But the WiFi library must store the password somewhere in order to use it next time. I looked into the source and found calls to wifi_station_set_config, which seems to save config (including the password) to flash memory. If that's so, then surely the problem is not actually solved.
What I have done is to use a very basic rotation cipher for storing the password, decrypting it for use and immediately wiping the decrypted form from memory. But if the WiFi library stores the password in flash anyway, I guess I'm wasting my time doing that.
Can anyone say whether my analysis is correct? I.e. is the WiFi library actually storing the password, and if so, is there any way to stop it doing so?