mirror of
https://github.com/espressif/esp-idf.git
synced 2026-05-19 23:45:28 +02:00
esp_wifi: refactor smartconfig callback to use esp event
This commit is contained in:
committed by
Xia Xiaotian
parent
3ff01d60a4
commit
29d1d2bd38
@@ -29,7 +29,7 @@ static EventGroupHandle_t s_wifi_event_group;
|
||||
to the AP with an IP? */
|
||||
static const int CONNECTED_BIT = BIT0;
|
||||
static const int ESPTOUCH_DONE_BIT = BIT1;
|
||||
static const char *TAG = "sc";
|
||||
static const char *TAG = "smartconfig_example";
|
||||
|
||||
static void smartconfig_example_task(void * parm);
|
||||
|
||||
@@ -43,6 +43,36 @@ static void event_handler(void* arg, esp_event_base_t event_base,
|
||||
xEventGroupClearBits(s_wifi_event_group, CONNECTED_BIT);
|
||||
} 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 };
|
||||
|
||||
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));
|
||||
wifi_config.sta.bssid_set = evt->bssid_set;
|
||||
if (wifi_config.sta.bssid_set == true) {
|
||||
memcpy(wifi_config.sta.bssid, evt->bssid, sizeof(wifi_config.sta.bssid));
|
||||
}
|
||||
|
||||
memcpy(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);
|
||||
|
||||
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() );
|
||||
} else if (event_base == SC_EVENT && event_id == SC_EVENT_SEND_ACK_DONE) {
|
||||
xEventGroupSetBits(s_wifi_event_group, ESPTOUCH_DONE_BIT);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,51 +87,18 @@ static void initialise_wifi(void)
|
||||
|
||||
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 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(s_wifi_event_group, ESPTOUCH_DONE_BIT);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static 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) );
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user