Hello,
i have been looking for an answer for a while to the following problem:
First the example code:
#include <EEPROM.h>
#include <ESP8266WiFi.h>
struct WiFiConf{
String ssid;
String password;
};
WiFiConf wifi = {
"test",
"test1"
};
WiFiConf wificonf = {
"",
""
};
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Start");
writeConfig(wifi);
delay(1000);
wificonf = getConfig();
}
void loop() {
// put your main code here, to run repeatedly:
//EMPTY LOOP
}
void writeConfig(WiFiConf wifi){
EEPROM.put(0, wifi);
EEPROM.commit();
}
WiFiConf getConfig(){
WiFiConf wifi = {
"",
""
};
EEPROM.get(0, wifi);
delay(1000);
Serial.println();
Serial.print("SSID size: ");
Serial.print(wifi.ssid.length());
Serial.print(" || SSID: ");
Serial.println(wifi.ssid);
Serial.print("Password size: ");
Serial.print(wifi.password.length());
Serial.print(" || Password: ");
Serial.println(wifi.password);
return wifi;
}
I am creating a struct object with two Strings. Creating two objects of that struct. One is going to be written to the EEPROM while the other is trying to read data from the EEPROOM. Writing the object with EEPROM.put(), wait for a second and then trying to read the data with EEPROM.get();
As I am using the ESP8266 I have to use EEPROM.commit() after writing the data.
My Problem: Both strings are empty when I try to print them, also the size is 0. I check that right after reading the data.
BTW: EEPROM.begin(size) is always ending in an error while runtime. Something with Fatal exception. But I kind of dont need it:
One weird thing to add here, when I change the code like this (use the ssid and password from EEPROM.get()), it is connecting to the network. But the serial.print still show empty strings. I dont get what is wrong with all of that. Am I missing something really hard here??
#include <EEPROM.h>
#include <ESP8266WiFi.h>
struct WiFiConf{
String ssid;
String password;
};
WiFiConf wifi = {
"Sibby Wlan",
"********"
};
WiFiConf wificonf = {
"",
""
};
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Start");
writeConfig(wifi);
delay(1000);
wificonf = getConfig();
char ssidChar[64];
char passwordChar[64];
wificonf.ssid.toCharArray(ssidChar, sizeof(ssidChar)/sizeof(char));
wificonf.password.toCharArray(passwordChar, sizeof(passwordChar)/sizeof(char));
WiFi.mode(WIFI_STA);
WiFi.begin(ssidChar, passwordChar);
// Connect to Wi-Fi network with SSID and password
Serial.println();
Serial.print("Connecting to ");
Serial.print(ssidChar);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected!");
}
void loop() {
// put your main code here, to run repeatedly:
//EMPTY LOOP
}
void writeConfig(WiFiConf wifi){
EEPROM.put(0, wifi);
EEPROM.commit();
}
WiFiConf getConfig(){
WiFiConf wifi = {
"",
""
};
EEPROM.get(0, wifi);
delay(1000);
Serial.println();
Serial.print("SSID size: ");
Serial.print(wifi.ssid.length());
Serial.print(" || SSID: ");
Serial.println(wifi.ssid);
Serial.print("Password size: ");
Serial.print(wifi.password.length());
Serial.print(" || Password: ");
Serial.println(wifi.password);
return wifi;
}
Here is the output of the serial monitor:
Start
SSID size: 0 || SSID:
Password size: 0 || Password:
Connecting to ...Connected!
I feel like I am missing something or just stupid. Google is not helping with that kind of problem. I mean the string is there, but I cant see it :o