README.md
1# Non-Volatile Storage (NVS) Read and Write Example
2
3(See the README.md file in the upper level 'examples' directory for more information about examples.)
4
5This example demonstrates how to read and write a single integer value and a blob (binary large object) using NVS to preserve them between ESP32 module restarts.
6
7 * value - tracks number of ESP32 module soft and hard restarts.
8 * blob - contains a table with module run times. The table is read from NVS to dynamically allocated RAM. New run time is added to the table on each manually triggered soft restart and written back to NVS. Triggering is done by pulling down GPIO0.
9
10Example also shows how to implement diagnostics if read / write operation was successful.
11
12Detailed functional description of NVS and API is provided in [documentation](https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/storage/nvs_flash.html).
13
14If not done already, consider checking simpler example *storage/nvs_rw_value*, that has been used as a starting point for preparing this one.
15
16## How to use example
17
18### Hardware required
19
20This example can be run on most common development boards which have an active button connected to GPIO0. On most boards, this button is labeled as "Boot". When pressed, the button connects GPIO0 to ground.
21
22### Build and flash
23
24Build the project and flash it to the board, then run monitor tool to view serial output:
25
26```
27idf.py -p PORT flash monitor
28```
29
30(To exit the serial monitor, type ``Ctrl-]``.)
31
32See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
33
34## Example Output
35
36First run:
37```
38Restart counter = 0
39Run time:
40Nothing saved yet!
41```
42
43At this point, press "Boot" button and hold it for a second. The board will perform software restart, printing:
44
45```
46Restarting...
47```
48
49After booting again, restart counter and run time array will be printed:
50
51```
52Restart counter = 1
53Run time:
541: 5110
55```
56
57After pressing "Boot" once more:
58
59```
60Restart counter = 2
61Run time:
621: 5110
632: 5860
64```
65
66To reset the counter and run time array, erase the contents of flash memory using `idf.py erase_flash`, then upload the program again as described above.
67
68