Saving and writing to eeprom

Duhjoker
Posts: 85
Joined: Mon Mar 20, 2017 8:09 am

Re: Saving and writing to eeprom

Postby Duhjoker » Mon Feb 12, 2018 5:52 am

None taken. I'm still learning to code. I understand some of it. Ive been reading tutorials and the Arduino playground page. But esp32 is a lil different. Is this where I need to know the bits for each variable in the slot structure?

or do I need to define two addresses somehow
Fear is the mind killer.......

GameR the DIY iot gaming device that does more......
https://github.com/Duhjoker/GameR-Iot_ESP

tele_player
Posts: 90
Joined: Sun Jul 02, 2017 3:38 am

Re: Saving and writing to eeprom

Postby tele_player » Mon Feb 12, 2018 6:09 am

Hint:

EEPROM.put(0, player);
EEPROM.put(sizeof(player), slots);

And something similar for get().

Duhjoker
Posts: 85
Joined: Mon Mar 20, 2017 8:09 am

Re: Saving and writing to eeprom

Postby Duhjoker » Mon Feb 12, 2018 6:20 am

ok so if Player uses 9 variables in its structure, all use int, that would be 36 for the size of player.

So something like this...

Code: Select all

void save()
{
  EEPROM.put(0, player);
  EEPROM.put(36, slots);
  EEPROM.commit();
  Serial.print("saved");
}

void load()
{
    EEPROM.get(0, player);
    EEPROM.get(36, slots);
 }

Fear is the mind killer.......

GameR the DIY iot gaming device that does more......
https://github.com/Duhjoker/GameR-Iot_ESP

tele_player
Posts: 90
Joined: Sun Jul 02, 2017 3:38 am

Re: Saving and writing to eeprom

Postby tele_player » Mon Feb 12, 2018 6:55 am

Yes, but it is bad practice to use a constant like 36. Better to use sizeof(player), let the compiler do the work.

And if it was me, I wouldn’t use EEPROM for data which will be written frequently. The esp32 EEPROM is a fixed region of the flash, and has a finite number of writes before it goes bad. I’d use SPIFFS, which presumably implements a wear-leveling technique.

The EEPROM library is more appropriate for seldom changed configuration data.

Duhjoker
Posts: 85
Joined: Mon Mar 20, 2017 8:09 am

Re: Saving and writing to eeprom

Postby Duhjoker » Mon Feb 12, 2018 7:00 am

It didn't work. Could you link me to the correct spiffs library?
Fear is the mind killer.......

GameR the DIY iot gaming device that does more......
https://github.com/Duhjoker/GameR-Iot_ESP

tele_player
Posts: 90
Joined: Sun Jul 02, 2017 3:38 am

Re: Saving and writing to eeprom

Postby tele_player » Mon Feb 12, 2018 7:06 am

Also worth mentioning, in the real world, data saved like this would usually have additional info saved indicating the version of the data. As your program evolves, it’s not unlikely that the data structures will change, and a previous ‘save’ will be incorrectly read by the new version.

As is, you’ll need to be aware of this if your data changes, and manually take steps to clear the incompatible save from EEPROM.

Duhjoker
Posts: 85
Joined: Mon Mar 20, 2017 8:09 am

Re: Saving and writing to eeprom

Postby Duhjoker » Mon Feb 12, 2018 7:13 am

I'm all for learning something new.... looking at the SPIFFS library and its example but not really understanding it. Theres no notes on the functions.

Why does eeprom only have so many writes?

Where is the data actually being saved in the spiffs example?
Last edited by Duhjoker on Mon Feb 12, 2018 7:17 am, edited 1 time in total.
Fear is the mind killer.......

GameR the DIY iot gaming device that does more......
https://github.com/Duhjoker/GameR-Iot_ESP

tele_player
Posts: 90
Joined: Sun Jul 02, 2017 3:38 am

Re: Saving and writing to eeprom

Postby tele_player » Mon Feb 12, 2018 7:16 am

SPIFFS should already be installed in you esp32 Arduino installation.

#include <SPIFFS.h>

Note: using SPIFFS involves learning how to use files. It’s much more involved than EEPROM. The are probably examples that can help, but there’s a lot to learn.

Flash memory wears out from writes. Google it. SPIFFS writes to flash, but wear-leveling moves the data around each time it’s written.

Duhjoker
Posts: 85
Joined: Mon Mar 20, 2017 8:09 am

Re: Saving and writing to eeprom

Postby Duhjoker » Mon Feb 12, 2018 8:02 am

Sounds like either way is going to burn out something. I honestly thought the esp32's eeprom partition was from flash.

So what would be the best way to store Data for my player and inventory slots?
Fear is the mind killer.......

GameR the DIY iot gaming device that does more......
https://github.com/Duhjoker/GameR-Iot_ESP

Duhjoker
Posts: 85
Joined: Mon Mar 20, 2017 8:09 am

Re: Saving and writing to eeprom

Postby Duhjoker » Mon Feb 12, 2018 8:31 am

why not save to SD?
Fear is the mind killer.......

GameR the DIY iot gaming device that does more......
https://github.com/Duhjoker/GameR-Iot_ESP

Who is online

Users browsing this forum: No registered users and 155 guests