2017-04-01 10:37:58 -04:00
|
|
|
/* 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 <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "freertos/event_groups.h"
|
|
|
|
#include "esp_wifi.h"
|
|
|
|
#include "esp_wpa2.h"
|
2018-11-20 05:26:53 -05:00
|
|
|
#include "esp_event.h"
|
2017-04-01 10:37:58 -04:00
|
|
|
#include "esp_log.h"
|
|
|
|
#include "esp_system.h"
|
|
|
|
#include "nvs_flash.h"
|
2019-09-01 12:25:23 -04:00
|
|
|
#include "esp_netif.h"
|
2017-04-01 10:37:58 -04:00
|
|
|
#include "esp_smartconfig.h"
|
|
|
|
|
|
|
|
/* FreeRTOS event group to signal when we are connected & ready to make a request */
|
2018-08-29 08:48:16 -04:00
|
|
|
static EventGroupHandle_t s_wifi_event_group;
|
2017-04-01 10:37:58 -04:00
|
|
|
|
|
|
|
/* 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;
|
2019-04-16 23:44:51 -04:00
|
|
|
static const char *TAG = "smartconfig_example";
|
2017-04-01 10:37:58 -04:00
|
|
|
|
2018-11-20 05:26:53 -05:00
|
|
|
static void smartconfig_example_task(void * parm);
|
2017-04-01 10:37:58 -04:00
|
|
|
|
2018-11-20 05:26:53 -05:00
|
|
|
static void event_handler(void* arg, esp_event_base_t event_base,
|
|
|
|
int32_t event_id, void* event_data)
|
2017-04-01 10:37:58 -04:00
|
|
|
{
|
2018-11-20 05:26:53 -05:00
|
|
|
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
|
2017-04-01 10:37:58 -04:00
|
|
|
xTaskCreate(smartconfig_example_task, "smartconfig_example_task", 4096, NULL, 3, NULL);
|
2018-11-20 05:26:53 -05:00
|
|
|
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
|
2017-04-01 10:37:58 -04:00
|
|
|
esp_wifi_connect();
|
2018-08-29 08:48:16 -04:00
|
|
|
xEventGroupClearBits(s_wifi_event_group, CONNECTED_BIT);
|
2018-11-20 05:26:53 -05:00
|
|
|
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
|
|
|
|
xEventGroupSetBits(s_wifi_event_group, CONNECTED_BIT);
|
2019-04-16 23:44:51 -04:00
|
|
|
} 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() );
|
2021-01-26 21:54:06 -05:00
|
|
|
ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
|
2021-01-15 07:00:40 -05:00
|
|
|
esp_wifi_connect();
|
2019-04-16 23:44:51 -04:00
|
|
|
} else if (event_base == SC_EVENT && event_id == SC_EVENT_SEND_ACK_DONE) {
|
|
|
|
xEventGroupSetBits(s_wifi_event_group, ESPTOUCH_DONE_BIT);
|
2017-04-01 10:37:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void initialise_wifi(void)
|
|
|
|
{
|
2019-11-29 04:54:02 -05:00
|
|
|
ESP_ERROR_CHECK(esp_netif_init());
|
2018-08-29 08:48:16 -04:00
|
|
|
s_wifi_event_group = xEventGroupCreate();
|
2018-11-20 05:26:53 -05:00
|
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
2019-10-17 05:36:34 -04:00
|
|
|
esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta();
|
|
|
|
assert(sta_netif);
|
2017-08-01 10:29:16 -04:00
|
|
|
|
2017-04-01 10:37:58 -04:00
|
|
|
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
|
|
|
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
|
2018-11-20 05:26:53 -05:00
|
|
|
|
|
|
|
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) );
|
2019-04-16 23:44:51 -04:00
|
|
|
ESP_ERROR_CHECK( esp_event_handler_register(SC_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL) );
|
2018-11-20 05:26:53 -05:00
|
|
|
|
2017-04-01 10:37:58 -04:00
|
|
|
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
|
|
|
|
ESP_ERROR_CHECK( esp_wifi_start() );
|
|
|
|
}
|
|
|
|
|
2018-11-20 05:26:53 -05:00
|
|
|
static void smartconfig_example_task(void * parm)
|
2017-04-01 10:37:58 -04:00
|
|
|
{
|
|
|
|
EventBits_t uxBits;
|
|
|
|
ESP_ERROR_CHECK( esp_smartconfig_set_type(SC_TYPE_ESPTOUCH) );
|
2019-04-16 23:44:51 -04:00
|
|
|
smartconfig_start_config_t cfg = SMARTCONFIG_START_CONFIG_DEFAULT();
|
|
|
|
ESP_ERROR_CHECK( esp_smartconfig_start(&cfg) );
|
2017-04-01 10:37:58 -04:00
|
|
|
while (1) {
|
2018-08-29 08:48:16 -04:00
|
|
|
uxBits = xEventGroupWaitBits(s_wifi_event_group, CONNECTED_BIT | ESPTOUCH_DONE_BIT, true, false, portMAX_DELAY);
|
2017-04-01 10:37:58 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
void app_main(void)
|
2017-04-01 10:37:58 -04:00
|
|
|
{
|
|
|
|
ESP_ERROR_CHECK( nvs_flash_init() );
|
|
|
|
initialise_wifi();
|
|
|
|
}
|
|
|
|
|