ESP32芯片上的环境变量?

huangapple go评论65阅读模式
英文:

Environment variables on ESP32 chip?

问题

有没有办法在构建我的ESP32芯片的镜像时设置环境变量,或类似环境变量的东西?

我知道我可以使用#define将参数嵌入我的程序中,但我想避免每次更改此参数的值时都重新构建整个程序。

英文:

Is there any way of setting an environment variable, or anything like an environment variable, when building an image for my ESP32 chip?

I know that I could bake my parameter into my program using a #define, but I would like to avoid rebuilding my whole program every time I change the value of this parameter.

答案1

得分: 3

环境变量对于ESP32来说并不是一个真正的概念,因为它是一个单一的“进程”环境,但你可以将配置单独存储在程序之外。

如何实现这一点取决于你的编程环境。你标记了esp-idf,所以我会假设你正在使用这个编程环境。

ESP-IDF包括一个名为“NVS(非易失性存储)”的库,用于存储键-值对。NVS使用闪存的一个分区来存储这些数据。它设计用于存储许多小值,更像是整数和短字符串(最多4000字节),而不是照片、视频或文件。因此,它适用于WiFi配置或安全证书等内容。

ESP-IDF包括如何使用NVS的示例。

从中读取的一个非常简单的示例将包括以下代码片段:

#include "nvs_flash.h"
#include "nvs.h"

// 初始化NVS
esp_err_t err = nvs_flash_init();
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
    // NVS分区被截断并需要擦除
    // 重新尝试nvs_flash_init
    ESP_ERROR_CHECK(nvs_flash_erase());
    err = nvs_flash_init();
}

// 打开NVS以开始使用它
nvs_handle_t my_handle;
err = nvs_open("storage", NVS_READWRITE, &my_handle);
if (err != ESP_OK) {
    printf("Error (%s) opening NVS handle!\n", esp_err_to_name(err));
} else {
    // 读取字符串 - 将设置ssid_len为读取的字节数
    size_t ssid_len = SSID_MAX_LEN;
    char ssid[SSID_MAX_LEN];

    err = nvs_get_str(my_handle, "ssid", ssid, &ssid_len);
    if (err == ESP_OK) {
        printf("got WiFi SSID %s\n", ssid);
    } else {
        printf("NVS lookup failed");
    }
}

你可以使用“NVS分区生成器”工具来构建一个NVS分区镜像,然后将其独立于固件烧录到ESP32上。

因为NVS使用自己的分区,与固件存储的位置分开,所以存储在NVS中的数据将在固件更新或更改时保留(只要新固件不更改闪存分区方案) - 即使新固件是完全不同的应用程序。

英文:

Environment variables aren't really a thing for the ESP32 as it's a single "process" environment, but you can store configuration separately from your program.

How you do this depends on your programming environment. You tagged esp-idf so I'll assume that's what you're programming in.

ESP-IDF includes a library called "NVS" for "Non-Volatile Storage". NVS uses a partition of the flash storage to store key-value pairs. It's designed to store many small values - more like integers and short strings (up to 4000 bytes) rather than photos or videos or files. So it's suitable for things like WiFi configuration or security certificates.

ESP-IDF includes examples of how to use NVS.

A very simple example of reading from it would include code fragments that would look like this:

#include "nvs_flash.h"
#include "nvs.h"

// initialize NVS
esp_err_t err = nvs_flash_init();
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
    // NVS partition was truncated and needs to be erased
    // Retry nvs_flash_init
    ESP_ERROR_CHECK(nvs_flash_erase());
    err = nvs_flash_init();
}

// open it to start using it
nvs_handle_t my_handle;
err = nvs_open("storage", NVS_READWRITE, &my_handle);
if (err != ESP_OK) {
    printf("Error (%s) opening NVS handle!\n", esp_err_to_name(err));
} else {
    // read a string - will set ssid_len to the number of bytes read
    size_t ssid_len = SSID_MAX_LEN;
    char ssid[SSID_MAX_LEN];

    err = nvs_get_str(my_handle, "ssid", ssid, &ssid_len);
    if (err == ESP_OK) {
        printf("got WiFi SSID %s\n", ssid);
    } else {
        printf("NVS lookup failed");
    }
}

You can use the "NVS Partition Generator" utility to build an NVS partition image which you can then flash to the ESP32 independently of the firmware.

Because NVS uses its own partition separate from where the firmware is stored, data stored in NVS will survive firmware updates or changes (as long as the new firmware doesn't change the flash partition scheme) - even if the new firmware is a completely different application.

huangapple
  • 本文由 发表于 2023年5月22日 02:39:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76301399.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定