esp32 SPIFFs 保存设备的状态参数和配置参数 idf 代码
时间: 2024-05-11 13:18:39 浏览: 170
在ESP32 IDF中使用SPIFFS(Arduino中也有SPIFFS库)进行设备状态参数和配置参数的保存需要以下步骤:
1. 配置SPIFFS文件系统
在`sdkconfig.h`中添加以下参数配置:
```c
#define CONFIG_SPIFFS_MAX_PARTITIONS 3
#define CONFIG_SPIFFS_OBJ_NAME_LEN 32
#define CONFIG_SPIFFS_USE_MAGIC 1
#define CONFIG_SPIFFS_MAX_FILES 8
#define CONFIG_SPIFFS_PAGE_CHECK 1
#define CONFIG_SPIFFS_CACHE_WR 1
#define CONFIG_SPIFFS_CACHE_WR_PAGES 4
#define CONFIG_SPIFFS_CACHE_WR_COUNT 2
#define CONFIG_SPIFFS_CACHE_READ 1
#define CONFIG_SPIFFS_CACHE_READ_COUNT 4
#define CONFIG_SPIFFS_META_LENGTH 4
#define CONFIG_SPIFFS_USE_MTIME 1
#define CONFIG_SPIFFS_USE_DIRTY 1
#define CONFIG_SPIFFS_OBJ_META_LEN 4
#define CONFIG_SPIFFS_USE_MAGIC 1
```
2. 初始化SPIFFS文件系统
在代码中添加以下初始化函数:
```c
#include "esp_spiffs.h"
void app_main()
{
// Mount SPIFFS
esp_vfs_spiffs_conf_t conf = {
.base_path = "/spiffs",
.partition_label = NULL,
.max_files = 5,
.format_if_mount_failed = true
};
esp_err_t ret = esp_vfs_spiffs_register(&conf);
if (ret != ESP_OK) {
if (ret == ESP_FAIL) {
printf("Failed to mount or format filesystem\n");
} else if (ret == ESP_ERR_NOT_FOUND) {
printf("Failed to find SPIFFS partition\n");
} else {
printf("Failed to initialize SPIFFS (%s)\n", esp_err_to_name(ret));
}
return;
}
}
```
3. 保存参数
```c
#include "esp_spiffs.h"
void save_param(char* param_name, char* param_val) {
FILE* f = fopen("/spiffs/params.txt", "w");
if (f == NULL) {
printf("Failed to open file for writing");
return;
}
fprintf(f, "%s=%s\n", param_name, param_val);
fclose(f);
}
void app_main()
{
// Mount SPIFFS
esp_vfs_spiffs_conf_t conf = {
.base_path = "/spiffs",
.partition_label = NULL,
.max_files = 5,
.format_if_mount_failed = true
};
esp_err_t ret = esp_vfs_spiffs_register(&conf);
if (ret != ESP_OK) {
if (ret == ESP_FAIL) {
printf("Failed to mount or format filesystem\n");
} else if (ret == ESP_ERR_NOT_FOUND) {
printf("Failed to find SPIFFS partition\n");
} else {
printf("Failed to initialize SPIFFS (%s)\n", esp_err_to_name(ret));
}
return;
}
// Save parameter
save_param("param1", "value1");
}
```
4. 读取参数
```c
#include "esp_spiffs.h"
void read_param(char* param_name, char* param_val, size_t len) {
FILE* f = fopen("/spiffs/params.txt", "r");
if (f == NULL) {
printf("Failed to open file for reading");
return;
}
char line[128];
while (fgets(line, sizeof(line), f)) {
char* pos = strchr(line, '=');
if (pos == NULL) {
continue;
}
*pos = '\0';
if (strcmp(line, param_name) == 0) {
strncpy(param_val, pos + 1, len);
break;
}
}
fclose(f);
}
void app_main()
{
// Mount SPIFFS
esp_vfs_spiffs_conf_t conf = {
.base_path = "/spiffs",
.partition_label = NULL,
.max_files = 5,
.format_if_mount_failed = true
};
esp_err_t ret = esp_vfs_spiffs_register(&conf);
if (ret != ESP_OK) {
if (ret == ESP_FAIL) {
printf("Failed to mount or format filesystem\n");
} else if (ret == ESP_ERR_NOT_FOUND) {
printf("Failed to find SPIFFS partition\n");
} else {
printf("Failed to initialize SPIFFS (%s)\n", esp_err_to_name(ret));
}
return;
}
// Save parameter
save_param("param1", "value1");
// Read parameter
char param_val[32];
read_param("param1", param_val, sizeof(param_val));
printf("Param1 value: %s\n", param_val);
}
```
阅读全文