In the previous two posts of this tutorial, I explained how to perform an over-the-air update of the firmware running on the esp32 chip.
Sometimes you may need to revert to the factory firmware, that is the firmware stored in the flash memory when the chip was programmed. Many consumer devices have a button or a pin that, if you press it for some seconds, triggers a reset function:
In this post I’ll show you how to add this functionality to your project.
As explained in a previous post, the flash memory connected to the esp32 chip is divided into some partitions, based on a layout configured when you program the chip.
Partitions that can store firmwares are of the app type. The partition that contains the firmware programmed via USB, has the factory subtype.
The esp-idf framework includes a method to search partitions in the flash memory:
In the following video you can see how to perform a factory reset. In the video you can also learn how to “count” the number of seconds a button is pressed to trigger the reset function only after a fixed threshold (3 seconds in my example). Enjoy!
In today’s tutorial you’ll learn how to permanently store information, so that they are preserved even if the esp32 chip is reset or power is removed.
NVS (Non Volatile Storage) is a library included in the esp-idf framework that allows to store information (in the form of key/value) in the flash memory, the content of which is not erased when you reset the chip or remove the power.
If you remember, in a previous post I explained how the external flash memory is organized. The main purpose of that memory is for sure to store the program that will be executed by the esp32 chip. However, it’s possible to divide the flash memory in partitions: the framework offers some ready-to-use partition tables but you’re free to define custom ones.
If you use the default partition table (“Single factory app, no OTA”), you may notice that it contains a partition of type data and subtype nvs:
The default size for that partition is 24Kbyte.
Thanks to the NVS library, you can store custom data into that partition. Information is organized in key/value pairs; a label (= key) with a maximum length of 15 characters is assigned to each value:
You can store different types of data: from numeric values to text strings and byte sequences (blob, binary large object). You’ll learn that the library provides specific methods based on the type of data you want to store or retrieve.
To be able to use the library in your code, first include the following headers:
The first step to use the nvs partition is to initialize the library, with the command:
esp_err_t err = nvs_flash_init();
The command returns ESP_OK if successful; on the contrary it returns one of the error codes defined in the nvs.h file (see below). In particular, if the partition was resized or changed, you may get the ESP_ERR_NVS_NO_FREE_PAGES error code. This error can be resolved “formatting” the partition.
First you have to identify the nvs partition in the flash memory:
Because of you cannot know a priori the size for string or blob values, you can do a “trick”: first call the nvs_get_string() method passing NULL as pointer to get the length of the value, then allocate a variable with the correct size and finally call again the method passing that variable to get the value:
For this tutorial I wrote a program that allows, using a simple command line, to store and retrieve information from the nvs partition. The source code is available on Github, here’s a video that shows how it works: