ESP8266_RTOS - 儲存篇(2) nvs 操作 nvs_set&vs_get
前言
Non-volatile storage(NVS)
NVS庫主要用於在flash中儲存鑑值格式的數據,NVS也是基於flash(read/write/erase)做的處理
詳情請看官方文檔:文檔
程式碼
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | void nvs_write_data_to_flash(void) { nvs_handle handle; static const char *NVS_CUSTOMER = "date1"; static const char *DATA1 = "param1"; static const char *DATA2 = "param2"; static const char *DATA3 = "param3"; int32_t value_for_store = 666; wifi_config_t wifi_config_to_store = { .sta = { .ssid = "ssid:hello_YEE", .password = "password:12345678", }, }; printf("set size:%u\r\n", sizeof(wifi_config_to_store)); ESP_ERROR_CHECK( nvs_open( NVS_CUSTOMER, NVS_READWRITE, &handle) ); ESP_ERROR_CHECK( nvs_set_str( handle, DATA1, "i am a string.") ); ESP_ERROR_CHECK( nvs_set_i32( handle, DATA2, value_for_store) ); ESP_ERROR_CHECK( nvs_set_blob( handle, DATA3, &wifi_config_to_store, sizeof(wifi_config_to_store)) ); ESP_ERROR_CHECK( nvs_commit(handle) ); nvs_close(handle); } |
=======
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | void nvs_read_data_from_flash(void) { nvs_handle handle; static const char *NVS_CUSTOMER = "date1"; static const char *DATA1 = "param1"; static const char *DATA2 = "param2"; static const char *DATA3 = "param3"; uint32_t str_length = 32; char str_data[32] = {0}; int32_t value = 0; wifi_config_t wifi_config_stored; memset(&wifi_config_stored, 0x0, sizeof(wifi_config_stored)); uint32_t len = sizeof(wifi_config_stored); ESP_ERROR_CHECK( nvs_open(NVS_CUSTOMER, NVS_READWRITE, &handle) ); ESP_ERROR_CHECK ( nvs_get_str(handle, DATA1, str_data, &str_length) ); ESP_ERROR_CHECK ( nvs_get_i32(handle, DATA2, &value) ); ESP_ERROR_CHECK ( nvs_get_blob(handle, DATA3, &wifi_config_stored, &len) ); printf("[data1]: %s len:%u\r\n", str_data, str_length); printf("[data2]: %d\r\n", value); printf("[data3]: ssid:%s passwd:%s\r\n", wifi_config_stored.sta.ssid, wifi_config_stored.sta.password); nvs_close(handle); } |
留言
張貼留言