scan and connect logic

This commit is contained in:
Morgan Allen 2018-10-09 21:23:21 -07:00
parent 48d61d1807
commit ffb505e516
1 changed files with 45 additions and 7 deletions

View File

@ -64,16 +64,50 @@ static esp_err_t wifi_event_handler(void *ctx, system_event_t *event) {
case SYSTEM_EVENT_STA_DISCONNECTED:
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_DISCONNECTED");
xEventGroupClearBits(wm_event_group, WIFI_CONNECTED);
xEventGroupSetBits(wm_event_group, WIFI_IDLE);
} else {
wifi_manager_connect();
}
break;
case SYSTEM_EVENT_SCAN_DONE:
ESP_LOGI(TAG, "SYSTEM_EVENT_SCAN_DONE");
uint8_t i = 0;
uint8_t j = 0;
uint16_t ap_count;
wifi_ap_record_t *records;
xEventGroupClearBits(wm_event_group, WIFI_SCANNING);
ESP_ERROR_CHECK(esp_wifi_scan_stop());
ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&ap_count));
if(ap_count > 0) {
records = malloc(ap_count * sizeof(wifi_ap_record_t));
ESP_LOGI(TAG, "Found %d APs", ap_count);
ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&ap_count, (wifi_ap_record_t *)records));
for(i = 0; i < ap_count; i++) {
ESP_LOGI(TAG, "AP: %s", records[i].ssid);
}
for(i = 0; i < ap_count; i++) {
ESP_LOGI(TAG, "AP: %s", records[i].ssid);
for(j = 0; j < ap_store.count; j++)
if(strcmp(&records[i].ssid, &ap_store.aps[j]) == 0) {
ap_store.last = j;
goto found;
}
}
found:
ESP_LOGI(TAG, "Got matching ESSID: %s", ap_store.aps[ap_store.last].ssid);
xEventGroupClearBits(wm_event_group, WIFI_SCANNING);
xEventGroupSetBits(wm_event_group, WIFI_IDLE);
wifi_manager_connect();
} else {
ESP_LOGI(TAG, "No APs found");
}
default:
break;
}
@ -134,11 +168,15 @@ void wifi_manager_main_loop() {
while(true) {
EventBits_t ev_bits = xEventGroupGetBits(wm_event_group);
ESP_LOGI(TAG, "ev_bits: 0x%02x", ev_bits);
if(ev_bits & AP_AVAILABLE &&
((ev_bits & (WIFI_SCANNING|WIFI_CONNECTED|WIFI_CONNECTING)) == 0)
) {
ESP_LOGI(TAG, "AP Available, starting WiFi connect");
wifi_manager_scan();
vTaskDelay(10000 / portTICK_PERIOD_MS);
}
vTaskDelay(3000 / portTICK_PERIOD_MS);