Deinit components before reboot (#1704)

* Deinit all components before reboot

* Update

* Update
This commit is contained in:
Slider0007
2022-12-27 20:29:33 +01:00
committed by GitHub
parent fca37ee699
commit 7fa0b87e6e
6 changed files with 83 additions and 57 deletions

View File

@@ -32,6 +32,8 @@
#ifdef ENABLE_MQTT #ifdef ENABLE_MQTT
#include "interface_mqtt.h" #include "interface_mqtt.h"
#endif //ENABLE_MQTT #endif //ENABLE_MQTT
#include "ClassControllCamera.h"
#include "connect_wlan.h"
#include "ClassLogFile.h" #include "ClassLogFile.h"
@@ -135,8 +137,6 @@ void CheckUpdate()
} }
static bool ota_update_task(std::string fn) static bool ota_update_task(std::string fn)
{ {
esp_err_t err; esp_err_t err;
@@ -289,6 +289,7 @@ static bool diagnostic(void)
return true; return true;
} }
void CheckOTAUpdate(void) void CheckOTAUpdate(void)
{ {
ESP_LOGI(TAG, "Start CheckOTAUpdateCheck..."); ESP_LOGI(TAG, "Start CheckOTAUpdateCheck...");
@@ -361,7 +362,6 @@ void CheckOTAUpdate(void)
} }
esp_err_t handler_ota_update(httpd_req_t *req) esp_err_t handler_ota_update(httpd_req_t *req)
{ {
#ifdef DEBUG_DETAIL_ON #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()); ESP_LOGD(TAG, "Delete Default File: %s", fn.c_str());
} }
}; }
if (_task.compare("emptyfirmwaredir") == 0) 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)); httpd_resp_send(req, resp_str, strlen(resp_str));
#ifdef DEBUG_DETAIL_ON
LogFile.WriteHeapInfo("handler_ota_update - Done");
#endif
*/ */
return ESP_OK; #ifdef DEBUG_DETAIL_ON
}; LogFile.WriteHeapInfo("handler_ota_update - Done");
#endif
void hard_restart() { return ESP_OK;
}
void hard_restart()
{
esp_task_wdt_init(1,true); esp_task_wdt_init(1,true);
esp_task_wdt_add(NULL); esp_task_wdt_add(NULL);
while(true); while(true);
} }
void task_reboot(void *pvParameter) 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); vTaskDelay(5000 / portTICK_PERIOD_MS);
esp_restart(); esp_restart(); // Reset type: CPU Reset (Reset both CPUs)
hard_restart();
} 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 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_INFO, TAG, "Reboot triggered by Software (5s).");
LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Reboot in 5sec"); LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Reboot in 5sec");
xTaskCreate(&task_reboot, "reboot", configMINIMAL_STACK_SIZE * 64, NULL, 10, NULL); xTaskCreate(&task_reboot, "task_reboot", 4 * 1024, 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();
} }
@@ -640,6 +646,7 @@ esp_err_t handler_reboot(httpd_req_t *req)
return ESP_OK; return ESP_OK;
} }
void register_server_ota_sdcard_uri(httpd_handle_t server) void register_server_ota_sdcard_uri(httpd_handle_t server)
{ {
ESP_LOGI(TAG, "Registering URI handlers"); ESP_LOGI(TAG, "Registering URI handlers");

View File

@@ -206,7 +206,7 @@ int MQTT_Init() {
} }
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Init"); LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Init");
MQTTdestroy_client(); MQTTdestroy_client(false);
esp_mqtt_client_config_t mqtt_cfg = { esp_mqtt_client_config_t mqtt_cfg = {
.uri = uri.c_str(), .uri = uri.c_str(),
@@ -270,7 +270,7 @@ int MQTT_Init() {
} }
void MQTTdestroy_client() { void MQTTdestroy_client(bool _disable = false) {
if (client) { if (client) {
if (mqtt_connected) { if (mqtt_connected) {
MQTTdestroySubscribeFunction(); MQTTdestroySubscribeFunction();
@@ -282,6 +282,9 @@ void MQTTdestroy_client() {
client = NULL; client = NULL;
mqtt_initialized = false; 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)); 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
vTaskDelay(10000 / portTICK_PERIOD_MS); // Delay execution of callback routine after connection got established if (callbackOnConnected) { // Call onConnected callback routine --> mqtt_server
if (callbackOnConnected) { // Call onConnected callback routine --> mqtt_server callbackOnConnected(maintopic, SetRetainFlag);
callbackOnConnected(maintopic, SetRetainFlag); }
} }
} }

View File

@@ -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, std::string _maintopic, std::string _lwt, std::string _lwt_connected, std::string _lwt_disconnected,
int _keepalive, int SetRetainFlag, void *callbackOnConnected); int _keepalive, int SetRetainFlag, void *callbackOnConnected);
int MQTT_Init(); 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 bool MQTTPublish(std::string _key, std::string _content, int retained_flag = 1); // retained Flag as Standart

View File

@@ -65,17 +65,16 @@ bool isSetupModusActive() {
void KillTFliteTasks() void KillTFliteTasks()
{ {
#ifdef DEBUG_DETAIL_ON #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 #endif
if (xHandletask_autodoFlow != NULL) if( xHandletask_autodoFlow != NULL )
{ {
TaskHandle_t xHandletask_autodoFlowTmp = xHandletask_autodoFlow; vTaskDelete(xHandletask_autodoFlow);
xHandletask_autodoFlow = NULL; 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
} }

View File

@@ -41,17 +41,12 @@
///////////////////// /////////////////////
#include "../../include/defines.h" #include "../../include/defines.h"
/* FreeRTOS event group to signal when we are connected*/ /* FreeRTOS event group to signal when we are connected*/
static EventGroupHandle_t s_wifi_event_group; static EventGroupHandle_t s_wifi_event_group;
static const char *TAG = "WIFI"; static const char *TAG = "WIFI";
static int s_retry_num = 0; 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() std::string* getIPAddress()
{ {
return &ipadress; return &ipadress;
} }
std::string* getSSID() std::string* getSSID()
{ {
return &ssid; return &ssid;
@@ -353,6 +347,7 @@ void task_doBlink(void *pvParameter)
vTaskDelete(NULL); //Delete this task if it exits from the loop above vTaskDelete(NULL); //Delete this task if it exits from the loop above
} }
void LEDBlinkTask(int _dauer, int _anz, bool _off) void LEDBlinkTask(int _dauer, int _anz, bool _off)
{ {
BlinkDauer = _dauer; 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, static void event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data) 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) { void strinttoip4(const char *ip, int &a, int &b, int &c, int &d) {
std::string zw = std::string(ip); std::string zw = std::string(ip);
std::stringstream s(zw); 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; 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) 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; RSSI_Threshold = _rssithreashold;
@@ -463,13 +461,13 @@ void wifi_init_sta(const char *_ssid, const char *_password, const char *_hostna
NULL, NULL,
&instance_got_ip)); &instance_got_ip));
#ifdef WLAN_USE_MESH_ROAMING #ifdef WLAN_USE_MESH_ROAMING
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
WIFI_EVENT_STA_BSS_RSSI_LOW, WIFI_EVENT_STA_BSS_RSSI_LOW,
&esp_bss_rssi_low_handler, &esp_bss_rssi_low_handler,
NULL, NULL,
&instance_bss_rssi_low)); &instance_bss_rssi_low));
#endif #endif
wifi_config_t wifi_config = { }; 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); esp_err_t ret = tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA , _hostname);
hostname = std::string(_hostname); hostname = std::string(_hostname);
if(ret != ESP_OK ){ if(ret != ESP_OK ){
ESP_LOGE(TAG,"failed to set hostname:%d",ret); ESP_LOGE(TAG,"Failed to set hostname: %d",ret);
} }
else { 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. */ * happened. */
if (bits & WIFI_CONNECTED_BIT) { if (bits & WIFI_CONNECTED_BIT) {
#ifdef __HIDE_PASSWORD #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 #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 #endif
} else if (bits & WIFI_FAIL_BIT) { } else if (bits & WIFI_FAIL_BIT) {
#ifdef __HIDE_PASSWORD #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 #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 #endif
} else { } else {
ESP_LOGE(TAG, "UNEXPECTED EVENT"); 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); // vEventGroupDelete(s_wifi_event_group);
} }
int get_WIFI_RSSI() int get_WIFI_RSSI()
{ {
wifi_ap_record_t ap; wifi_ap_record_t ap;
@@ -536,17 +535,34 @@ int get_WIFI_RSSI()
return ap.rssi; return ap.rssi;
} }
void wifi_init_sta(const char *_ssid, const char *_password, const char *_hostname) void wifi_init_sta(const char *_ssid, const char *_password, const char *_hostname)
{ {
wifi_init_sta(_ssid, _password, _hostname, NULL, NULL, NULL, NULL, 0); wifi_init_sta(_ssid, _password, _hostname, NULL, NULL, NULL, NULL, 0);
} }
void wifi_init_sta(const char *_ssid, const char *_password) void wifi_init_sta(const char *_ssid, const char *_password)
{ {
wifi_init_sta(_ssid, _password, NULL, NULL, NULL, NULL, NULL, 0); wifi_init_sta(_ssid, _password, NULL, NULL, NULL, NULL, NULL, 0);
} }
bool getWIFIisConnected() {
bool getWIFIisConnected()
{
return WIFIConnected; 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();
}

View File

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