1 /* Esptouch example
2 
3    This example code is in the Public Domain (or CC0 licensed, at your option.)
4 
5    Unless required by applicable law or agreed to in writing, this
6    software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
7    CONDITIONS OF ANY KIND, either express or implied.
8 */
9 
10 #include <string.h>
11 #include <stdlib.h>
12 #include "freertos/FreeRTOS.h"
13 #include "freertos/task.h"
14 #include "freertos/event_groups.h"
15 #include "esp_wifi.h"
16 #include "esp_wpa2.h"
17 #include "esp_event.h"
18 #include "esp_log.h"
19 #include "esp_system.h"
20 #include "nvs_flash.h"
21 #include "esp_netif.h"
22 #include "esp_smartconfig.h"
23 
24 /* FreeRTOS event group to signal when we are connected & ready to make a request */
25 static EventGroupHandle_t s_wifi_event_group;
26 
27 /* The event group allows multiple bits for each event,
28    but we only care about one event - are we connected
29    to the AP with an IP? */
30 static const int CONNECTED_BIT = BIT0;
31 static const int ESPTOUCH_DONE_BIT = BIT1;
32 static const char *TAG = "smartconfig_example";
33 
34 static void smartconfig_example_task(void * parm);
35 
event_handler(void * arg,esp_event_base_t event_base,int32_t event_id,void * event_data)36 static void event_handler(void* arg, esp_event_base_t event_base,
37                                 int32_t event_id, void* event_data)
38 {
39     if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
40         xTaskCreate(smartconfig_example_task, "smartconfig_example_task", 4096, NULL, 3, NULL);
41     } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
42         esp_wifi_connect();
43         xEventGroupClearBits(s_wifi_event_group, CONNECTED_BIT);
44     } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
45         xEventGroupSetBits(s_wifi_event_group, CONNECTED_BIT);
46     } else if (event_base == SC_EVENT && event_id == SC_EVENT_SCAN_DONE) {
47         ESP_LOGI(TAG, "Scan done");
48     } else if (event_base == SC_EVENT && event_id == SC_EVENT_FOUND_CHANNEL) {
49         ESP_LOGI(TAG, "Found channel");
50     } else if (event_base == SC_EVENT && event_id == SC_EVENT_GOT_SSID_PSWD) {
51         ESP_LOGI(TAG, "Got SSID and password");
52 
53         smartconfig_event_got_ssid_pswd_t *evt = (smartconfig_event_got_ssid_pswd_t *)event_data;
54         wifi_config_t wifi_config;
55         uint8_t ssid[33] = { 0 };
56         uint8_t password[65] = { 0 };
57         uint8_t rvd_data[33] = { 0 };
58 
59         bzero(&wifi_config, sizeof(wifi_config_t));
60         memcpy(wifi_config.sta.ssid, evt->ssid, sizeof(wifi_config.sta.ssid));
61         memcpy(wifi_config.sta.password, evt->password, sizeof(wifi_config.sta.password));
62         wifi_config.sta.bssid_set = evt->bssid_set;
63         if (wifi_config.sta.bssid_set == true) {
64             memcpy(wifi_config.sta.bssid, evt->bssid, sizeof(wifi_config.sta.bssid));
65         }
66 
67         memcpy(ssid, evt->ssid, sizeof(evt->ssid));
68         memcpy(password, evt->password, sizeof(evt->password));
69         ESP_LOGI(TAG, "SSID:%s", ssid);
70         ESP_LOGI(TAG, "PASSWORD:%s", password);
71         if (evt->type == SC_TYPE_ESPTOUCH_V2) {
72             ESP_ERROR_CHECK( esp_smartconfig_get_rvd_data(rvd_data, sizeof(rvd_data)) );
73             ESP_LOGI(TAG, "RVD_DATA:");
74             for (int i=0; i<33; i++) {
75                 printf("%02x ", rvd_data[i]);
76             }
77             printf("\n");
78         }
79 
80         ESP_ERROR_CHECK( esp_wifi_disconnect() );
81         ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
82         esp_wifi_connect();
83     } else if (event_base == SC_EVENT && event_id == SC_EVENT_SEND_ACK_DONE) {
84         xEventGroupSetBits(s_wifi_event_group, ESPTOUCH_DONE_BIT);
85     }
86 }
87 
initialise_wifi(void)88 static void initialise_wifi(void)
89 {
90     ESP_ERROR_CHECK(esp_netif_init());
91     s_wifi_event_group = xEventGroupCreate();
92     ESP_ERROR_CHECK(esp_event_loop_create_default());
93     esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta();
94     assert(sta_netif);
95 
96     wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
97     ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
98 
99     ESP_ERROR_CHECK( esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL) );
100     ESP_ERROR_CHECK( esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL) );
101     ESP_ERROR_CHECK( esp_event_handler_register(SC_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL) );
102 
103     ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
104     ESP_ERROR_CHECK( esp_wifi_start() );
105 }
106 
smartconfig_example_task(void * parm)107 static void smartconfig_example_task(void * parm)
108 {
109     EventBits_t uxBits;
110     ESP_ERROR_CHECK( esp_smartconfig_set_type(SC_TYPE_ESPTOUCH) );
111     smartconfig_start_config_t cfg = SMARTCONFIG_START_CONFIG_DEFAULT();
112     ESP_ERROR_CHECK( esp_smartconfig_start(&cfg) );
113     while (1) {
114         uxBits = xEventGroupWaitBits(s_wifi_event_group, CONNECTED_BIT | ESPTOUCH_DONE_BIT, true, false, portMAX_DELAY);
115         if(uxBits & CONNECTED_BIT) {
116             ESP_LOGI(TAG, "WiFi Connected to ap");
117         }
118         if(uxBits & ESPTOUCH_DONE_BIT) {
119             ESP_LOGI(TAG, "smartconfig over");
120             esp_smartconfig_stop();
121             vTaskDelete(NULL);
122         }
123     }
124 }
125 
app_main(void)126 void app_main(void)
127 {
128     ESP_ERROR_CHECK( nvs_flash_init() );
129     initialise_wifi();
130 }
131