main/task_wifi.c

changeset 23
2cc30d828d6e
parent 22
9c0bcc91fe1a
child 24
74609f70411e
equal deleted inserted replaced
22:9c0bcc91fe1a 23:2cc30d828d6e
168 } 168 }
169 169
170 170
171 static void init_wifi(void) 171 static void init_wifi(void)
172 { 172 {
173 ESP_LOGI(TAG, "initialise_wifi() start");
174 ESP_ERROR_CHECK(esp_event_loop_create_default()); 173 ESP_ERROR_CHECK(esp_event_loop_create_default());
175 xSemaphoreWiFi = xSemaphoreCreateMutex(); 174 xSemaphoreWiFi = xSemaphoreCreateMutex();
176 175
177 /* initialize the tcp stack */ 176 /* initialize the tcp stack */
178 ESP_ERROR_CHECK(esp_netif_init()); 177 ESP_ERROR_CHECK(esp_netif_init());
184 183
185 ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL, &instance_any_id) ); 184 ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL, &instance_any_id) );
186 ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, ESP_EVENT_ANY_ID, &got_ip_event_handler, NULL, &instance_got_ip) ); 185 ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, ESP_EVENT_ANY_ID, &got_ip_event_handler, NULL, &instance_got_ip) );
187 186
188 ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); 187 ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
189 ESP_ERROR_CHECK( esp_wifi_start() ); 188 ESP_ERROR_CHECK(esp_wifi_start());
190 189
191 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED); 190 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED);
192 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED); 191 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED);
193
194 ESP_LOGI(TAG, "initialise_wifi() done");
195 } 192 }
196 193
197 194
198 void wifi_connect(void) 195 void wifi_connect(void)
199 { 196 {
200 ESP_LOGI(TAG, "wifi_connect() start"); 197 ESP_LOGI(TAG, "wifi_connect() start");
201 wifi_config_t wifi_Config = { ///< Current STA configuration. 198 wifi_config_t wifi_Config = { ///< Current STA configuration.
202 .sta = { 199 .sta = {
203 .ssid = ESP_WIFI_SSID, 200 .ssid = ESP_WIFI_SSID,
204 .password = ESP_WIFI_PASS, 201 .password = ESP_WIFI_PASS,
202 .threshold.authmode = WIFI_AUTH_WPA2_PSK,
205 }, 203 },
206 }; 204 };
207 // .threshold.authmode = WIFI_AUTH_WPA2_PSK, 205 // .threshold.authmode = WIFI_AUTH_WPA2_PSK,
208 // .pmf_cfg = {
209 // .capable = true,
210 // .required = false
211 // },
212 206
213 ESP_ERROR_CHECK(esp_wifi_disconnect()); 207 ESP_ERROR_CHECK(esp_wifi_disconnect());
214 ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_Config) ); 208 ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_Config) );
215 209
216 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_FAILED); 210 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_FAILED);
228 222
229 void task_wifi( void * pvParameters ) 223 void task_wifi( void * pvParameters )
230 { 224 {
231 uint64_t starttime = 0; 225 uint64_t starttime = 0;
232 226
233 ESP_LOGI(TAG, "Start WiFi"); 227 ESP_LOGI(TAG, "Starting WiFi task");
234 esp_log_level_set("wifi", ESP_LOG_WARNING); 228 esp_log_level_set("wifi", ESP_LOG_ERROR);
235 229
236 /* 230 /*
237 * memory allocation of objects used by the task 231 * memory allocation of objects used by the task
238 */ 232 */
239 wifi_state = malloc(sizeof(WIFI_State)); 233 wifi_state = malloc(sizeof(WIFI_State));
240 memset(wifi_state, 0x00, sizeof(WIFI_State)); 234 memset(wifi_state, 0x00, sizeof(WIFI_State));
241 sprintf(wifi_state->STA_ssid, "%s", ESP_WIFI_SSID);
242 235
243 /* 236 /*
244 * event group for the wifi driver 237 * event group for the wifi driver
245 */ 238 */
246 xEventGroupWifi = xEventGroupCreate(); 239 xEventGroupWifi = xEventGroupCreate();
248 /* 241 /*
249 * init wifi as station 242 * init wifi as station
250 */ 243 */
251 init_wifi(); 244 init_wifi();
252 EventBits_t uxBits; 245 EventBits_t uxBits;
253 246 int8_t tx_level;
254 ESP_LOGI(TAG, "Startup completed, enter task loop"); 247 ESP_ERROR_CHECK(esp_wifi_get_max_tx_power(&tx_level));
248 ESP_LOGI(TAG, "Startup completed, maximum transmit power %d dBm", tx_level / 4);
255 249
256 for(;;) { 250 for(;;) {
257 251
258 /* 252 /*
259 * Actions that can trigger: request a connection or a disconnection 253 * Actions that can trigger: request a connection or a disconnection

mercurial