igrr/nvs-dotenv

1.0.0

uploaded 7 months ago
nvs-doteenv makes variables defined in a .env file accessible using getenv function

readme

# nvs-dotenv

[![Component Registry](https://components.espressif.com/components/igrr/nvs-dotenv/badge.svg)](https://components.espressif.com/components/igrr/nvs-dotenv)

`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.

## Using the component

1. In an existing ESP-IDF project, run the following command to install the component:
   ```bash
   idf.py add-dependency "igrr/nvs-dotenv"
   ```
2. Edit your partition table, adding a new partition:
   ```csv
   # Name,     Type,   SubType, Offset,  Size, Flags
   dotenv,     data,   nvs,           ,  12k,
   ```
3. Create a `.env` file in your project directory:
   ```
   WIFI_SSID=yyyyyy
   WIFI_PASS=xxxxxx
   ```
4. In the application, initialize `nvs-dotenv`:
   ```c++
   ESP_ERROR_CHECK(nvs_dotenv_load());
   ```
5. Now you can access variables from `.env` using `getenv`:
   ```c++
   const char *ssid = getenv("WIFI_SSID");
   ```


## Example

This component includes an example illustrating the above steps. You can try the example as follows:

```bash
idf.py create-project-from-example "igrr/nvs-dotenv:nvs-dotenv-example"
```

Then build as usual:
```bash
cd nvs-dotenv-example
idf.py build
```

And flash it to the board:
```bash
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
```

## License

This component is provided under Apache 2.0 license, see [LICENSE](LICENSE.md) file for details.

api

# API Reference

## Header files

- [nvs_dotenv.h](#file-nvs_dotenvh)

## File nvs_dotenv.h


## Functions

| Type | Name |
| ---: | :--- |
|  esp\_err\_t | [**nvs\_dotenv\_load**](#function-nvs_dotenv_load) (void) <br>_Load environment variables from NVS._ |



## Functions Documentation

### function `nvs_dotenv_load`

_Load environment variables from NVS._
```c
esp_err_t nvs_dotenv_load (
    void
) 
```

**Returns:**

ESP\_OK on success


Links

Supports all targets

License: Apache-2.0

To add this component to your project, run:

idf.py add-dependency "igrr/nvs-dotenv^1.0.0"

or download archive

Dependencies

  • ESP-IDF >=4.4.0
  • Examples:

    nvs-dotenv-example

    To create a project from this example, run:

    idf.py create-project-from-example "igrr/nvs-dotenv^1.0.0:nvs-dotenv-example"

    or download archive

    Stats

    • Downloaded in total
      Downloaded in total 28 times
    • Downloaded this version
      This version: 28 times

    Badge

    igrr/nvs-dotenv version: 1.0.0
    |