提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
ESP32&微信小程序SmartConfig配网
- 前言
- 一、SmartConfig是什么?
- 二、使用乐鑫官方的smart_config例子
- 1.运行照片
- 三、微信小程序
- 总结
前言
本人是酷爱ESP32S3这款单片机,使用它,给我带来很多乐趣,在工作中也帮助了我。
我使用ESP32做了一下功能:
1:esp32&RF433
2:esp32&外部flash
3:esp32&串口屏幕
4:esp32&编码,电流IC
5:esp32&MQTT
6:esp32&微信小程序
提示:今天分享的是个人摸索,结合前辈们的经验修改的案例,实现微信小程序给esp32配网
在这里插入图片描述
一、SmartConfig是什么?
我对它的理解不是熟悉,我只知道有这么一个功能,我想去实现它,我就在网上各种的搜索,群里疯狂的咨询,经过3天的,总与有了一些突破。。
二、使用乐鑫官方的smart_config例子
1.运行照片
代码如下(示例):
/* Esptouch exampleThis example code is in the Public Domain (or CC0 licensed, at your option.)Unless required by applicable law or agreed to in writing, thissoftware is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES ORCONDITIONS 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_eap_client.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 "esp_mac.h"/* 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 connectedto the AP with an IP? */
static const int CONNECTED_BIT = BIT0;
static const int ESPTOUCH_DONE_BIT = BIT1;
static const char *TAG = "smartconfig_example";static void smartconfig_example_task(void *parm);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);wifi_event_sta_disconnected_t *disconnected = (wifi_event_sta_disconnected_t *)event_data;ESP_LOGI(TAG, "Disconnected from AP, reason: %d", disconnected->reason);}else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP){xEventGroupSetBits(s_wifi_event_group, CONNECTED_BIT);}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");}else if (event_base == SC_EVENT && event_id == SC_EVENT_GOT_SSID_PSWD){ESP_LOGI(TAG, "Got SSID and password");smartconfig_event_got_ssid_pswd_t *evt = (smartconfig_event_got_ssid_pswd_t *)event_data;wifi_config_t wifi_config;uint8_t ssid[33] = {0};uint8_t password[65] = {0};uint8_t rvd_data[33] = {0};ESP_LOGI(TAG, "Received SSID: %s", evt->ssid);ESP_LOGI(TAG, "Received Password: %s", evt->password);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));#ifdef CONFIG_SET_MAC_ADDRESS_OF_TARGET_APwifi_config.sta.bssid_set = evt->bssid_set;if (wifi_config.sta.bssid_set == true){ESP_LOGI(TAG, "Set MAC address of target AP: " MACSTR " ", MAC2STR(evt->bssid));memcpy(wifi_config.sta.bssid, evt->bssid, sizeof(wifi_config.sta.bssid));}
#endifmemcpy(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();}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));ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));ESP_ERROR_CHECK(esp_wifi_start());
}static void smartconfig_example_task(void *parm)
{EventBits_t uxBits;ESP_ERROR_CHECK(esp_smartconfig_set_type(SC_TYPE_ESPTOUCH));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");}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());initialise_wifi();
}
三、微信小程序
代码没有上传,是因为个人能力不够,就不发出来,需要的朋友可以@我,可以私发。