nvs-dotenv
is an ESP-IDF component which makes variables defined in a .env
file accessible using getenv
function:
- Create a
.env
file inside you project directory - Keep various local configs and secrets in
.env
— Wi-Fi credentials, API tokens, and so on. - In your ESP-IDF application, call
getenv
function to access these variables
With nvs-dotenv
, you won't have to:
- hardcode credentials in your application source code
- store the credentials in sdkconfig, and wait for the entire app to rebuild when you change them.
nvs-dotenv
is intended to be used mainly during the development phase. In a production device you will likely need a provisioning workflow of some sort.
- In an existing ESP-IDF project, run the following command to install the component:
idf.py add-dependency "igrr/nvs-dotenv"
- Edit your partition table, adding a new partition:
# Name, Type, SubType, Offset, Size, Flags dotenv, data, nvs, , 12k,
- Create a
.env
file in your project directory:WIFI_SSID=yyyyyy WIFI_PASS=xxxxxx
- In the application, initialize
nvs-dotenv
:ESP_ERROR_CHECK(nvs_dotenv_load());
- Now you can access variables from
.env
usinggetenv
:const char *ssid = getenv("WIFI_SSID");
This component includes an example illustrating the above steps. You can try the example as follows:
idf.py create-project-from-example "igrr/nvs-dotenv:nvs-dotenv-example"
Then build as usual:
cd nvs-dotenv-example
idf.py build
And flash it to the board:
idf.py -p PORT flash monitor
The output should be:
I (3263) example: Loading environment variables
I (6033) example: WIFI_SSID: yyyyyy
I (6053) example: WIFI_PASS: xxxxxx
This component is provided under Apache 2.0 license, see LICENSE file for details.