2020-06-08
上周五开始搞smartconfig,遇到问题,获取到正确的ssid和password之后,wifi始终连不上。
在网上找了ESP32 LyraT V4.3的软件,但是没找到。然后翻到一个帖子,说官方adf里面有smartconfig的例程。二话不说,翻了一些文件夹,确实有。
cd examples/wifi/smart_config
make all
make flash 报错了。然后make clean清除了旧的数据。重新make all
ok了
开心啊,折腾了几天终于能连上wifi了。
之前在开源一小步上例程移植的一直连不上。晚点再研究一下。。总之很开心
2020-06-08晚
将官方的例程整个.c文件的内容拷贝到我自己新建的工程上,编译,还是不ok。无语,估计前面我自己新建的工程哪里有问题,和原来的代码没太大关系。。。明天继续努力!
2020-06-09
今天直接将官方idf里面smartconfig_main.c拷贝到我自己的工程里面,编译,测试ok。把其他功能移植回来,程序都正常了。附上我的例程代码
/* 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
#include
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_wifi.h"
#include "esp_wpa2.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "esp_system.h"
#include "nvs_flash.h"
#include "tcpip_adapter.h"
#include "esp_smartconfig.h"
#define LED_IO 22
#define KEY_REC_IO 36
#define KEY_MODE_IO 39
uint8_t key_value = 0;
uint16_t timer_ms_cnt = 0;
uint16_t timer_1s_cnt = 0;
uint8_t led_flag = 0;
//定时器句柄
esp_timer_handle_t fw_timer_handle = 0;
void read_key(void);
void fw_timer_cb(void *arg);
/* FreeRTOS event group to signal when we are connected & ready to make a request */
static EventGroupHandle_t 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;
static const char *TAG = "sc";
void smartconfig_example_task(void * parm);
static esp_err_t event_handler(void *ctx, system_event_t *event)
{
switch(event->event_id) {
case SYSTEM_EVENT_STA_START:
xTaskCreate(smartconfig_example_task, "smartconfig_example_task", 4096, NULL, 3, NULL);
break;
case SYSTEM_EVENT_STA_GOT_IP:
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP");
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_DISCONNECTED");
esp_wifi_connect();
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
break;
default:
break;
}
return ESP_OK;
}
static void initialise_wifi(void)
{
tcpip_adapter_init();
wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
ESP_ERROR_CHECK( esp_wifi_start() );
}
static void sc_callback(smartconfig_status_t status, void *pdata)
{
switch (status) {
case SC_STATUS_WAIT:
ESP_LOGI(TAG, "SC_STATUS_WAIT");
break;
case SC_STATUS_FIND_CHANNEL:
ESP_LOGI(TAG, "SC_STATUS_FINDING_CHANNEL");
break;
case SC_STATUS_GETTING_SSID_PSWD:
ESP_LOGI(TAG, "SC_STATUS_GETTING_SSID_PSWD");
break;
case SC_STATUS_LINK:
ESP_LOGI(TAG, "SC_STATUS_LINK");
wifi_config_t *wifi_config = pdata;
ESP_LOGI(TAG, "SSID:%s", wifi_config->sta.ssid);
ESP_LOGI(TAG, "PASSWORD:%s", wifi_config->sta.password);
ESP_ERROR_CHECK( esp_wifi_disconnect() );
ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, wifi_config) );
ESP_ERROR_CHECK( esp_wifi_connect() );
break;
case SC_STATUS_LINK_OVER:
ESP_LOGI(TAG, "SC_STATUS_LINK_OVER");
if (pdata != NULL) {
uint8_t phone_ip[4] = { 0 };
memcpy(phone_ip, (uint8_t* )pdata, 4);
ESP_LOGI(TAG, "Phone ip: %d.%d.%d.%d\n", phone_ip[0], phone_ip[1], phone_ip[2], phone_ip[3]);
}
xEventGroupSetBits(wifi_event_group, ESPTOUCH_DONE_BIT);
break;
default:
break;
}
}
void smartconfig_example_task(void * parm)
{
EventBits_t uxBits;
ESP_ERROR_CHECK( esp_smartconfig_set_type(SC_TYPE_ESPTOUCH) );
ESP_ERROR_CHECK( esp_smartconfig_start(sc_callback) );
while (1) {
uxBits = xEventGroupWaitBits(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()
{
printf("app_main!\n");
//选择 IO
gpio_pad_select_gpio(LED_IO);
gpio_pad_select_gpio(KEY_REC_IO);
gpio_pad_select_gpio(KEY_MODE_IO);
//设置 IO 为输出
gpio_set_direction(LED_IO, GPIO_MODE_OUTPUT);
gpio_set_level(LED_IO, 1);//不亮
//设置按键 IO 输入
gpio_set_direction(KEY_REC_IO, GPIO_MODE_INPUT);
gpio_set_direction(KEY_MODE_IO, GPIO_MODE_INPUT);
//定时器结构体初始化
esp_timer_create_args_t fw_timer =
{
.callback = &fw_timer_cb, //回调函数
.arg = NULL, //参数
.name = "fw_timer" //定时器名称
};
//定时器创建、启动
esp_err_t err = esp_timer_create(&fw_timer, &fw_timer_handle);
err = esp_timer_start_periodic(fw_timer_handle, 1 * 1000);//1ms回调
if(err == ESP_OK)
{
printf("fw timer cteate and start ok!\r\n");
}
ESP_ERROR_CHECK( nvs_flash_init() );
initialise_wifi();
while(1)
{
if(timer_ms_cnt > 10) //10ms
{
timer_ms_cnt -= 10;
read_key();
}
if(timer_1s_cnt >= 1000) //1s
{
timer_1s_cnt -= 1000;
printf("key_value = 0x%.2x \n",key_value);
if(led_flag)
{
led_flag = 0;
gpio_set_level(LED_IO, 1); //led亮
}
else
{
led_flag = 1;
gpio_set_level(LED_IO, 0); //led灭
}
}
}
}
void read_key(void)
{
static uint8_t cnt1 = 50;
static uint8_t cnt2 = 50;
if(gpio_get_level(KEY_REC_IO)==0)//按键按下
cnt1++;
else
cnt1--;
if(gpio_get_level(KEY_MODE_IO)==0)//按键按下
cnt2++;
else
cnt2--;
if(cnt1 > 52)
{
cnt1 = 50;
key_value |= 0x01;
}
else if(cnt1 < 48)
{
cnt1 = 50;
key_value &= ~0x01;
}
if(cnt2 > 52)
{
cnt2 = 50;
key_value |= 0x02;
}
else if(cnt2 < 48)
{
cnt2 = 50;
key_value &= ~0x02;
}
}
void fw_timer_cb(void *arg)
{
timer_ms_cnt++;
timer_1s_cnt++;
}