diff -r 2cf4b15895f2 -r 1ab1f4a8c328 main/co2meter.c --- a/main/co2meter.c Sat Jan 25 12:02:22 2020 +0100 +++ b/main/co2meter.c Sat Mar 14 13:07:02 2020 +0100 @@ -23,6 +23,7 @@ extern SemaphoreHandle_t xSemaphoreDS18B20; ///< DS18B20 lock semaphore extern ADC_State *adc_state; ///< ADC state extern SemaphoreHandle_t xSemaphoreADC; ///< ADC lock semaphore +extern SemaphoreHandle_t xSemaphoreWiFi; extern WIFI_State *wifi_state; ///< WiFi state extern EventGroupHandle_t xEventGroupUser; extern int count_pub; ///< Published MQTT messages in transit @@ -122,11 +123,23 @@ xTaskCreate(&task_user, "task_user", 4096, NULL,10, &xTaskUser); xTaskCreate(&task_ds18b20, "task_ds18b20", 2560, NULL, 8, &xTaskDS18B20); xTaskCreate(&task_adc, "task_adc", 2560, NULL, 8, &xTaskADC); + esp_log_level_set("MQTT_CLIENT", ESP_LOG_ERROR); + xTaskCreate(&task_mqtt, "task_mqtt", 4096, NULL, 5, &xTaskMQTT); esp_log_level_set("wifi", ESP_LOG_ERROR); xTaskCreate(&task_wifi, "task_wifi", 4096, NULL, 3, &xTaskWifi); vTaskDelay(10 / portTICK_PERIOD_MS); - esp_log_level_set("MQTT_CLIENT", ESP_LOG_ERROR); - xTaskCreate(&task_mqtt, "task_mqtt", 4096, NULL, 5, &xTaskMQTT); + int wait = 150; + while (wait) { + if (ready_WiFi()) { + ESP_LOGI(TAG, "Online in %.1f seconds", (150 - wait) / 10.0); + wait = 0; + } else { + wait--; + if (wait < 1) + ESP_LOGE(TAG, "Timeout network connection"); + } + vTaskDelay(100 / portTICK_PERIOD_MS); + } /* * Main application loop. @@ -139,8 +152,8 @@ while (1) { switch (Main_Loop1) { case ML1_INIT: + status_WiFi(); Main_Loop1 = ML1_CONNECT; - requestWiFi_system(true); request_ds18b20(); request_adc(); break; @@ -149,12 +162,13 @@ if (ready_WiFi()) { Main_Loop1 = ML1_MQTT_CONNECT; user_refresh(); + if (! ready_mqtt()) + connect_mqtt(true); } break; case ML1_MQTT_CONNECT: if (ready_ds18b20() && ready_adc()) { - connect_mqtt(true); Main_Loop1 = ML1_WAITCON; uint32_t temp[DS18B20_MAX]; @@ -242,27 +256,10 @@ break; case ML1_WAITACK: - if (count_pub == 0) // Wait until all published messages are sent. - Main_Loop1 = ML1_MQTT_DISCONNECT; - break; - - case ML1_MQTT_DISCONNECT: - connect_mqtt(false); // Doesn't really disconnect. - Main_Loop1 = ML1_DISCONNECT; - break; - - case ML1_DISCONNECT: - if (! ready_mqtt()) { - requestWiFi_system(false); - Main_Loop1 = ML1_WIFI_OFF; - } - break; - - case ML1_WIFI_OFF: - if (! ready_WiFi()) { + if (count_pub == 0) { // Wait until all published messages are sent. ESP_LOGI(TAG, "Main loop: Done, user busy: %s", user_busy() ? "true":"false"); - Main_Loop1 = ML1_DONE; - user_refresh(); + Main_Loop1 = ML1_DONE; + user_refresh(); } break;