Forráskód Böngészése

Deinit components before reboot (#1704)

* Deinit all components before reboot

* Update

* Update
Slider0007 3 éve
szülő
commit
7fa0b87e6e

+ 32 - 25
code/components/jomjol_fileserver_ota/server_ota.cpp

@@ -32,6 +32,8 @@
 #ifdef ENABLE_MQTT
     #include "interface_mqtt.h"
 #endif //ENABLE_MQTT
+#include "ClassControllCamera.h"
+#include "connect_wlan.h"
 
 
 #include "ClassLogFile.h"
@@ -135,8 +137,6 @@ void CheckUpdate()
 }
 
 
-
-
 static bool ota_update_task(std::string fn)
 {
     esp_err_t err;
@@ -289,6 +289,7 @@ static bool diagnostic(void)
     return true;
 }
 
+
 void CheckOTAUpdate(void)
 {
     ESP_LOGI(TAG, "Start CheckOTAUpdateCheck...");
@@ -361,7 +362,6 @@ void CheckOTAUpdate(void)
 }
 
 
-
 esp_err_t handler_ota_update(httpd_req_t *req)
 {
 #ifdef DEBUG_DETAIL_ON     
@@ -398,7 +398,7 @@ esp_err_t handler_ota_update(httpd_req_t *req)
             ESP_LOGD(TAG, "Delete Default File: %s", fn.c_str());
         }
 
-    };
+    }
 
     if (_task.compare("emptyfirmwaredir") == 0)
     {
@@ -578,45 +578,51 @@ esp_err_t handler_ota_update(httpd_req_t *req)
     }
 
     httpd_resp_send(req, resp_str, strlen(resp_str));  
-
-#ifdef DEBUG_DETAIL_ON 
-    LogFile.WriteHeapInfo("handler_ota_update - Done");    
-#endif
 */
 
+    #ifdef DEBUG_DETAIL_ON 
+        LogFile.WriteHeapInfo("handler_ota_update - Done");    
+    #endif
+
     return ESP_OK;
-};
+}
 
-void hard_restart() {
+
+void hard_restart() 
+{
   esp_task_wdt_init(1,true);
   esp_task_wdt_add(NULL);
   while(true);
 }
 
+
 void task_reboot(void *pvParameter)
 {
-    while(1)
-    {
+    KillTFliteTasks();  // Kill autoflow task
+
+    /* Stop service tasks */
+    #ifdef ENABLE_MQTT
+        MQTTdestroy_client(true);
+    #endif //ENABLE_MQTT
+    gpio_handler_destroy();
+    esp_camera_deinit();
+    WIFIDestroy();
+
         vTaskDelay(5000 / portTICK_PERIOD_MS);
-        esp_restart();
-        hard_restart();
-    }
+    esp_restart();      // Reset type: CPU Reset (Reset both CPUs)
+
+    vTaskDelay(5000 / portTICK_PERIOD_MS);
+    hard_restart();     // Reset type: System reset (Triggered by watchdog), if esp_restart stalls (WDT needs to be activated)
 
     vTaskDelete(NULL); //Delete this task if it exits from the loop above
 }
 
-void doReboot(){
+
+void doReboot()
+{
     LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Reboot triggered by Software (5s).");
     LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Reboot in 5sec");
-    xTaskCreate(&task_reboot, "reboot", configMINIMAL_STACK_SIZE * 64, NULL, 10, NULL);
-    // KillTFliteTasks(); // kills itself 
-    gpio_handler_destroy();
-    #ifdef ENABLE_MQTT
-        MQTTdestroy_client();
-    #endif //ENABLE_MQTT
-    vTaskDelay(5000 / portTICK_PERIOD_MS);
-    esp_restart();
-    hard_restart();
+    xTaskCreate(&task_reboot, "task_reboot", 4 * 1024, NULL, 10, NULL);
 }
 
 
@@ -640,6 +646,7 @@ esp_err_t handler_reboot(httpd_req_t *req)
     return ESP_OK;
 }
 
+
 void register_server_ota_sdcard_uri(httpd_handle_t server)
 {
     ESP_LOGI(TAG, "Registering URI handlers");

+ 10 - 7
code/components/jomjol_mqtt/interface_mqtt.cpp

@@ -206,7 +206,7 @@ int MQTT_Init() {
     }
 
     LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Init");
-    MQTTdestroy_client();
+    MQTTdestroy_client(false);
 
     esp_mqtt_client_config_t mqtt_cfg = {
         .uri = uri.c_str(),
@@ -270,7 +270,7 @@ int MQTT_Init() {
 }
 
 
-void MQTTdestroy_client() {
+void MQTTdestroy_client(bool _disable = false) {
     if (client) {
         if (mqtt_connected) {
             MQTTdestroySubscribeFunction();      
@@ -282,6 +282,9 @@ void MQTTdestroy_client() {
         client = NULL;
         mqtt_initialized = false;
     }
+
+    if (_disable) // Disable MQTT service, avoid restart with MQTTPublish
+        mqtt_configOK = false;
 }
 
 
@@ -328,11 +331,11 @@ void MQTTconnected(){
                 LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "topic " + it->first + " subscribe successful, msg_id=" + std::to_string(msg_id));
             }
         }
-    }
-    
-    vTaskDelay(10000 / portTICK_PERIOD_MS);                 // Delay execution of callback routine after connection got established   
-    if (callbackOnConnected) {                              // Call onConnected callback routine --> mqtt_server
-        callbackOnConnected(maintopic, SetRetainFlag);
+
+        vTaskDelay(10000 / portTICK_PERIOD_MS);                 // Delay execution of callback routine after connection got established   
+        if (callbackOnConnected) {                              // Call onConnected callback routine --> mqtt_server
+            callbackOnConnected(maintopic, SetRetainFlag);
+        }
     }
 }
 

+ 1 - 1
code/components/jomjol_mqtt/interface_mqtt.h

@@ -13,7 +13,7 @@ bool MQTT_Configure(std::string _mqttURI, std::string _clientid, std::string _us
                     std::string _maintopic, std::string _lwt, std::string _lwt_connected, std::string _lwt_disconnected,
                     int _keepalive, int SetRetainFlag, void *callbackOnConnected);
 int MQTT_Init();
-void MQTTdestroy_client();
+void MQTTdestroy_client(bool _disable);
 
 bool MQTTPublish(std::string _key, std::string _content, int retained_flag = 1);            // retained Flag as Standart
 

+ 6 - 7
code/components/jomjol_tfliteclass/server_tflite.cpp

@@ -65,17 +65,16 @@ bool isSetupModusActive() {
 void KillTFliteTasks()
 {
     #ifdef DEBUG_DETAIL_ON      
-        ESP_LOGD(TAG, "Handle: xHandletask_autodoFlow: %ld", (long) xHandletask_autodoFlow);
+        ESP_LOGD(TAG, "KillTFliteTasks: xHandletask_autodoFlow: %ld", (long) xHandletask_autodoFlow);
     #endif
-    if (xHandletask_autodoFlow != NULL)
+    if( xHandletask_autodoFlow != NULL )
     {
-        TaskHandle_t xHandletask_autodoFlowTmp = xHandletask_autodoFlow;
+        vTaskDelete(xHandletask_autodoFlow);
         xHandletask_autodoFlow = NULL;
-        vTaskDelete(xHandletask_autodoFlowTmp);
-        #ifdef DEBUG_DETAIL_ON      
-            ESP_LOGD(TAG, "Killed: xHandletask_autodoFlow");
-        #endif
     }
+    #ifdef DEBUG_DETAIL_ON      
+    	ESP_LOGD(TAG, "Killed: xHandletask_autodoFlow");
+    #endif
 }
 
 

+ 32 - 16
code/components/jomjol_wlan/connect_wlan.cpp

@@ -41,17 +41,12 @@
 
 
 /////////////////////
-
-
-
-
 #include "../../include/defines.h"
 
 
 /* FreeRTOS event group to signal when we are connected*/
 static EventGroupHandle_t s_wifi_event_group;
 
-
 static const char *TAG = "WIFI";
 
 static int s_retry_num = 0;
@@ -304,13 +299,12 @@ static void esp_bss_rssi_low_handler(void* arg, esp_event_base_t event_base,
 //////////////////////////////////
 
 
-
-
 std::string* getIPAddress()
 {
     return &ipadress;
 }
 
+
 std::string* getSSID()
 {
     return &ssid;
@@ -353,6 +347,7 @@ void task_doBlink(void *pvParameter)
     vTaskDelete(NULL); //Delete this task if it exits from the loop above
 }
 
+
 void LEDBlinkTask(int _dauer, int _anz, bool _off)
 {
 	BlinkDauer = _dauer;
@@ -363,6 +358,7 @@ void LEDBlinkTask(int _dauer, int _anz, bool _off)
 }
 /////////////////////////////////////////////////////////
 
+
 static void event_handler(void* arg, esp_event_base_t event_base,
                                 int32_t event_id, void* event_data)
 {
@@ -398,6 +394,7 @@ static void event_handler(void* arg, esp_event_base_t event_base,
     }
 }
 
+
 void strinttoip4(const char *ip, int &a, int &b, int &c, int &d) {
     std::string zw = std::string(ip);
     std::stringstream s(zw);
@@ -405,6 +402,7 @@ void strinttoip4(const char *ip, int &a, int &b, int &c, int &d) {
     s >> a >> ch >> b >> ch >> c >> ch >> d;
 }
 
+
 void wifi_init_sta(const char *_ssid, const char *_password, const char *_hostname, const char *_ipadr, const char *_gw,  const char *_netmask, const char *_dns, int _rssithreashold)
 {
 	RSSI_Threshold = _rssithreashold;
@@ -463,13 +461,13 @@ void wifi_init_sta(const char *_ssid, const char *_password, const char *_hostna
                                                         NULL,
                                                         &instance_got_ip));
 
-#ifdef WLAN_USE_MESH_ROAMING
+	#ifdef WLAN_USE_MESH_ROAMING
 	ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, 
                                                         WIFI_EVENT_STA_BSS_RSSI_LOW,
                                                         &esp_bss_rssi_low_handler, 
                                                         NULL,
                                                         &instance_bss_rssi_low));
-#endif
+	#endif
 
     wifi_config_t wifi_config = { };
 
@@ -485,10 +483,10 @@ void wifi_init_sta(const char *_ssid, const char *_password, const char *_hostna
         esp_err_t ret = tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA , _hostname);
         hostname = std::string(_hostname);
         if(ret != ESP_OK ){
-            ESP_LOGE(TAG,"failed to set hostname:%d",ret);  
+            ESP_LOGE(TAG,"Failed to set hostname: %d",ret);  
         }
         else {
-            ESP_LOGI(TAG,"Set Hostname to:%s", _hostname); 
+            ESP_LOGI(TAG,"Set hostname to: %s", _hostname); 
         }
 
     }
@@ -507,15 +505,15 @@ void wifi_init_sta(const char *_ssid, const char *_password, const char *_hostna
      * happened. */
     if (bits & WIFI_CONNECTED_BIT) {
         #ifdef __HIDE_PASSWORD
-            ESP_LOGI(TAG, "connected to ap SSID: %s, password: XXXXXXX", _ssid);
+            ESP_LOGI(TAG, "Connected with AP: %s, password: XXXXXXX", _ssid);
         #else
-            ESP_LOGI(TAG, "connected to ap SSID: %s, password: %s", _ssid, _password);
+            ESP_LOGI(TAG, "Connected with AP: %s, password: %s", _ssid, _password);
         #endif        
     } else if (bits & WIFI_FAIL_BIT) {
         #ifdef __HIDE_PASSWORD
-            ESP_LOGI(TAG, "Failed to connect to SSID: %s, password: XXXXXXXX", _ssid);
+            ESP_LOGI(TAG, "Failed to connect with AP: %s, password: XXXXXXXX", _ssid);
         #else
-            ESP_LOGI(TAG, "Failed to connect to SSID: %s, password: %s", _ssid, _password);
+            ESP_LOGI(TAG, "Failed to connect with AP: %s, password: %s", _ssid, _password);
         #endif        
     } else {
         ESP_LOGE(TAG, "UNEXPECTED EVENT");
@@ -529,6 +527,7 @@ void wifi_init_sta(const char *_ssid, const char *_password, const char *_hostna
 //    vEventGroupDelete(s_wifi_event_group);
 }
 
+
 int get_WIFI_RSSI()
 {
     wifi_ap_record_t ap;
@@ -536,17 +535,34 @@ int get_WIFI_RSSI()
     return ap.rssi;
 }
 
+
 void wifi_init_sta(const char *_ssid, const char *_password, const char *_hostname)
 {
     wifi_init_sta(_ssid, _password, _hostname, NULL, NULL, NULL, NULL, 0);
 }
 
+
 void wifi_init_sta(const char *_ssid, const char *_password)
 {
     wifi_init_sta(_ssid, _password, NULL, NULL, NULL, NULL, NULL, 0);
 }
 
-bool getWIFIisConnected() {
+
+bool getWIFIisConnected() 
+{
     return WIFIConnected;
 }
 
+
+void WIFIDestroy() 
+{	
+    esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, event_handler);
+    esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, event_handler);
+	#ifdef WLAN_USE_MESH_ROAMING
+	esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_BSS_RSSI_LOW, esp_bss_rssi_low_handler);
+	#endif
+	
+	esp_wifi_stop();
+	esp_wifi_deinit();
+}
+

+ 1 - 0
code/components/jomjol_wlan/connect_wlan.h

@@ -13,6 +13,7 @@ std::string* getIPAddress();
 std::string* getSSID();
 int get_WIFI_RSSI();
 bool getWIFIisConnected();
+void WIFIDestroy();
 
 extern std::string hostname;
 extern std::string std_hostname;