ไลบรารีที่เก็บข้อมูลที่ไม่ระดมววววววววววววววววววววววววววววววววววววววววววววววววววววววววววววววววววววว
1. คุณสมบัติ
2. ESP32 NVS
3. การใช้งานขั้นพื้นฐาน
4. การเริ่มต้น
5. ตัวอย่าง
6. การบริจาค
7. ใบอนุญาต
NVS ทำงานกับคู่คีย์-ค่า คีย์คือสตริง ASCII ความยาวสูงสุดของคีย์คือ 15 อักขระ ค่าสามารถมีหนึ่งในประเภทต่อไปนี้:
NVS กำหนดคู่คีย์-ค่าแต่ละคู่ให้กับหนึ่งในเนมสเปซ ชื่อเนมสเปซเป็นไปตามกฎเดียวกันกับชื่อคีย์คือความยาวสูงสุดคือ 15 อักขระ นอกจากนี้ยังมีเนมสเปซที่แตกต่างกันไม่เกิน 254 ในพาร์ติชัน NVS หนึ่งพาร์ติชัน
#include <stdlib.h>
#include "esp_check.h"
#include "esp_log.h"
#include "non_volatile_storage.h"
static const char * TAG = "app_main" ;
void app_main ( void )
{
ESP_ERROR_CHECK ( nvs_init ());
// Float
float write_float = 123456.789 ;
if ( nvs_write_float ( "namespace_1" , "key_1" , write_float ) == ESP_OK ) {
ESP_LOGI ( TAG , "Successfully write float to NVS" );
} else {
ESP_LOGE ( TAG , "Failed to write float to NVS" );
}
float read_float ;
if ( nvs_read_float ( "namespace_1" , "key_1" , & read_float ) == ESP_OK ) {
ESP_LOGI ( TAG , "Successfully read float from NVS: %f" , read_float );
} else {
ESP_LOGE ( TAG , "Failed to read float from NVS" );
}
// String
char * write_string = "Hello, World!" ;
if ( nvs_write_string ( "namespace_1" , "key_2" , write_string ) == ESP_OK ) {
ESP_LOGI ( TAG , "Successfully write string to NVS" );
} else {
ESP_LOGE ( TAG , "Failed to write string to NVS" );
}
char * read_string = NULL ;
if ( nvs_read_string ( "namespace_1" , "key_2" , & read_string ) == ESP_OK ) {
ESP_LOGI ( TAG , "Successfully read string from NVS: %s" , read_string );
} else {
ESP_LOGE ( TAG , "Failed to read string from NVS" );
}
// IMPORTANT NOTE!: This applies ONLY to strings. Remember to delete the pointer to avoid a memory leak.
free ( read_string );
// Blob
#define MAX_STRING_LENGTH 10
typedef struct {
char name [ MAX_STRING_LENGTH ];
uint32_t id ;
} person_t ;
const person_t write_blob = {
. name = "Joe" ,
. id = 123 ,
};
if ( nvs_write_blob ( "namespace_1" , "key_3" , & write_blob , sizeof ( person_t )) == ESP_OK ) {
ESP_LOGI ( TAG , "Successfully write blob to NVS" );
} else {
ESP_LOGE ( TAG , "Failed to write blob to NVS" );
}
person_t read_blob = {};
if ( nvs_read_blob ( "namespace_1" , "key_3" , & read_blob , sizeof ( person_t )) == ESP_OK ) {
ESP_LOGI ( TAG , "Successfully read blob from NVS: name:%s, id:%d" , read_blob . name , read_blob . id );
} else {
ESP_LOGE ( TAG , "Failed to read blob from NVS" );
}
}ในการเริ่มต้นใช้งานโครงการตรวจสอบงาน ESP32 คุณจะต้องใช้ไมโครคอนโทรลเลอร์ ESP32 และคอมพิวเตอร์โฮสต์ที่ใช้ Python คุณจะต้องติดตั้งกรอบการพัฒนา ESP-IDF และแพ็คเกจ Python ที่จำเป็น
git clone git @ github . com : VPavlusha / ESP32_NVS . git cd ESP32_NVS / example
idf . py build idf . py - p PORT [ - b BAUD ] flash แทนที่พอร์ตด้วยชื่อพอร์ตอนุกรมของบอร์ด ESP32 นอกจากนี้คุณยังสามารถเปลี่ยนอัตราการส่งแฟลช Baud โดยการแทนที่ baud ด้วยอัตราการรับส่งข้อมูลที่คุณต้องการ อัตราการรับส่งข้อมูลเริ่มต้นคือ 460800
idf . py - p < PORT > monitorอย่าลืมแทนที่พอร์ตด้วยชื่อพอร์ตอนุกรมของคุณ
ข้อมูลเพิ่มเติมวิธีการสร้างโครงการ: คู่มือการเขียนโปรแกรม ESP-IDF
โครงการนี้มีตัวอย่างที่แสดงฟังก์ชั่นของไลบรารีการตรวจสอบงาน ตัวอย่างนี้ให้การสาธิตในทางปฏิบัติเกี่ยวกับวิธีการใช้ NVS API เพื่อเขียน/อ่านข้อมูลไปยัง/จาก NVS ในแอปพลิเคชันของคุณเอง
ยินดีต้อนรับการมีส่วนร่วมในโครงการ ESP32 NVS หากคุณพบข้อผิดพลาดหรือมีคำขอคุณสมบัติโปรดส่งปัญหาในหน้า GitHub ของโครงการ หากคุณต้องการมีส่วนร่วมในรหัสโปรดส่งคำขอดึง
โครงการ ESP32 NVS ได้รับใบอนุญาตภายใต้ใบอนุญาต MIT ดูไฟล์ลิขสิทธิ์ MIT สำหรับข้อมูลเพิ่มเติม