代码拉取完成,页面将自动刷新
/* Esptouch 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 <string.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_wifi.h"
#include "esp_wpa2.h"
#include "esp_event.h"
#include "esp_log.h"
#include "esp_system.h"
#include "nvs_flash.h"
#include "esp_netif.h"
#include "esp_smartconfig.h"
#include "smartconfig_led.h"
void mqtt_app_start(void);
/* FreeRTOS event group to signal when we are connected & ready to make a request */
static EventGroupHandle_t s_wifi_event_group;
/* The event group allows multiple bits for each event,
but we only care about one event - are we connected
to the AP with an IP? */
static const int CONNECTED_BIT = BIT0;
static const int ESPTOUCH_DONE_BIT = BIT1;
#define WIFI_CONNECTED_BIT BIT0
#define WIFI_FAIL_BIT BIT1
static const char *TAG = "smartconfig_example";
wifi_config_t wifi_config;
static void smartconfig_example_task(void * parm);
//////////////////////////// save_wifi_api.c --start
typedef enum {
wifi_unconfiged = 0,
wifi_configed = 0xAA,
}wifi_info_storage_t;
//保存wifi配置参数结构体变量wifi_config到nvs------------------------------------------------
#define ID_AND_PWD_LEN (32+64)
/**********************************************
* *函数名:clearWifiConfigFlag
* *功能描述:清除配网标记,如果运行这个函数,可以配合esp_restart(),复位系统。重新配网
* * -- 主要是取代nvs_flash_erase()函数,这个函数把所有的数据都擦除,是不对的。
* *******************************************/
void clearWifiConfigFlag(void)
{
nvs_handle my_handle;
// 0.打开
nvs_open("WIFI_CONFIG", NVS_READWRITE, &my_handle);
// 1.写入标记 0x00,清除配网标记
nvs_set_u8(my_handle, "WifiConfigFlag", wifi_unconfiged);
// 2.提交 并保存表的内容
ESP_ERROR_CHECK(nvs_commit(my_handle));
// 3.退出
nvs_close(my_handle);
}
static void saveWifiConfig(wifi_config_t *wifi_config)
{
nvs_handle my_handle;
// 0.打开
nvs_open("WIFI_CONFIG", NVS_READWRITE, &my_handle);
// 1.写入标记 0xaa,表示已经配过网
nvs_set_u8(my_handle, "WifiConfigFlag", wifi_configed);
// 2.写入AP ID和AP password
ESP_ERROR_CHECK(nvs_set_blob(my_handle, "wifi_config", wifi_config, ID_AND_PWD_LEN));
// 3.提交 并保存表的内容
ESP_ERROR_CHECK(nvs_commit(my_handle));
// 4.退出
nvs_close(my_handle);
}
//从nvs中读取wifi配置到给定的sta_config结构体变量
static esp_err_t readWifiConfig(wifi_config_t *sta_config)
{
nvs_handle my_handle;
unsigned char u8WifiConfigVal;
// 0.打开
nvs_open("WIFI_CONFIG", NVS_READWRITE, &my_handle);
// 1.读取标志位,并判断
nvs_get_u8(my_handle, "WifiConfigFlag", &u8WifiConfigVal);
if(u8WifiConfigVal != wifi_configed){
// 1.1 没有配过网,关闭nvs,返回错误码
ESP_LOGI(TAG, "no wifi config,read fail!");
nvs_close(my_handle);
return ESP_FAIL;
}else{
// 1.2 进入下个步骤
ESP_LOGI(TAG, "wifi configed ,read ok!");
}
// 2.读取上一次配网的ID,password
uint32_t len = ID_AND_PWD_LEN;
esp_err_t err = nvs_get_blob(my_handle, "wifi_config", sta_config, &len);
ESP_LOGI(TAG, "readout SSID:%s", sta_config->sta.ssid);
ESP_LOGI(TAG, "readout PASSWORD:%s", sta_config->sta.password);
// 3.关闭nvs退出
nvs_close(my_handle);
return err;
}
//////////////////////////// save_wifi_api.c --end
static void event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
// if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
// xTaskCreate(smartconfig_example_task, "smartconfig_example_task", 4096, NULL, 3, NULL);
// } else
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
esp_wifi_connect();
xEventGroupClearBits(s_wifi_event_group, CONNECTED_BIT);
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
xEventGroupSetBits(s_wifi_event_group, CONNECTED_BIT);
delegate_wifi_connected_status();
//readWifiConfig(&wifi_config);
} else if (event_base == SC_EVENT && event_id == SC_EVENT_SCAN_DONE) {
ESP_LOGI(TAG, "Scan done");
} else if (event_base == SC_EVENT && event_id == SC_EVENT_FOUND_CHANNEL) {
ESP_LOGI(TAG, "Found channel");
delegate_wifi_connecting_status();
} else if (event_base == SC_EVENT && event_id == SC_EVENT_GOT_SSID_PSWD) {
ESP_LOGI(TAG, "Got SSID and password");
//wifi_config_t wifi_config;
smartconfig_event_got_ssid_pswd_t *evt = (smartconfig_event_got_ssid_pswd_t *)event_data;
uint8_t ssid[33] = { 0 };
uint8_t password[65] = { 0 };
uint8_t rvd_data[33] = { 0 };
bzero(&wifi_config, sizeof(wifi_config_t));
memcpy(wifi_config.sta.ssid, evt->ssid, sizeof(wifi_config.sta.ssid));
memcpy(wifi_config.sta.password, evt->password, sizeof(wifi_config.sta.password));
wifi_config.sta.bssid_set = evt->bssid_set;
if (wifi_config.sta.bssid_set == true) {
memcpy(wifi_config.sta.bssid, evt->bssid, sizeof(wifi_config.sta.bssid));
}
memcpy(ssid, evt->ssid, sizeof(evt->ssid));
memcpy(password, evt->password, sizeof(evt->password));
ESP_LOGI(TAG, "SSID:%s", ssid);
ESP_LOGI(TAG, "PASSWORD:%s", password);
if (evt->type == SC_TYPE_ESPTOUCH_V2) {
ESP_ERROR_CHECK( esp_smartconfig_get_rvd_data(rvd_data, sizeof(rvd_data)) );
ESP_LOGI(TAG, "RVD_DATA:");
for (int i=0; i<33; i++) {
printf("%02x ", rvd_data[i]);
}
printf("\n");
}
ESP_ERROR_CHECK( esp_wifi_disconnect() );
ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
esp_wifi_connect();
// 运行到此处,说明连接成功,可以保存id,password
saveWifiConfig(&wifi_config);
} else if (event_base == SC_EVENT && event_id == SC_EVENT_SEND_ACK_DONE) {
xEventGroupSetBits(s_wifi_event_group, ESPTOUCH_DONE_BIT);
}
}
static void initialise_wifi(void)
{
ESP_ERROR_CHECK(esp_netif_init());
s_wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta();
assert(sta_netif);
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
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) );
ESP_ERROR_CHECK( esp_event_handler_register(SC_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL) );
//wifi_config_t wifi_config;
if(ESP_OK == readWifiConfig(&wifi_config))
{
wifi_config.sta .threshold.authmode = WIFI_AUTH_WPA2_PSK;
/* 设置WiFi的工作模式为 STA */
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
/* 设置WiFi连接的参数,主要是ssid和password */
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
/* 启动WIFI 驱动程序*/
ESP_ERROR_CHECK(esp_wifi_start());
/* 启动WiFi连接到 AP*/
ESP_ERROR_CHECK(esp_wifi_connect());
ESP_LOGI(TAG, "wifi_init_sta finished.");
/* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
* number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,pdFALSE, pdFALSE,portMAX_DELAY);
/* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
* happened. */
if (bits & WIFI_CONNECTED_BIT) {
ESP_LOGI(TAG, "connected to ap SSID:%s password:%s",
wifi_config.sta.ssid, wifi_config.sta.password);
mqtt_app_start();
} else if (bits & WIFI_FAIL_BIT) {
ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s",
wifi_config.sta.ssid, wifi_config.sta.password);
} else {
ESP_LOGE(TAG, "UNEXPECTED EVENT");
}
}
else
{
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
ESP_ERROR_CHECK( esp_wifi_start() );
xTaskCreate(smartconfig_example_task, "smartconfig_example_task", 4096, NULL, 3, NULL);
}
}
static void smartconfig_example_task(void * parm)
{
EventBits_t uxBits;
//ESP_ERROR_CHECK( esp_smartconfig_set_type(SC_TYPE_ESPTOUCH) );
ESP_ERROR_CHECK( esp_smartconfig_set_type(SC_TYPE_ESPTOUCH_AIRKISS) );
smartconfig_start_config_t cfg = SMARTCONFIG_START_CONFIG_DEFAULT();
ESP_ERROR_CHECK( esp_smartconfig_start(&cfg) );
while (1) {
uxBits = xEventGroupWaitBits(s_wifi_event_group, CONNECTED_BIT | ESPTOUCH_DONE_BIT, true, false, portMAX_DELAY);
if(uxBits & CONNECTED_BIT) {
ESP_LOGI(TAG, "WiFi Connected to ap");
mqtt_app_start();
}
if(uxBits & ESPTOUCH_DONE_BIT) {
ESP_LOGI(TAG, "smartconfig over");
esp_smartconfig_stop();
vTaskDelete(NULL);
}
}
}
void app_main(void)
{
ESP_ERROR_CHECK( nvs_flash_init() );
//ESP_ERROR_CHECK(nvs_flash_erase()); ESP_ERROR_CHECK( nvs_flash_init() );
wifi_status_led_init();
smartconfig_button_init();
initialise_wifi();
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。