我有一个应用程序,我正在写三个字符串到EEPROM。字符串来自于用户第一次启动应用程序时。它们来自于"WiFiManager"
代码WiFiManager wifiManager;
WiFiManagerParameter customAPIKey("authorizationKey", "Authorization Code", authorizationKey, 32);
WiFiManagerParameter customAPIKey2("apiKey", "Time Zone #", apiKey, 32);
WiFiManagerParameter customAPIKey3("userNameKey", "User Name",userNameKey, 32);
wifiManager.addParameter(&customAPIKey);
wifiManager.addParameter(&customAPIKey2);
wifiManager.addParameter(&customAPIKey3);
wifiManager.autoConnect("FloWT3");
Serial.println("Connected");
strcpy(authorizationKey, customAPIKey.getValue());
strcpy(apiKey, customAPIKey2.getValue());
strcpy(userNameKey, customAPIKey3.getValue());
当应用程序启动时如果它不能连接到互联网它会打开一个本地接入点"FloWT3"用户填写本地WiFi名称、密码、MQTT authorizationKey、时间偏移和MQTT用户名。
WiFi管理器自动存储WiFi名称和密码,我将其他三个存储在EEPROM与此代码;
这些都在void setup()
然后在void loop()WiFiManager wifiManager;
//if the wifi is not connected I open "FloWT3" access point again.
if (WiFi.status() == WL_DISCONNECTED) {
wifiManager.autoConnect("FloWT3");
delay(60000);}
//but if it is connected I access EEPROM and read what has been read.
else if (WiFi.status() == WL_CONNECTED) { Serial.println("Connected");
delay(2000);
WiFiClient client;
EEPROM.begin(512); //Initialize EEPROM
//This is where I moved the code to and added if statement.
String rrr = apiKey;
if (rrr.length()==0){
Serial.println("empty"):
}
else {
Serial,println("not empty");
//Write string to eeprom
String uuu = authorizationKey;
Serial.print("uuu");
Serial.print(uuu);
String www = apiKey;//Homenetwork + uuu;
Serial.print("www");
Serial.print(www);
String yyy = userNameKey;
String fff = String(www)+String(",");
String vvv = String(yyy)+String(",");
Serial.print("vvv");
Serial.print(vvv);
for(int i=0;i<uuu.length();i++) //loop upto string lenght www.length() returns length of string
{
EEPROM.write(0x0F+i,uuu[i]); //Write one by one with starting address of 0x0F
}
for(int i=0;i<fff.length();i++) //loop upto string lenght www.length() returns length of string
{
EEPROM.write(0x50+i,fff[i]); //Write one by one with starting address of 0x0F
}
for(int i=0;i<vvv.length();i++) //loop upto string lenght www.length() returns length of string
{
EEPROM.write(0x90+i,vvv[i]); //Write one by one with starting address of 0x0F
}
EEPROM.commit(); //Store data to EEPROM
}
delay (500);
String www;
//here I read the first part I wrote to address 0x0F which is the authorizationKey
for(int i=0;i<32;i++)
{
www = www + char(EEPROM.read(0x0F+i)); //Read one by one with starting address of 0x0F
}
//here is read from the address 0X50 which I wrote the apiKey (time offset)
String uuu;
for(int i=0;i<32;i++)
{uuu = uuu + char(EEPROM.read(0x50+i));
}
//here I read from address 0x090+i where I wrote the userNameKey
String vvv;
for(int i=0;i<32;i++)
{vvv = vvv + char(EEPROM.read(0x90+i));
}
Serial.println("this");
Serial.print(www);
Serial.println("that");
Serial.print(uuu);
Serial.println("those");
Serial.print(vvv);
当我第一次编程ESP8266-01时,用户必须在"FloWT3"中填写信息。接入点这些是我在串行监视器上得到的读数
this
11:35:13.576 -> aio_XXXXXXXXXXXXXXXXXXXXXXXXXXXthat
11:35:13.576 -> -07,⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮those
11:35:13.576 -> XXXXXXX,⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮
这是预期的,一切正常。
现在,当我运行应用程序,它连接到存储的信息,我得到这些读数是相同的这11:35:13.576→aio_XXXXXXXXXXXXXXXXXXXXXXXXXXXthat11:35:13.576→-07年,⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮11:35:13.576→XXXXXXX,⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮
我现在可以使用indexOf(",")和子字符串拆分出我需要的部分。一切都很顺利。
我很感谢张翔的建议,我会在策略方面多做研究。
我找到了更换的原因,s。当应用程序打开并运行时,它将一个空白字符串写入eeprom的开头,从而覆盖第一个字符。因此,我将EEPROM的写入部分移动到if(WiFi.status() == WL_CONNECTED)中,并检查了apiKey的字符串长度是否等于0。如果它是0,那么信息已经存储在EEPROM中,不需要再次写入。如果它有一个长度,那么应用程序是第一次设置和写入EEPROM需要发生。我已经修改了我的原始帖子以反映这些变化。