Search code examples
arduinoesp8266eeprom

EEPROM replacing first char in string with a comma


I have an app that I am writing three strings to EEPROM. The strings are from when the user first starts the app. They are from "WiFiManager".

code

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());

So this is when the app starts up if it can't connect to the internet it opens up a local access point "FloWT3" and the user fills in the local WiFi Name, Password, MQTT authorizationKey, Time Offset, and MQTT Username.

WiFi manager automatically stores the WiFi Name and Password and I store the other three in EEPROM with this code;

This is all in void setup()

Then in 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);

When I program the ESP8266-01 the first time so the user has to fill in the info in the "FloWT3" access point these are the reading I get on the Serial Monitor

this
11:35:13.576 -> aio_XXXXXXXXXXXXXXXXXXXXXXXXXXXthat
11:35:13.576 -> -07,⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮those
11:35:13.576 -> XXXXXXX,⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮

This is as expected and everything works fine.

Now when I run the app and it connects with the stored information I get these reading which are the same this 11:35:13.576 -> aio_XXXXXXXXXXXXXXXXXXXXXXXXXXXthat 11:35:13.576 -> -07,⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮those 11:35:13.576 -> XXXXXXX,⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮

I can now split out the parts I need using indexOf(",") and substrings. Everything is working very well.

I am thankful to hcheung for the suggestion and I will study up more on strcpy.


Solution

  • I found the reason for the replacement ,s. When the app open and ran it wrote a blank string to the beginning of the EEPROMs thus overriding the first character. So I moved the write part of the EEPROM into the if(WiFi.status() == WL_CONNECTED and also checked to see if the length of a String from the apiKey was equal to 0 or not. If it was 0 then the info was already stored in the EEPROM and didn't need to be written again. If it had a length then the app was being set up for the first time and writing to EEPROM needed to happen. I have changed my original post to reflect these changes.