简单例子
10秒后重启ESP32
10秒后重启ESP32
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
void app_10s_restart(void)
{
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();
}
void app_main(void)
{
printf("Hello DaoBanMoJie!\n");
app_10s_restart();
}
Wifi
WiFi扫描
WiFi扫描
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "esp_wifi.h"
#include "esp_log.h"
#include "esp_event.h"
#include "nvs_flash.h"
void NVS_Init(void)
{
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK( ret );
}
static void wifi_scan(void)
{
//Wi-Fi 初始化
ESP_ERROR_CHECK(esp_netif_init()); //初始化底层TCP/IP堆栈
ESP_ERROR_CHECK(esp_event_loop_create_default()); //以默认配置创建一个系统事件任务
esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta(); //创建有 TCP/IP 堆栈的默认网络接口实例绑定 station
assert(sta_netif);
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg)); //创建 Wi-Fi 驱动程序任务
// 配置扫描
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); // 配置存储类型默认为 Flash
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); // 配置为station
ESP_ERROR_CHECK(esp_wifi_start()); // 启动wifi
//ESP_ERROR_CHECK(esp_wifi_scan_start(NULL, true)); // 默认扫描
static wifi_scan_config_t scanConf = {
.ssid = NULL,
.bssid = NULL,
.channel = 0,
.show_hidden = 0
}; // 关闭隐藏 0:扫描会忽略带有隐藏SSID的AP;1:扫描会将隐藏AP视为正常AP
ESP_ERROR_CHECK(esp_wifi_scan_start(&scanConf, true)); // 开始扫描并等待扫描结束
uint16_t ap_count = 0;
ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&ap_count)); // 获取WiFi数量
ESP_LOGI("无线接入点数量:", "%u", ap_count);
wifi_ap_record_t ap_info[ap_count];
ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&ap_count, ap_info)); //获取WiFi信息
for (int i = 0; i < ap_count; i++) {
ESP_LOGI("无线名称", " %s", ap_info[i].ssid);
ESP_LOGI("信号强度", " %d\n", ap_info[i].rssi);
}
ESP_LOGI("\n", " 扫描结束\n");
}
void app_main(void)
{
NVS_Init();
wifi_scan();
}
WiFi连接
WiFi连接
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "lwip/err.h"
#include "lwip/sys.h"
#define EXAMPLE_ESP_WIFI_SSID "WaliMaker"
#define EXAMPLE_ESP_WIFI_PASS "jdsap7h3"
#define EXAMPLE_ESP_MAXIMUM_RETRY CONFIG_ESP_MAXIMUM_RETRY
/* FreeRTOS事件组在我们连接时发出信号*/
static EventGroupHandle_t s_wifi_event_group;
/* The event group allows multiple bits for each event, but we only care about two events:
* - we are connected to the AP with an IP
* - we failed to connect after the maximum amount of retries */
#define WIFI_CONNECTED_BIT BIT0
#define WIFI_FAIL_BIT BIT1
void NVS_Init(void)
{
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
{
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
ESP_LOGI("esp_err_t类型返回值:", " %d\n", ret);
}
// 系统事件循环处理函数
static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
{
static int s_retry_num = 0;
// wifi事件状态为 start 时连接
if (event_base == WIFI_EVENT )
{
if (event_id == WIFI_EVENT_STA_START) // 事件id为STA开始
{
ESP_LOGI("WIFI_EVENT事件 ", "连接wifi\n");
esp_wifi_connect();
}
else if (event_id == WIFI_EVENT_STA_DISCONNECTED) // 事件id为STA断开,重新连接直到最大重连数
{
if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY)
{
esp_wifi_connect();
s_retry_num++;
ESP_LOGI("WIFI_EVENT事件:", "重试连接AP");
}
else
{
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
}
ESP_LOGI("WIFI_EVENT事件:", "连接AP失败");
}
}
else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) // 系统事件为ip地址事件,且事件id为成功获取ip地址
{
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
ESP_LOGI("IP_EVENT事件 ", "得到IP:" IPSTR, IP2STR(&event->ip_info.ip));
s_retry_num = 0;
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
}
}
void wifi_init_sta(void)
{
s_wifi_event_group = xEventGroupCreate(); // 创建事件标志组,返回值为事件标志组句柄,根据WIFI的连接状况来做出相应动作。
ESP_ERROR_CHECK(esp_netif_init()); // 初始化底层TCP/IP堆栈
ESP_ERROR_CHECK(esp_event_loop_create_default()); // 创建默认事件循环
esp_netif_create_default_wifi_sta(); // 创建一个默认的WIFI-STA网络接口
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); // 默认的wifi配置参数
ESP_ERROR_CHECK(esp_wifi_init(&cfg)); // 根据 cfg 初始化 wifi
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); // 配置存储类型设置为 RAM
// 将事件处理程序注册到系统默认事件循环,分别是WiFi事件和IP地址事件
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL));
// 配置WiFi连接的 ssid password 参数
wifi_config_t wifi_config = {
.sta = {
.ssid = EXAMPLE_ESP_WIFI_SSID,
.password = EXAMPLE_ESP_WIFI_PASS,
.scan_method = WIFI_ALL_CHANNEL_SCAN,
.sort_method = WIFI_CONNECT_AP_BY_SIGNAL,
.threshold.rssi = -127,
.threshold.authmode = WIFI_AUTH_WPA2_PSK,
.pmf_cfg = {
.capable = true,
.required = false},
},
};
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); // 工作模式设置为 STA
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); // 根据 wifi_config 进行设置
ESP_ERROR_CHECK(esp_wifi_start()); // 启动wifi连接
/* 等待标志组。条件到达最大重试次数 WIFI_FAIL_BIT ,直到连接建立 WIFI_CONNECTED_BIT 或连接失败。位由event_handler()设置(参见上面) */
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group, // 需要等待的事件标志组的句柄
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT, // 需要等待的事件位
pdFALSE, // 为pdFALSE时,在退出此函数之前所设置的这些事件位不变,为pdFALSE则清零
pdFALSE, // 为pdFALSE时,设置的这些事件位任意一个置1就会返回,为pdFALSE则需全为1才返回
portMAX_DELAY); // 设置为最长阻塞等待时间,单位为时钟节拍
/* xEventGroupWaitBits()返回调用的位,根据事件标志组等待函数的返回值获取WiFi连接状态 */
if (bits & WIFI_CONNECTED_BIT) // WiFi连接成功事件
{
ESP_LOGI("标志bits状态:", "连接成功\n 名称:%s\n 密码:%s\n",EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
}
else if (bits & WIFI_FAIL_BIT)
{
ESP_LOGI("标志bits状态:", "连接失败\n 名称:%s\n 密码:%s\n",EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
}
else
{
ESP_LOGE("标志bits状态:", "突发事件");
}
// 注销事件
ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler));
ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler));
vEventGroupDelete(s_wifi_event_group); // 删除WiFi连接事件标志组
}
void app_main(void)
{
NVS_Init();
wifi_init_sta();
}
NVS
初始化NVS
初始化NVS,如果初始化失败则擦除后再初始化。
初始化NVS
#include "esp_log.h"
#include "nvs_flash.h"
void NVS_Init(void)
{
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK( ret );
ESP_LOGI("esp_err_t类型返回值:", " %d\n", ret);
}
void app_main(void)
{
NVS_Init();
}