zoukankan      html  css  js  c++  java
  • 107-ESP32_SDK开发-flash数据存储nvs

    <p><iframe name="ifd" src="https://mnifdv.cn/resource/cnblogs/LearnESP32" frameborder="0" scrolling="auto" width="100%" height="1500"></iframe></p>

    说明

    nvs是底层封装的一套把数据存储到flash里面的函数;

    数据是以键值对的形式存储数据(就是标识符+数据的形式)

    不如直接看代码

    #include <stdio.h>
    #include <string.h>
    #include "freertos/FreeRTOS.h"
    #include "freertos/task.h"
    #include "freertos/queue.h"
    #include "freertos/event_groups.h"
    #include "esp_system.h"
    #include "nvs_flash.h"
    #include "esp_log.h"
    
    
    void app_main(void)
    {
        //初始化 NVS
        esp_err_t err = nvs_flash_init();
        if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
          ESP_ERROR_CHECK(nvs_flash_erase());
          err = nvs_flash_init();
        }
        ESP_ERROR_CHECK(err);
    
    
        /*操作nvs时用的句柄*/
        nvs_handle_t my_handle;
    
        /*打开*/     //操作的表格名字 //以读写模式打开
        err = nvs_open("storage", NVS_READWRITE, &my_handle);
    
        /**/
        err = nvs_set_i32(my_handle, "test", 111);
        /*提交*/
        err = nvs_commit(my_handle);
    
        int32_t test_value;
        /**/
        err = nvs_get_i32(my_handle, "test", &test_value);
        printf("test_value = %d
    ", test_value);
    
        /*关闭*/
        nvs_close(my_handle);
    }

    1.其实相当于操作一个表格,初始化和打开这个表格

    storage代表做操作的表格的名字,可随意设置,字符串的长度默认最长15个字符 (NVS_PART_NAME_MAX_SIZE - 1)

    2.设置表格中字段名字为test的值为111 

    i32代表32字节数据,其实有许多类型

    test字符串是咱的数据的标签名在数据库中也常叫做字段名

    ,可随意设置,字符串的长度默认最长15个字符 (NVS_PART_NAME_MAX_SIZE - 1)

     

    后面的111是设置的值

    3.提交

    设置完值以后调用一下提交函数

    4.读取

    更加详细的代码可参看官方

  • 相关阅读:
    云计算初探
    MySQL、HBase、ES的特点和区别
    MongoDB、ElasticSearch、Redis、HBase这四种热门数据库的优缺点及应用场景
    主流 Kubernetes 发行版梳理
    如何在flink中传递参数
    (47)zabbix报警媒介:Ez Texting
    (46)zabbix报警媒介:Jabber
    (45)zabbix报警媒介:SMS
    (44)zabbix报警媒介:email
    (43)zabbix报警媒介介绍
  • 原文地址:https://www.cnblogs.com/yangfengwu/p/15221493.html
Copyright © 2011-2022 走看看