Hi All
I have a ESP32 and am trying to Save and Read the ssid and password to and from the Flash Memory
The Script saves the data and when it reads it back it looks ok
but it cant not connect to the wifi
if i add my ssid and password to line 41 & 42 then it works all ok
but if i read the data with the same text it dose not connect
anyone know why it is not working
Thanks
Steve
/////////////////////////////////////////////////////
#include <SPI.h>
#include <Wire.h>
#include <WiFi.h>
#include <SPIFFS.h>
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
char* MyFilename = "/mta.txt"; // was /
String WiFissid = "";
String WiFipassword = "";
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 0 * 60 * 60; //3600;
const int daylightOffset_sec = 3600;
unsigned long PreviousTime = 0;
#define TRIGGER_PIN 0
WiFiManager wm;
// variables will change:
int buttonState = 0;
int timeout = 120; // seconds to run for
void setup() {
delay(5000);
WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP
Serial.begin(115200);
Serial.println("Starting Setup");
pinMode(TRIGGER_PIN, INPUT_PULLUP);
Serial.println("Reading Flash Memory");
readFile(MyFilename);
Serial.println(WiFissid);
Serial.println(WiFipassword);
delay(2000);
//String WiFissid = "my ssid";
//String WiFipassword = "my password";
WiFi.begin(WiFissid.c_str(), WiFipassword.c_str());
delay(1000);
unsigned long startTime = millis();
while (WiFi.status() != WL_CONNECTED) {
Serial.println("Trying to connect to WiFi");
if (millis() - startTime > 2000) {
Serial.println("Could not connect to WiFi");
break;
}
delay(1000);
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("Connected to WiFi");
}
}
void loop() {
if (digitalRead(TRIGGER_PIN) == LOW) {
RunPortal();
ESP.restart();
}
} // end of loop
//////////////////////////////////////////
//////////////////////////////////////////
// Save Data
//////////////////////////////////////////
//////////////////////////////////////////
void Save_File(String FileName) {
SPIFFS.begin();
File file = SPIFFS.open("/mta.txt", FILE_WRITE);
if (!file) {
Serial.println("Failed to open file for writing");
return;
}
Serial.println("Saving Data");
file.println(WiFissid);
file.println(WiFipassword);
file.close();
Serial.println("File Saved");
Serial.println("");
}
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
// Reading the File
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
void readFile(String FileName) {
Serial.print("Reading File");
Serial.println("");
if (!SPIFFS.begin(true)) {
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
File file = SPIFFS.open(MyFilename, "r");
if (!file) {
Serial.println("An Error has occurred while opening the file for reading");
//return;
}
WiFissid = file.readStringUntil('\n');
WiFipassword = file.readStringUntil('\n');
file.close();
}
////////////////////////////////////////////////
////////////////////////////////////////////////
// Run Portal
////////////////////////////////////////////////
////////////////////////////////////////////////
void RunPortal() {
Serial.println("Running Portal");
// is configuration portal requested?
WiFiManager wifiManager;
//reset settings - for testing
wifiManager.resetSettings();
wifiManager.setTimeout(120);
WiFi.begin(WiFissid.c_str(), WiFipassword.c_str());
if (!wifiManager.startConfigPortal("Display_AP", "1234567890")) {
Serial.println("failed to connect and hit timeout");
delay(2000);
//reset and try again, or maybe put it to deep sleep
ESP.restart();
delay(2000);
}
// Connected!
Serial.println("WiFi connected");
delay(1000);
String WiFissid(WiFi.SSID());
String WiFipassword(WiFi.psk());
Save_File(MyFilename);
Serial.println("Portal Ended.");
delay(1000);
}
Can not connect to Wifi from Saved data from Flash Memory
-
- Posts: 1
- Joined: Mon Jan 30, 2023 10:58 am
Re: Can not connect to Wifi from Saved data from Flash Memory
You are writing the file using println, which in Arduino-land will usually append \r\n (https://reference.arduino.cc/reference/ ... l/println/).
When you read it back with readStringUntil('\n'), the resulting string will then include the '\r'.
When you read it back with readStringUntil('\n'), the resulting string will then include the '\r'.
Who is online
Users browsing this forum: No registered users and 140 guests