在ESP32 NVS中保存wifi ssid和password,并记录设备重启次数

接上篇文章,增加了在NVS中保存wifi ssid和password的代码:

运行结果如图:
在这里插入图片描述

1.nvs初始化:

在这里插入图片描述

2.记录设备重启次数:

在这里插入图片描述

3.保存wifi ssid和password

在这里插入图片描述

4.关闭NVS:

在这里插入图片描述

完整代码如下:

/* Hello World Example

This example code is in the Public Domain (or CC0 licensed, at your option.)

Unless required by applicable law or agreed to in writing, this
software is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include <string.h> //michael add for NVS test
#include “sdkconfig.h”
#include “freertos/FreeRTOS.h”
#include “freertos/task.h”
#include “esp_system.h”
#include “esp_spi_flash.h”
#include “nvs_flash.h” //michael add for NVS test
#include “esp_log.h”//michael add for NVS test

void app_main(void)
{

vTaskDelay(1000/portTICK_PERIOD_MS);//阻止flash esp32后下面程序被立即执行

/* 初始化nvs*/
char * michael_nvs_namespace = "michael2";//namespace类似windows的文件夹
nvs_flash_init();//类似windows插入U盘
nvs_handle_t michael_handle;
nvs_open(michael_nvs_namespace,NVS_READWRITE,&michael_handle);//取得namespace句柄,类似windows打开文件夹

/* 下面这段是在nvs中记录设备重启次数*/

uint32_t counter_val = 0;//value类似windows的文件内容
char * counter_key = "counter";//key类似windows的文件名
nvs_get_u32(michael_handle,counter_key,&counter_val);
ESP_LOGI("nvs","KEY:VALUE %s:%d",counter_key,counter_val);

counter_val = counter_val + 1;
nvs_set_u32(michael_handle,counter_key,counter_val);
nvs_commit(michael_handle);//立刻执行nvs_set动作

/* 下面这段是在nvs中记录wifi ssid和password*/
uint32_t max_ap = 2;
typedef struct {
    char ssid[50];
    char password[50];
} ap_t;

ap_t aps_set[max_ap];

memset(aps_set,0,sizeof(aps_set));

for (int i=0;i<max_ap;i++) {
    strcpy(aps_set[i].ssid, "michaelap");
    strcpy(aps_set[i].password, "123456");
}

nvs_set_blob(michael_handle,"aps",aps_set,sizeof(aps_set));//写入ssid和password
nvs_commit(michael_handle);//立刻执行nvs_set动作
ap_t aps_get[max_ap];
size_t length = sizeof(aps_get);
nvs_get_blob(michael_handle,"aps",aps_get,&length);//读出ssid和password,用于后面打印

for (int i=0;i<max_ap;i++) {
    ESP_LOGI("wifi","SSID:PASSORD %s:%s", aps_get[i].ssid, aps_get[i].password);
}

/* 关闭nvs*/
nvs_close(michael_handle);//释放namespace句柄,类似windows关闭文件夹
nvs_flash_deinit();//类似windows拔出U盘

/* 下面这段是Hello world例程*/
printf("Hello world!\n");
/* Print chip information */
esp_chip_info_t chip_info;
esp_chip_info(&chip_info);
printf("This is %s chip with %d CPU core(s), WiFi%s%s, ",
        CONFIG_IDF_TARGET,
        chip_info.cores,
        (chip_info.features & CHIP_FEATURE_BT) ? "/BT" : "",
        (chip_info.features & CHIP_FEATURE_BLE) ? "/BLE" : "");

printf("silicon revision %d, ", chip_info.revision);

printf("%dMB %s flash\n", spi_flash_get_chip_size() / (1024 * 1024),
        (chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external");

printf("Minimum free heap size: %d bytes\n", esp_get_minimum_free_heap_size());

for (int i = 10; i >= 0; i--) {
    printf("Restarting in %d seconds...\n", i);
    vTaskDelay(1000 / portTICK_PERIOD_MS);
}
printf("Restarting now.\n");
fflush(stdout);
esp_restart();

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ESP32是一种低成本、低功耗的微控制器,它内置了WiFi和蓝牙模块,可以轻松地连接到互联网或其他设备。 要使用ESP32连接WiFi网络,您可以使用ESP-IDF(ESP32开发框架)提供的WiFi库。以下是基本的步骤: 1. 在ESP-IDF配置WiFi连接参数,例如SSID和密码。 2. 初始化WiFi连接并启用WiFi。 3. 使用WiFi管理器扫描可用的WiFi网络。 4. 连接到您选择的WiFi网络。 5. 确认连接成功并开始使用网络。 以下是一个简单的ESP-IDF程序,演示了如何连接到WiFi网络: ``` #include <string.h> #include <stdlib.h> #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "esp_system.h" #include "esp_wifi.h" #include "esp_event.h" #include "esp_log.h" #define WIFI_SSID "your_wifi_ssid" #define WIFI_PASS "your_wifi_password" static const char *TAG = "wifi station"; static void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { if (event_id == WIFI_EVENT_STA_START) { esp_wifi_connect(); } else if (event_id == WIFI_EVENT_STA_DISCONNECTED) { esp_wifi_connect(); ESP_LOGI(TAG,"retry to connect to the AP"); } else if (event_id == WIFI_EVENT_STA_CONNECTED) { ESP_LOGI(TAG,"connected to AP"); } } void wifi_init_sta(void) { tcpip_adapter_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); esp_event_handler_instance_t instance_any_id; esp_event_handler_instance_t instance_got_ip; esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL, &instance_any_id); esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &wifi_event_handler, NULL, &instance_got_ip); wifi_config_t wifi_config = { .sta = { .ssid = WIFI_SSID, .password = WIFI_PASS, .threshold.authmode = WIFI_AUTH_WPA2_PSK, .pmf_cfg = { .capable = true, .required = false }, }, }; ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); ESP_ERROR_CHECK(esp_wifi_start()); ESP_LOGI(TAG, "wifi_init_sta finished."); } void app_main() { ESP_ERROR_CHECK(nvs_flash_init()); wifi_init_sta(); } ``` 请注意,此示例仅适用于连接到使用WPA2密码保护的WiFi网络。如果您的网络使用不同的安全协议或没有密码,请相应地更改WiFi配置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值