main/task_wifi.c

changeset 15
64028e178ff1
parent 9
1659bd3c7a2b
child 18
12506716211c
--- a/main/task_wifi.c	Tue Apr 04 14:33:26 2023 +0200
+++ b/main/task_wifi.c	Tue Apr 04 20:57:49 2023 +0200
@@ -42,7 +42,7 @@
 
 bool ready_WiFi(void)
 {
-    if (wifi_state->STA_connected && wifi_state->STA_online)
+    if ((xEventGroupGetBits(xEventGroupWifi) & (TASK_WIFI_STA_CONNECTED | TASK_WIFI_HAS_IP)) == (TASK_WIFI_STA_CONNECTED | TASK_WIFI_HAS_IP))
         return true;
     return false;
 }
@@ -72,7 +72,6 @@
 #ifdef CONFIG_CODE_TESTING
 		ESP_ERROR_CHECK(esp_netif_set_hostname(sta_netif, "wemos"));
 #endif
-//        	ESP_ERROR_CHECK(esp_wifi_connect());
 		break;
 
 	case WIFI_EVENT_STA_CONNECTED: {
@@ -93,9 +92,11 @@
 	    }
 
 	case WIFI_EVENT_STA_DISCONNECTED: {
-		ESP_LOGI(TAG, "Event STA disconnected");
-//		wifi_event_sta_disconnected_t* disconnected = (wifi_event_sta_disconnected_t*) event_data;
-//                ESP_LOGI(TAG, "Event STA disconnected, reason:%d", disconnected->reason);
+		wifi_event_sta_disconnected_t* disconnected = (wifi_event_sta_disconnected_t*) event_data;
+
+                ESP_LOGI(TAG, "Event STA disconnected, reason: %d", disconnected->reason);
+		if (disconnected->reason != 8)
+		    request_mqtt(false);
                 if (xSemaphoreTake(xSemaphoreWiFi, 35) == pdTRUE) {
                     wifi_state->STA_connected = false;
                     wifi_state->STA_online = false;
@@ -103,8 +104,6 @@
                 } else {
 		    ESP_LOGE(TAG, "wifi_event_handler() lock error WIFI_EVENT_STA_DISCONNECTED");
 		}
-		if (ready_mqtt())
-		    connect_mqtt(false);
                 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED);
                 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED);
 		break;
@@ -135,7 +134,6 @@
 		} else {
 			ESP_LOGE(TAG, "got_ip_event_handler() lock error IP_EVENT_STA_GOT_IP");
 		}
-		connect_mqtt(true);
 		break;
 
 	case IP_EVENT_STA_LOST_IP:
@@ -150,8 +148,6 @@
 		} else {
 			ESP_LOGE(TAG, "got_ip_event_handler() lock error IP_EVENT_STA_LOST_IP");
 		}
-		if (ready_mqtt())
-		    connect_mqtt(false);
 		break;
 
 	default:
@@ -215,8 +211,6 @@
 	     * user requested a disconnect, this will in effect disconnect the wifi
 	     */
 	    ESP_LOGI(TAG, "Request STA disconnect");
-	    connect_mqtt(false);
-	    wait_mqtt(10000);
 	    ESP_ERROR_CHECK(esp_wifi_disconnect());
 	    xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED, pdFALSE, pdTRUE, portMAX_DELAY );
 

mercurial