前言
随着智能家居,物联网的兴起,越来越多的设备需要连接家庭wifi网络。而WIFI网络的接入是需要知道无线路由器的SSID和密码。大部分的设备是没有输入接口的,在设备中预置WIFI的SSID和密码是不现实的,为了解决这个问题,smartconfig配网方式应运而生。
ESP8266,ESP32使用了ESP-Touch协议,采用的是Smartconfig(快连)技术。当前设备没有和其他设备建立任何实际连接状态下,使用smartconfig一键配置该设备接入WIFI。
smartconfig只需要通过手机UDP发送报文,设备扫描可用无线信道,找到smartconfig的报文,并锁定在这一信道上开始接收数据。
使用smartconfig配网只需要以下3个步骤
-
设备初始化,开始监听smartconfig报文数据
-
手机设置wifi的SSID和密码后,发送UDP广播
-
设备获得报文,配置网络,接入WIFI。
代码说明
- 头文件
#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(