Editing
esp
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===EEPROM=== ;Reading and Writing Data Structures to EEPROM Using structures, reading and writing to the EEPROM is reduced to a single call.<br> (Reference from [https://playground.arduino.cc/Code/EEPROMWriteAnything/ playground.arduino.cc].<br> I used this in my Monkey Detector to record the servo positions and timing between boot sessions.<br> First, add a tab in the IDE named: '''eepromAnything.h''', then add this code:<br> <nowiki>// https://playground.arduino.cc/Code/EEPROMWriteAnything/ #include <EEPROM.h> #include <Arduino.h> // for type definitions template <class T> int EEPROM_writeAnything(int ee, const T& value) { const byte* p = (const byte*)(const void*)&value; unsigned int i; for (i = 0; i < sizeof(value); i++) EEPROM.write(ee++, *p++); return i; } template <class T> int EEPROM_readAnything(int ee, T& value) { byte* p = (byte*)(void*)&value; unsigned int i; for (i = 0; i < sizeof(value); i++) *p++ = EEPROM.read(ee++); return i; }</nowiki> <br><br> ;At the top of the sketch, add this: <nowiki>#include "eepromAnything.h" //structure declaration with three values as members. struct servo { int locked_Position ; int unlocked_Position ; unsigned long drawerTime; }; //create an unitialized struct variable called myServo. struct servo myServo; //Declare the individual variables for use later. //I could also use the struct? int locked_Position = myServo.locked_Position; int unlocked_Position = myServo.unlocked_Position; unsigned long drawerTime = myServo.drawerTime; </nowiki> <br><br> ;In setup(), I read the EEPROM just for verification: <nowiki> EEPROM_readAnything(0, myServo); locked_Position = myServo.locked_Position; unlocked_Position = myServo.unlocked_Position; drawerTime = myServo.drawerTime; Serial.print(F("lock-unlock-time= ")); Serial.print(locked_Position); Serial.print(F(" - ")); Serial.print(unlocked_Position); Serial.print(F(" - ")); Serial.println(drawerTime);</nowiki> <br><br> ;When I write data to the EEPROM: <nowiki>myServo.locked_Position = locked_Position; myServo.unlocked_Position = unlocked_Position; myServo.drawerTime = drawerTime; EEPROM_writeAnything(0, myServo);</nowiki> <br> {{template:top}} ;Old EEPROM Stuff [https://github.com/esp8266/Arduino/tree/master/libraries/EEPROM/examples EEPROM Library] for the ESP devices. <br> :This is a bit different from standard EEPROM class. :'''EEPROM.begin(size)''' ::You need to call EEPROM.begin(size) before you start reading or writing, size being the number of bytes you want to use. Size can be anywhere between 4 and 4096 bytes.<br> :'''EEPROM.write()''' :EEPROM.write does not write to flash immediately, instead you must call EEPROM.commit() whenever you wish to save changes to flash. EEPROM.end() will also commit, and will release the RAM copy of EEPROM contents. :'''EEPROM.commit()''' ::you must call EEPROM.commit() whenever you wish to save changes to flash. :'''EEPROM.end()''' ::EEPROM.end() will also commit, and will release the RAM copy of EEPROM contents. <br> :EEPROM library uses one sector of flash located just after the SPIFFS. <br><br> ;The ESP8266 handles EEPROM different from the Arduino.<br> Here is a sketch that I found on the Google machine a few months ago, edited for readability.<br> Sample code:<br> <nowiki>#include <EEPROM.h> void setup() { Serial.begin(115200); while (!Serial) {} Serial.println(); Serial.println("EEPROM_esp8266_example.ino"); /* Using the ESP8266 EEPROM is different from the standard Arduino EEPROM class. You need to call EEPROM.begin(size) before you can start reading or writing, where the size parameter is the number of bytes you want to use store Size can be anywhere between a minimum of 4 and maximum of 4096 bytes. */ EEPROM.begin(32); //EEPROM.begin(Size) /* Commands EEPROM.write or EEPROM.put do not write to flash immediately, to invoke them you must call EEPROM.commit() to save changes to flash/EEPROM. EEPROM.end() will also commit, but releases the RAM copy of EEPROM contents. */ // Commands to determine variable sizes, needed for storing to EEPROM Serial.println(); Serial.println(" Floating point variables need: " + String(sizeof(float)) + " Bytes"); // Determine how many bytes (4) are needed to save a floating point variable Serial.println("Double size floating point variables need: " + String(sizeof(double)) + " Bytes"); // Determine how many bytes (8) are needed to save a floating point variable Serial.println(" Integer variables need: " + String(sizeof(int)) + " Bytes"); // Determine how many bytes (4) are needed to save an integer variable Serial.println(" Boolean values or variables need: " + String(sizeof(bool)) + " Bytes"); // Determine how many bytes (1) are needed to save a boolean variable Serial.println(" String variables need at least: " + String(sizeof(String)) + " Bytes"); // Determine how many bytes (min. 12) are needed to save a string variable Serial.println(); //---------------------------------------------------------------------------- // Example-1 Write a value to EEPROM at address = 0 int EEaddress = 0; EEPROM.write(EEaddress, 123); // Writes the value 123 to EEPROM // 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 // Contents of EEPROM // 7B 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 // Contents of RAM EEPROM.commit(); // 7B 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 // Contents of EEPROM // Serial.print(F("Write a value: 123 to EEPROM at address = 0")); Serial.print("EEPROM contents at Address=0 is : "); Serial.println(EEPROM.read(EEaddress)); //---------------------------------------------------------------------------- // Example-2 Write a value to EEPROM at address = 0 // 7B 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 // Contents of EEPROM EEPROM.write(EEaddress, 257); // Writes the value 257 to EEPROM EEPROM.commit(); // 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 // Contents of EEPROM Serial.print("EEPROM contents at Address=0 is : "); Serial.println(EEPROM.read(EEaddress)); //---------------------------------------------------------------------------- // Example-3 Write an integer variable to EEPROM at address = 0 int integer_variable = 257; // 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 // Contents of EEPROM EEPROM.put(EEaddress, integer_variable); // Writes the value 257 to EEPROM EEPROM.commit(); // 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 // Contents of EEPROM Serial.print("EEPROM contents at Address=0 is : "); EEPROM.get(EEaddress, integer_variable); Serial.println(integer_variable); //---------------------------------------------------------------------------- // Example-4 Write another integer variable to EEPROM int integer_variable2 = 1234; EEaddress = EEaddress + sizeof(int); // Moves the address along by 4 EEPROM.put(EEaddress, integer_variable2); // Writes the value 1234 to EEPROM EEPROM.commit(); // 01 01 00 00 D2 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 // Contents of EEPROM Serial.print("EEPROM contents at Address=4 is : "); EEPROM.get(EEaddress, integer_variable2); Serial.println(integer_variable2); EEaddress = EEaddress + sizeof(int); // Moves the address along by 4 //---------------------------------------------------------------------------- // Example-5 Write a floating point variable to EEPROM float floatingpoint_variable = 3.141592654; EEPROM.put(EEaddress, floatingpoint_variable); // Writes the value 3.141592654 to EEPROM EEPROM.commit(); Serial.print("EEPROM contents at Address=8 is : "); EEPROM.get(EEaddress, floatingpoint_variable); Serial.println(floatingpoint_variable, 8); EEaddress = EEaddress + sizeof(float); // Moves the address along by 4 //---------------------------------------------------------------------------- // Example-6 Write a string variable to EEPROM String string_variable = "Hello world"; EEPROM.put(EEaddress, string_variable); // Writes the value 3.141592654 to EEPROM EEPROM.commit(); Serial.print("EEPROM contents at Address=12 is : "); EEPROM.get(EEaddress, string_variable); Serial.println(string_variable); EEaddress = EEaddress + sizeof(string_variable); // Moves the address along by 4 //---------------------------------------------------------------------------- // Example-7 Write a series of values to EEPROM for (int i = 1000; i <= 1032; i = i + 4) { EEPROM.put(i - 1000, i); // Address range 0-32 } EEPROM.commit(); for (int j = 1000; j <= 1032; j = j + 4) { EEPROM.get((j - 1000), integer_variable); // Read the 32 values Serial.println(integer_variable); } //---------------------------------------------------------------------------- // Example-8 Testing that the EEPROM extent has not been exceeded, remember not to exceed address space if (EEaddress == 32) { EEaddress = 0; } //---------------------------------------------------------------------------- // Example-9 Compact method of writing and reading values from EEPROM EEaddress = 20; // Writing floatingpoint_variable = 2 * PI; EEaddress += EEPROM.put(EEaddress, floatingpoint_variable); integer_variable = 123456789; EEaddress += EEPROM.put(EEaddress, integer_variable); EEPROM.end(); EEaddress = 20; // Reading EEaddress += EEPROM.get(EEaddress, floatingpoint_variable); EEaddress += EEPROM.get(EEaddress, integer_variable); EEPROM.commit(); Serial.println(floatingpoint_variable, 7); Serial.println(integer_variable); } void loop() {} </nowiki> <br> {{template:top}}
Summary:
Please note that all contributions to Notebook may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Notebook:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Home Page
C++ Reference
ESP Hardware
ESPHome
Home Assistant
Network Stuff
Tools
What links here
Related changes
Special pages
Page information