ESP32 , EEprom

Hi,

Why does my H letter from ’ Hello ’ is not retrieved?

Here is the output:

7
7: H
8: e
9: l
10: l
11: o
12:  
13: o
14: n
15: c
16: e
17:  
18: i
19: n
20:  
21: a
22:  
23: w
24: h
25: i
26: l
27: e
28: .
29: 
------------   WHERE IS MY  H ?  ---------------

⸮ello once in a while.

----------------
72
101
108
108
111

----------------
⸮
⸮
⸮
e

Here is the code:

[code]
/*
   Testing EEPROMClas
   ESP32 Flash memory in a multiple user-defined EEPROM class objects.
   Created for arduino-esp32 on 25 Dec, 2017
   by Elochukwu Ifediora (fedy0)
   converted to nvs by lbernstone - 06/22/2019
   https://rntlab.com/question/eeprom-update-on-esp32/#answer-70346
   https://github.com/espressif/arduino-esp32/tree/master/libraries/Preferences
  putString(const char* key, const String value)
*/
#include "EEPROM.h"
#include "Preferences.h"

#define uS_TO_S_FACTOR 1000000ULL
RTC_DATA_ATTR uint32_t age = 0;
RTC_DATA_ATTR uint32_t check2 = 0;

EEPROMClass NAMES("eeprom0", 0x500);
EEPROMClass HEIGHT("eeprom1", 0x200);
EEPROMClass AGE("eeprom2", 0x100);


void setup() {
  Serial.begin(115200);
  AGE.get(0, age);
  printf("\neeprom age = %d\n", age);

  if (age != 47) {

    // Instantiate eeprom objects with parameter/argument names and sizes

    if (!NAMES.begin(NAMES.length())) {
      Serial.println("Failed to initialise NAMES");
      Serial.println("Restarting...");
      ESP.restart();
    }
    if (!HEIGHT.begin(HEIGHT.length())) {
      Serial.println("Failed to initialise HEIGHT");
      Serial.println("Restarting...");
      delay(1000);
      ESP.restart();
    }
    if (!AGE.begin(AGE.length())) {
      Serial.println("Failed to initialise AGE");
      Serial.println("Restarting...");
      delay(1000);
      ESP.restart();
    }
    AGE.put(0, age);
  }


  Serial.println(age);
  Serial.println(check2);


  if (age != 47) {
    const char* name = "2020-09-16 | 17:04:28  Humidité du sol: 114.00%  Température du sol:  14.87°C  Température: 26.40°C  Humidité: 65.70%  Ressentie: 27.66°C  Rosé 19.48°C";
    double height = 5.8;
    uint32_t age = 47;
    check2 += 1;
    Serial.print("In check2:  "); Serial.println(check2);
    Serial.println("----------------- WRITE -------------------\n");
    NAMES.put(0, name);
    HEIGHT.put(0, height);
    AGE.put(0, 47);

    // Write: Variables ---> EEPROM stores
    Serial.print("name: "); Serial.println(name);
    Serial.print("height: "); Serial.println(height);
    Serial.print("age: "); Serial.println(age);

    Serial.println("----------------- CLEAR -------------------\n");
    // Clear variables
    Serial.printf("name: %s", name);
    name = '\0';
    Serial.println(name);
    height = 0;
    age = 0;
    Serial.print("name: "); Serial.println(name);
    Serial.print("height: "); Serial.println(height);
    Serial.print("age: "); Serial.println(age);

    Serial.println("----------------- READ -------------------\n");
    // Read: Variables <--- EEPROM stores

    NAMES.get(0, name);
    HEIGHT.get(0, height);
    AGE.get(0, age);
    Serial.print("name: "); Serial.println( name);
    Serial.print("height: "); Serial.println(height);
    Serial.print("age: "); Serial.println(age);
    uint32_t age3 = 47;
    //AGE.put(0, age3);
    AGE.put(0, 47);
    Serial.println("-----------------  Going to sleep.");
    delay(1000);
    esp_sleep_enable_timer_wakeup(30 * uS_TO_S_FACTOR);
    esp_deep_sleep_start();
    //ESP.restart();
  }
  if (age == 47) {
    Serial.println("In else");
    const char rname[24] = {};
    NAMES.get(0, rname[0]);
    double height2;
    HEIGHT.get(0, height2);
    uint32_t age2;
    AGE.get(0, age2);

    Serial.print("name: "); Serial.println( rname);
    Serial.print("height: "); Serial.println(height2);
    Serial.print("age2: "); Serial.println(age2);
  }

  Serial.println("\n-----------------   Done!");
}
void loop() {
  delay(0xFFFFFFFF);
}

[/code]

Regards

JPDaviau

Where exactly in the code you posted does it send "hello once in a while." to the Serial Monitor? Are you sure you posted the correct code?

You can always put a small delay after Serial.begin() to make sure the UART is ready.

blh64:
Where exactly in the code you posted does it send “hello once in a while.” to the Serial Monitor? Are you sure you posted the correct code?

Yes and no.

I am sorry for this. Here is the correct code.

#include

#define EEPROM_SIZE 40

void writeChar(uint8_t ad, const char *ch)
{
printf("%d\n", ad);
printf("%d: %c\n", ad++, *ch);
EEPROM.write(ad, *ch);
EEPROM.commit();
while (*ch++ != ‘\0’) {
EEPROM.write(ad, *ch);
EEPROM.commit();
printf("%d: %c\n", ad++, *ch);
}
}

void readChar(uint8_t ad)
{
uint8_t ch = ‘\0’;

ch = EEPROM.read(ad++);
//Serial.printf("%c", ch);
while (ch != ‘\0’) {
Serial.printf("%c", ch);
ch = EEPROM.read(ad++);//ch address is always the same
}
}

void setup() {//strlen(value);
Serial.begin (115200);
EEPROM.begin(EEPROM_SIZE);
const char *hel = “Hello once in a while.”;
writeChar(7, hel);
Serial.println("\n----------------------------");
readChar(7);

Serial.println("\n----------------");
Serial.println(EEPROM.read(0));
Serial.println(EEPROM.read(1));
Serial.println(EEPROM.read(2));
Serial.println(EEPROM.read(3));
Serial.println(EEPROM.read(4));
Serial.println("\n----------------");
Serial.println((char)EEPROM.read(5));
Serial.println((char)EEPROM.read(6));
Serial.println((char)EEPROM.read(7));
Serial.println((char)EEPROM.read(8));

//EEPROM.write(0,‘H’);//Reset memory value
//EEPROM.write(1,‘e’);
//EEPROM.write(2,‘l’);
//EEPROM.write(3,‘l’);
//EEPROM.write(4,‘o’);
//EEPROM.commit();
}
void loop() {

}

void writeChar(uint8_t ad, const char *ch)
{
  printf("%d\n", ad);
  printf("%d: %c\n", ad++, *ch);
  EEPROM.write(ad, *ch);
  EEPROM.commit();
  ad++; //increment before next character
  while (*ch++ != '\0') {
    EEPROM.write(ad, *ch);
    EEPROM.commit();
    printf("%d: %c\n", ad++, *ch);
  }
}

You need to increment ad before you store the second character. You are overwriting the initial address.

With 59 posts, you should know how to get your code within code tags.

Hi,

Sorry about that.

Thanks.

Regards

JPDaviau

#include <EEPROM.h>

#define EEPROM_SIZE 55

void writeChar(int16_t ad, const char *ch)
{
  EEPROM.write(ad++, *ch);
  EEPROM.commit();
  printf("%d: %c\n", ad, *ch);
  while (*ch++ != '\0') {
    printf("%d: %c\n", ad, *ch);
    EEPROM.write(ad++, *ch);
    EEPROM.commit();
    
  }
}

void readChar(int16_t ad)
{
  uint8_t ch = '\0';

  ch = EEPROM.read(ad++);
  while (ch != '\0') {
    Serial.printf("%c", ch);
    ch = EEPROM.read(ad++);//ch address is always the same
  }
}

void setup() {
  Serial.begin (115200);
  EEPROM.begin(EEPROM_SIZE);
  //an issue with french characters
  //const char* hel = "2020-09-16 | 17:04:28  Humidité du sol: 114.00%  Température du sol:  14.87°C  Température: 26.40°C  Humidité: 65.70%  Ressentie: 27.66°C  Rosé 19.48°C";
  const char *hel = "Hello once in a while.";
  //int16_t lnth = strlen(hel);
  uint8_t from = 6;
  writeChar(from, hel);
  Serial.println("\n----------------------------");
  readChar(from);

  Serial.println("\n----------------");
  Serial.println((char)EEPROM.read(6));
  Serial.println((char)EEPROM.read(7));
  Serial.println((char)EEPROM.read(8));
}
void loop() {

}

Also, you don't need to do EEPROM.commit() after each individual char write. Just do it once at the end of the function.