Chat here about code rewrites, mods, etc... with respect to the github project https://github.com/esp8266/Arduino

Moderator: igrr

User avatar
By Chris--A
#17054
biobier wrote:Hi,

any idea why this does not work with this lib?

Code: Select all  int i=0;
  int data=0;
  while(i<512){
   i+= EEPROM.put(i, data);
   Serial.println(i);
  }


Not sure about the resets, however the code will work incorrectly even if not resetting.

EEPROM.put() returns the 2nd param 'data'. Seeing as its zero, your 'i+=...' does nothing and you are 'putting' the data in the same location. This also means your loop never ends, and could be causing Serial to never print, eventually blowing its memory.
User avatar
By biobier
#22548 For my next Project I gave this a new try and the code posted by gerardwr works good, Thanks! :)

@draco: Should I commit this to the GitHub repo for you? I think it would be great to make it more available to the community by this.

With the latest ESP8266 Arduino IDE the the two files need to be placed here (I recommend to rename the origin files instead replacing them):
C:\Users\USERNAME\AppData\Roaming\Arduino15\packages\esp8266\hardware\esp8266\1.6.4-673-g8cd3697\libraries\EEPROM

That is my test sketch in case some one want's to give this a try:
Code: Select all#include <EEPROM.h>

extern "C" {
  #include "user_interface.h" //Needed for the reset command
}

  char* charOut = "bla/bla_blub";
  int intOut = 1;
  String stringOut = "Test";


void setup() {
  Serial.begin(115200); 
 
  EEPROM.begin(512);
 
  int addr=0;
  addr += EEPROM.put(addr, charOut); 
  yield();
  Serial.println(addr);
  addr += EEPROM.put(addr, intOut);
  yield();
  Serial.println(addr);
  addr += EEPROM.put(addr, stringOut);
  yield();
  Serial.println(addr);
 
  EEPROM.commit();
 
  delay(5000);
 
  String ssidIN;
  String pwdIN;
  int intIN;
  char* charIN;
  String stringIN;
  addr=0;
  addr += EEPROM.get(addr, charIN);
  addr += EEPROM.get(addr, intIN);
  addr += EEPROM.get(addr, stringIN);
  EEPROM.commit();
  Serial.println("char");
  Serial.println(charIN);
  Serial.println("int");
  Serial.println(intIN);
  Serial.println("string");
  Serial.println(stringIN);
  delay(5000);

}


void loop() {
  delay(100);
  Serial.println("Restarting!");
  system_restart();
  delay(200); 
}
User avatar
By eysaku
#37574 Hi, I have some issues with the esp and arduino I use the code below just for test and write in the eeprom all fine when tested in arduino uno, but I upload the code in the esp and have problems any one can help me with these thank you.

#include <EEPROM.h>
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
char password[16];
char password2[16];
char password3[16];
int i;


void setup() {
EEPROM.get(i,password);
// }
// initialize serial:
Serial.begin(9600);
// reserve 200 bytes for the inputString:
inputString.reserve(200);
Serial.println(password);

}


void loop() {
serialEvent(); //call the function
// print the string when a newline arrives:
if (stringComplete) {
// Serial.println(inputString);
inputString.toCharArray(password2, 16);
for (int e=0; e<16; e++)
{
i= password2[e];
password3[e] = password2[e];

// EEPROM.write(password[e], password[e]);
// Serial.println(i, DEC);
// Serial.println(i );

}
Serial.println(password3 );
Serial.println(password[2] );
EEPROM.put(i,password3);
// clear the string:
inputString = "";
stringComplete = false;
}

}


/*
SerialEvent occurs whenever a new data comes in the
hardware serial RX. This routine is run between each
time loop() runs, so using delay inside loop can delay
response. Multiple bytes of data may be available.
*/
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;

}
}
}