diff --git a/examples/wifi/smart_config/Makefile b/examples/wifi/smart_config/Makefile new file mode 100644 index 000000000..5f5583121 --- /dev/null +++ b/examples/wifi/smart_config/Makefile @@ -0,0 +1,9 @@ +# +# This is a project Makefile. It is assumed the directory this Makefile resides in is a +# project subdirectory. +# + +PROJECT_NAME := smart_config + +include $(IDF_PATH)/make/project.mk + diff --git a/examples/wifi/smart_config/README.md b/examples/wifi/smart_config/README.md new file mode 100644 index 000000000..9453edbb6 --- /dev/null +++ b/examples/wifi/smart_config/README.md @@ -0,0 +1,42 @@ +# smartconfig Example + +This example shows how ESP32 connects to AP with ESPTOUCH. Example does the following steps: + +* Download ESPTOUCH APP from app store. [Android source code](https://github.com/EspressifApp/EsptouchForAndroid) and [iOS source code](https://github.com/EspressifApp/EsptouchForIOS) is available. + +* Compile this example and upload it to an ESP32. + +* Make sure your phone connect to target AP (2.4GHz). + +* Open ESPTOUCH app and input password. There will be success message after few sec. + +### Example output + +Here is an example of smartconfig console output. +``` +I (372) wifi: mode : sta (24:0a:c4:00:44:86) +I (422) smartconfig: SC version: V2.6.6 +I (3802) wifi: ic_enable_sniffer +I (3802) sc: SC_STATUS_FIND_CHANNEL +I (234592) smartconfig: TYPE: ESPTOUCH +I (234592) smartconfig: T|PHONE MAC:68:3e:34:88:59:bf +I (234592) smartconfig: T|AP MAC:a4:56:02:47:30:07 +I (234592) sc: SC_STATUS_GETTING_SSID_PSWD +I (239922) smartconfig: T|pswd: 123456789 +I (239922) smartconfig: T|ssid: IOT_DEMO_TEST +I (239922) smartconfig: T|bssid: a4:56:02:47:30:07 +I (239922) wifi: ic_disable_sniffer +I (239922) sc: SC_STATUS_LINK +I (239932) sc: SSID:IOT_DEMO_TEST +I (239932) sc: PASSWORD:123456789 +I (240062) wifi: n:1 0, o:1 0, ap:255 255, sta:1 0, prof:1 +I (241042) wifi: state: init -> auth (b0) +I (241042) wifi: state: auth -> assoc (0) +I (241052) wifi: state: assoc -> run (10) +I (241102) wifi: connected with IOT_DEMO_TEST, channel 1 +I (244892) event: ip: 192.168.0.152, mask: 255.255.255.0, gw: 192.168.0.1 +I (244892) sc: WiFi Connected to ap +I (247952) sc: SC_STATUS_LINK_OVER +I (247952) sc: Phone ip: 192.168.0.31 +I (247952) sc: smartconfig over +``` diff --git a/examples/wifi/smart_config/main/component.mk b/examples/wifi/smart_config/main/component.mk new file mode 100644 index 000000000..17fe35eb4 --- /dev/null +++ b/examples/wifi/smart_config/main/component.mk @@ -0,0 +1,6 @@ +# +# "main" pseudo-component makefile. +# +# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.) + + diff --git a/examples/wifi/smart_config/main/smartconfig_main.c b/examples/wifi/smart_config/main/smartconfig_main.c new file mode 100644 index 000000000..63af25e65 --- /dev/null +++ b/examples/wifi/smart_config/main/smartconfig_main.c @@ -0,0 +1,124 @@ +/* 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" + +/* 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: + xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); + break; + case 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() +{ + ESP_ERROR_CHECK( nvs_flash_init() ); + initialise_wifi(); +} +