ESP32 - Flash Memory

So im currently trying to use the serial input to save the ssid of my network to the memory of my esp32 with the EEPROM.h library and then read the memory to output it on the serial monitor.

To write it to the memory i made an extra function:

void storeSSID()
{
  char SSID[33]; //char array to write it byte by byte
  int len = 0; //Length of String
  while(Serial.available()<=0){}
  Serial.println();
  String Message = Serial.readStringUntil('\n'); //reading the ssid from the serial monitor
  len = Message.length() - 1;
  Message.toCharArray(SSID, 33); //turning it into a char array
  Serial.println(Message);
  for(int i=0;i<=len;i++)
  {
    EEPROM.write(i,SSID[i]);
  }
  EEPROM.commit();
}
 //this part is in my setup() function
  char buf[8];
  for(int i=9;i>0;i--)
  {
    buf[i] == EEPROM.read(i);
    Serial.println(buf[i]);
    display.print(buf[i]);
  }

However when i read from the Memory it only outputs null terminators. It is being written reversed in the for loop because i had some weird problem where the loop wouldnt finish when ive done it in normal order.

Welcome to the forum

Shouldn't there be a call to EEPROM.begin() in there somewhere ?

Another thing, why not use the EEPROM get() and put() functions instead of your for loops ?

i have that in the setup function, did not want to post the whole code in here

Now you know why we ask for the full sketch to be posted. Please post it

#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <UrlEncode.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <EEPROM.h>

#define UP_BUTTON 10
#define DOWN_BUTTON 5
#define LEFT_BUTTON 7
#define RIGHT_BUTTON 9
#define ENTER_BUTTON 14

#define Screen_Width 128
#define Screen_Height 64
#define OLED_RESET -1

#define EEPROM_Size 1

Adafruit_SSD1306 display = Adafruit_SSD1306(Screen_Width, Screen_Height, &Wire, OLED_RESET);

const char* ssid = "";
const char* password = "";

String PhoneNumber = "";
String APIKey = "";

void sendMessage(String message)
{
  String url = "https://api.callmebot.com/whatsapp.php?phone=" + PhoneNumber + "&apikey=" + APIKey + "&text=" + urlEncode(message);
  HTTPClient http;
  http.begin(url);

  http.addHeader("Content-Type", "application/x-www-form-urlencoded");
  http.POST(url);
  http.end();
}

void storeSSID()
{
  char SSID[33];
  int len = 0;
  while(Serial.available()<=0){}
  Serial.println();
  String Message = Serial.readStringUntil('\n');
  len = Message.length() - 1;
  Message.toCharArray(SSID, 33);
  Serial.println(Message);
  Serial.println(len);
  for(int i=0;i<=len;i++)
  {
    EEPROM.put(i,SSID[i]);
  }
  EEPROM.commit();
}

void storePassword()
{
  char Password[33];
  int len = 0;
  while(Serial.available()<=0){}
  Serial.println();
  String Message = Serial.readStringUntil('\n');
  len = Message.length() - 1;
  Message.toCharArray(Password, 33);
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.setTimeout(15000);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3c);

  display.display();
  delay(1000);
  display.clearDisplay();
  display.display();

  EEPROM.begin(EEPROM_Size);

  WiFi.begin(ssid, password);
  while(WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  //sendMessage("Funkt");
  storeSSID();
  
  display.setCursor(0,0);
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  
  char buf[8];
  for(int i=9;i>0;i--)
  {
    buf[i] == EEPROM.read(i);
    Serial.println(buf[i]);
    display.print(buf[i]);
  }
  String mystring = buf;
  //display.print(mystring);
  display.println("nig");
  display.display();
  Serial.println("nig");
}

void loop() {
  // put your main code here, to run repeatedly:
}

#define EEPROM_Size 1

    EEPROM.begin(EEPROM_Size);

Another reason to post your whole sketch

You have set the EEPROM size to 1 byte. Why ?

it was also set to 1 byte in the guide from "Random Nerd Tutorials" and since i had to save every character in a seperate byte i thought i could leave it like that

Please post a link to the tutorial

From the first example in that tutorial

Then, you define the EEPROM size. This is the number of bytes you’ll want to access in the flash memory. In this case, we’ll just save the LED state, so the EEPROM size is set to 1.

They are only saving the LED state, hence 1 byte of EEPROM is good enough, but you need room for the whole SSID including its terminating '\0' character

As a side note, did you notice that EEPROM on the ESP32 is new deprecated in favour of the Preferences library ?

As a side note

1 Like

have a look at the ESP32 preferences library - the documentation states
It should be considered as the replacement for the Arduino EEPROM library.
Edit: missed that @UKHeliBob had already recommended preferences!!

1 Like

a third vote for preferences..
used it to store and load my ssid and pass..
ESP Cam Client..
easy to use, works well..

good luck.. ~q

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.