esp32s3-idf使用smart_config一键配网

所谓配网,就是让esp32收到wifi的账号和密码,自己去连接wifi,使用的的是乐鑫开发的esptouch这个配网app,只支持2.4ghz的wifi,不支持5g。

配网的流程是esp32先开启配网,然后手机打开配网软件,配网软件会连续发送三个开始码,然后发送账号密码,在发送三个终止码。

esp32端通过事件接收机制,会收到事件的数据,每一个事件类型都对应一个数据类型,然后处理数据获取账号和密码,再配置wifi就可以连接了。

  • WIFI_EVENT_*:如 WIFI_EVENT_STA_CONNECTED 对应 wifi_event_sta_connected_t,WIFI_EVENT_STA_DISCONNECTED 对应 wifi_event_sta_disconnected_t

  • IP_EVENT_*:如 IP_EVENT_STA_GOT_IP 对应 ip_event_got_ip_t

  • SC_EVENT_*(SmartConfig):如 SC_EVENT_GOT_SSID_PSWD 对应 smartconfig_event_got_ssid_pswd_t,SC_EVENT_SEND_ACK_DONE 一般无数据

    #include <stdio.h>
    #include "esp_wifi.h"
    #include "nvs_flash.h"
    #include "esp_event.h"
    #include "esp_err.h"
    #include "esp_log.h"
    #include <string.h>
    #include "esp_smartconfig.h"

    #define TAG "STA"
    #define TAG2 "IP"

    void wifi_cb(void* event_handler_arg,esp_event_base_t event_base,int32_t event_id,void* event_data){
    if(event_base==WIFI_EVENT){
    switch (event_id)
    {
    case WIFI_EVENT_STA_START://开启sta模式成功
    esp_wifi_connect();//链接路由器
    break;
    case WIFI_EVENT_STA_CONNECTED://成功连接到路由器
    ESP_LOGI(TAG,"esp32s3 connected wifi");
    case WIFI_EVENT_STA_DISCONNECTED://断开连接
    ESP_LOGI(TAG,"esp32s3 disconnected wifi");
    esp_wifi_connect();
    default:
    break;
    }
    }
    else if (event_base==IP_EVENT)
    {
    switch (event_id)
    {
    case IP_EVENT_STA_GOT_IP:
    ESP_LOGI(TAG2,"esp32 got ip,connnecet success");
    break;

    复制代码
          default:
              break;
          }
      }
    
      else if (event_base==SC_EVENT)
      {
          switch (event_id)
          {
          case SC_EVENT_GOT_SSID_PSWD://获得wifi的账号和密码
          //类型转换,告诉编辑器是哪一个数据类型,不同事件对应不同的数据类型,void*数据类型不能直接访问成员数据
          //得到数据类型后按照结构体定义的顺序进行偏移
              smartconfig_event_got_ssid_pswd_t *evt=(smartconfig_event_got_ssid_pswd_t *)event_data;
              wifi_config_t wifi_config={0};
              memset(&wifi_config,0,sizeof(wifi_config));
              //char*加上数组可以返回数组的地址
              snprintf((char*)wifi_config.sta.ssid,sizeof(wifi_config.sta.ssid),"%s",(char*)evt->ssid);
              snprintf((char*)wifi_config.sta.password,sizeof(wifi_config.sta.password),"%s",(char*)evt->password);
              wifi_config.sta.bssid_set=evt->bssid_set;
              if(wifi_config.sta.bssid_set){
                  memcpy(wifi_config.sta.bssid,evt->bssid,6);
              }
              esp_wifi_disconnect();
              //选择网卡和配置wifi
              esp_wifi_set_config(WIFI_IF_STA,&wifi_config);
              esp_wifi_connect();
              break;
          case SC_EVENT_SEND_ACK_DONE://发送应答完成
              esp_smartconfig_stop();//结束smart_config配网
              break;
          default:
              break;
          }
      }

    }

    void app_main(void)
    {
    //初始化nvs,后续会用来保存wifi的账号密码
    nvs_flash_init();
    //初始化网络接口(TCP/IP 协议栈相关)
    esp_netif_init();
    //创建创建默认事件循环机制
    esp_event_loop_create_default();
    //创建默认的 STA(站点)网络接口对象。
    esp_netif_create_default_wifi_sta();
    //按照默认配置,初始化wifi的操作
    wifi_init_config_t cfg=WIFI_INIT_CONFIG_DEFAULT();
    esp_wifi_init(&cfg);
    //向事件循环里面加入回调函数,有对应事件发生时会自动调用回调函数
    esp_event_handler_register(WIFI_EVENT,ESP_EVENT_ANY_ID,wifi_cb,NULL);//sta,数据链路层·
    //获取到ip,网络层
    esp_event_handler_register(IP_EVENT,IP_EVENT_GOT_IP6,wifi_cb,NULL);
    //捕获所有的关于smartconfig的事件
    esp_event_handler_register(SC_EVENT,ESP_EVENT_ANY_ID,wifi_cb,NULL);

    复制代码
      esp_wifi_set_mode(WIFI_MODE_STA);
      //启动 Wi‑Fi,开始连接流程
      esp_wifi_start();
      smartconfig_start_config_t cfg2=SMARTCONFIG_START_CONFIG_DEFAULT();
      esp_smartconfig_start(&cfg2);

    }

相关推荐
QCzblack44 分钟前
见面考复现
网络
Eric.Lee20212 小时前
查看ubuntu机器正在使用的网络端口
网络·ubuntu·php
Zero-Talent2 小时前
TCP/IP协议
运维·服务器·网络
Du_chong_huan2 小时前
1.7 计算机网络和因特网的历史 | 《计算机网络:自顶向下方法》精读版
运维·服务器·网络
Java成神之路-3 小时前
DNS 与 CDN 底层原理深度剖析:从域名解析到内容分发全链路解析
网络·网络协议·tcp/ip
AI浩3 小时前
UCAN:用于轻量级超分辨率中扩展感受野的统一卷积注意力网络
网络
echome8884 小时前
Python 异步编程实战:asyncio 核心概念与最佳实践
开发语言·网络·python
Predestination王瀞潞4 小时前
5.4.3 通信->WWW万维网内容访问标准(W3C):WWW(World Wide Web) 协议架构(分层)
前端·网络·网络协议·架构·www
喵喵爱自由4 小时前
Docker容器共享宿主机-安全网络
网络·安全·docker
星爷AG I4 小时前
15-6 威胁性信息(AGI基础理论)
网络·agi