ESP32编程笔记(9)——Smartconfig
标签搜索

ESP32编程笔记(9)——Smartconfig

ChencyCT
2024-02-27 / 0 评论 / 9 阅读 / 正在检测是否收录...

前言

随着智能家居,物联网的兴起,越来越多的设备需要连接家庭wifi网络。而WIFI网络的接入是需要知道无线路由器的SSID和密码。大部分的设备是没有输入接口的,在设备中预置WIFI的SSID和密码是不现实的,为了解决这个问题,smartconfig配网方式应运而生。

ESP8266,ESP32使用了ESP-Touch协议,采用的是Smartconfig(快连)技术。当前设备没有和其他设备建立任何实际连接状态下,使用smartconfig一键配置该设备接入WIFI。

smartconfig只需要通过手机UDP发送报文,设备扫描可用无线信道,找到smartconfig的报文,并锁定在这一信道上开始接收数据。

lt4he89m.png

使用smartconfig配网只需要以下3个步骤

  1. 设备初始化,开始监听smartconfig报文数据
  2. 手机设置wifi的SSID和密码后,发送UDP广播
  3. 设备获得报文,配置网络,接入WIFI。

代码说明

  1. 头文件

    #include "esp_smartconfig.h

  2. 使用 esp_smartconfig_start() 函数配置 smartconfig并开始等待smartconfig报文数据。

    esp_err_t esp_smartconfig_start(const smartconfig_start_config_t *config)

  3. 配置完成后使用 esp_err_t esp_smartconfig_stop() 函数停止smartconfig

实例

代码还是有点问题,先水一篇文章吧。

#include <stdio.h>
#include <string.h>

#include "esp_system.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_event.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "esp_netif.h"
#include "esp_wifi.h"
#include "esp_smartconfig.h"
#include "lwip/sockets.h"

#define TCP_SERVER "192.168.8.104"


const char *TAG="smartconfig";




static void System_Init(void);
static void WiFi_init(void);
static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *data);
static void smartconfig_task(void);

void app_main(){
    System_Init();
    WiFi_init();
}
static void smartconfig_task(void){
    ESP_ERROR_CHECK(esp_smartconfig_set_type(SC_TYPE_ESPTOUCH));
    smartconfig_start_config_t cnf = SMARTCONFIG_START_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(esp_smartconfig_start(&cnf));
    while(1){
        vTaskDelay(500 / portTICK_PERIOD_MS);
    }
}

static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *data){
    if(event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START){
        ESP_LOGI(TAG, "wifi start");
        ESP_LOGI(TAG,"smartconfig");
        xTaskCreate(smartconfig_task,"task1",4096,NULL,3,NULL);
    }else if(event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED){
        ESP_LOGI(TAG,"wifi disconnected, retry");
        esp_wifi_connect();
    }else if(event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP){
        ESP_LOGI(TAG, "got ip");
        ip_event_got_ip_t *event  = (ip_event_got_ip_t *)data;
        ESP_LOGI(TAG,"got ip:"IPSTR,IP2STR(&event->ip_info.ip));
    }else if(event_base == SC_EVENT && event_id == SC_EVENT_FOUND_CHANNEL){
        ESP_LOGI(TAG,"sc found channel");
    }else if(event_base == SC_EVENT && event_id == SC_EVENT_SCAN_DONE){
        ESP_LOGI(TAG,"sc done");
    }else if(event_base == SC_EVENT && event_id == SC_EVENT_GOT_SSID_PSWD){
        ESP_LOGI(TAG,"sc got SSID and PWD");
        
        smartconfig_event_got_ssid_pswd_t *evt = (smartconfig_event_got_ssid_pswd_t *)data;
        wifi_config_t cnf;

        bzero(&cnf,sizeof(wifi_config_t));
        memcpy(cnf.sta.ssid, evt->ssid,sizeof(cnf.sta.ssid));
        memcpy(cnf.sta.password, evt->password,sizeof(cnf.sta.password));
        cnf.sta.bssid_set = evt->bssid_set;

        if(cnf.sta.bssid_set == true){
            memcpy(cnf.sta.bssid,evt->bssid,sizeof(cnf.sta.bssid));
        }

        ESP_LOGI(TAG,"GOT SSID: %s", evt->ssid);
        ESP_LOGI(TAG,"GOT PWD: %s",evt->password);

        esp_wifi_disconnect();
        esp_wifi_set_config(WIFI_IF_STA, &cnf);
        esp_wifi_connect();
    }
}

static void System_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(ret);
        ret = nvs_flash_init();
    }
    ESP_ERROR_CHECK(ret);
}

static void WiFi_init(void){
    ESP_ERROR_CHECK(esp_netif_init());
    ESP_ERROR_CHECK(esp_event_loop_create_default());

    esp_netif_create_default_wifi_sta();

    esp_event_handler_instance_t esp_any_id;
    esp_event_handler_instance_t esp_ip_id;
    esp_event_handler_instance_t esp_sc_id;
    ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,ESP_EVENT_ANY_ID,event_handler,NULL,&esp_any_id));
    ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,IP_EVENT_STA_GOT_IP,event_handler,NULL,&esp_ip_id));
    ESP_ERROR_CHECK(esp_event_handler_instance_register(SC_EVENT,ESP_EVENT_ANY_ID,event_handler,NULL,&esp_sc_id));
    wifi_init_config_t cnf = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(esp_wifi_init(&cnf));
    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
    ESP_ERROR_CHECK(esp_wifi_start());

    ESP_ERROR_CHECK(esp_event_handler_instance_unregister(WIFI_EVENT,ESP_EVENT_ANY_ID,&esp_any_id));
    ESP_ERROR_CHECK(esp_event_handler_instance_unregister(IP_EVENT,IP_EVENT_STA_GOT_IP,&esp_ip_id));
    ESP_ERROR_CHECK(esp_event_handler_instance_unregister(SC_EVENT,ESP_EVENT_ANY_ID,&esp_sc_id));
}

运行结果

lt4hgkpw.png
lt4hgtro.png

0

评论 (0)

取消