14 EventGroupHandle_t xEventGroupWifi; ///< Events WiFi task. |
14 EventGroupHandle_t xEventGroupWifi; ///< Events WiFi task. |
15 uint16_t ap_num = MAX_AP_NUM; ///< Scan counter. |
15 uint16_t ap_num = MAX_AP_NUM; ///< Scan counter. |
16 wifi_ap_record_t *accessp_records; ///< [MAX_AP_NUM] records array with scan results. |
16 wifi_ap_record_t *accessp_records; ///< [MAX_AP_NUM] records array with scan results. |
17 wifi_config_t *task_wifi_ConfigSTA = NULL; ///< Current STA configuration. |
17 wifi_config_t *task_wifi_ConfigSTA = NULL; ///< Current STA configuration. |
18 WIFI_State *wifi_state = NULL; ///< Public state for other tasks. |
18 WIFI_State *wifi_state = NULL; ///< Public state for other tasks. |
|
19 esp_netif_t *sta_netif = NULL; ///< Station interface |
|
20 |
19 |
21 |
20 wifi_scan_config_t scan_config = { |
22 wifi_scan_config_t scan_config = { |
21 .ssid = 0, |
23 .ssid = 0, |
22 .bssid = 0, |
24 .bssid = 0, |
23 .channel = 0, |
25 .channel = 0, |
86 break; |
88 break; |
87 |
89 |
88 case WIFI_EVENT_STA_START: |
90 case WIFI_EVENT_STA_START: |
89 ESP_LOGI(TAG, "Event wifi START"); |
91 ESP_LOGI(TAG, "Event wifi START"); |
90 // Set the configured hostname for the dhcp client. |
92 // Set the configured hostname for the dhcp client. |
91 ESP_ERROR_CHECK(tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA, config.hostname)); |
93 ESP_ERROR_CHECK(esp_netif_set_hostname(sta_netif, config.hostname)); |
92 esp_wifi_connect(); |
94 esp_wifi_connect(); |
93 break; |
95 break; |
94 |
96 |
95 case WIFI_EVENT_STA_CONNECTED: |
97 case WIFI_EVENT_STA_CONNECTED: |
96 { |
98 { |
147 case IP_EVENT_STA_GOT_IP: |
149 case IP_EVENT_STA_GOT_IP: |
148 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_HAS_IP); |
150 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_HAS_IP); |
149 ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; |
151 ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; |
150 if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) { |
152 if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) { |
151 wifi_state->STA_online = true; |
153 wifi_state->STA_online = true; |
152 snprintf(wifi_state->STA_ip, 16, "%s", ip4addr_ntoa(&event->ip_info.ip)); |
154 snprintf(wifi_state->STA_ip, 16, IPSTR, IP2STR(&event->ip_info.ip)); |
153 snprintf(wifi_state->STA_nm, 16, "%s", ip4addr_ntoa(&event->ip_info.netmask)); |
155 snprintf(wifi_state->STA_nm, 16, IPSTR, IP2STR(&event->ip_info.netmask)); |
154 snprintf(wifi_state->STA_gw, 16, "%s", ip4addr_ntoa(&event->ip_info.gw)); |
156 snprintf(wifi_state->STA_gw, 16, IPSTR, IP2STR(&event->ip_info.gw)); |
155 xSemaphoreGive(xSemaphoreWiFi); |
157 xSemaphoreGive(xSemaphoreWiFi); |
156 } else { |
158 } else { |
157 ESP_LOGE(TAG, "got_ip_event_handler() lock error IP_EVENT_STA_GOT_IP"); |
159 ESP_LOGE(TAG, "got_ip_event_handler() lock error IP_EVENT_STA_GOT_IP"); |
158 } |
160 } |
159 connect_mqtt(true); |
161 connect_mqtt(true); |
191 ESP_LOGI(TAG, "Starting WiFi"); |
193 ESP_LOGI(TAG, "Starting WiFi"); |
192 |
194 |
193 /* event handler and event group for the wifi driver */ |
195 /* event handler and event group for the wifi driver */ |
194 xEventGroupWifi = xEventGroupCreate(); |
196 xEventGroupWifi = xEventGroupCreate(); |
195 /* initialize the tcp stack */ |
197 /* initialize the tcp stack */ |
196 tcpip_adapter_init(); |
198 ESP_ERROR_CHECK(esp_netif_init()); |
197 ESP_ERROR_CHECK(esp_event_loop_create_default()); |
199 ESP_ERROR_CHECK(esp_event_loop_create_default()); |
|
200 sta_netif = esp_netif_create_default_wifi_sta(); |
|
201 assert(sta_netif); |
198 |
202 |
199 /* |
203 /* |
200 * memory allocation of objects used by the task |
204 * memory allocation of objects used by the task |
201 */ |
205 */ |
202 accessp_records = (wifi_ap_record_t*)malloc(sizeof(wifi_ap_record_t) * MAX_AP_NUM); |
206 accessp_records = (wifi_ap_record_t*)malloc(sizeof(wifi_ap_record_t) * MAX_AP_NUM); |
205 |
209 |
206 xSemaphoreWiFi = xSemaphoreCreateMutex(); |
210 xSemaphoreWiFi = xSemaphoreCreateMutex(); |
207 wifi_state = malloc(sizeof(WIFI_State)); |
211 wifi_state = malloc(sizeof(WIFI_State)); |
208 memset(wifi_state, 0x00, sizeof(WIFI_State)); |
212 memset(wifi_state, 0x00, sizeof(WIFI_State)); |
209 |
213 |
210 /* stop dhcp server and start dhcp client */ |
|
211 ESP_ERROR_CHECK(tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP)); |
|
212 ESP_ERROR_CHECK(tcpip_adapter_dhcpc_start(TCPIP_ADAPTER_IF_STA)); |
|
213 |
|
214 /* |
214 /* |
215 * init wifi as station |
215 * init wifi as station |
216 */ |
216 */ |
217 wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT(); |
217 wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT(); |
218 ESP_ERROR_CHECK(esp_wifi_init(&wifi_init_config)); |
218 ESP_ERROR_CHECK(esp_wifi_init(&wifi_init_config)); |
219 |
219 |
220 ESP_ERROR_CHECK( esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL) ); |
220 esp_event_handler_instance_t instance_any_id; |
221 ESP_ERROR_CHECK( esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, &got_ip_event_handler, NULL) ); |
221 esp_event_handler_instance_t instance_got_ip; |
|
222 ESP_ERROR_CHECK( esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL, &instance_any_id) ); |
|
223 ESP_ERROR_CHECK( esp_event_handler_instance_register(IP_EVENT, ESP_EVENT_ANY_ID, &got_ip_event_handler, NULL, &instance_got_ip) ); |
222 |
224 |
223 ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); |
225 ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); |
224 ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); |
226 ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); |
225 ESP_ERROR_CHECK(esp_wifi_start()); |
227 ESP_ERROR_CHECK(esp_wifi_start()); |
226 |
228 |