Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deinit components before reboot #1704

Merged
merged 3 commits into from
Dec 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 32 additions & 25 deletions code/components/jomjol_fileserver_ota/server_ota.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#ifdef ENABLE_MQTT
#include "interface_mqtt.h"
#endif //ENABLE_MQTT
#include "ClassControllCamera.h"
#include "connect_wlan.h"


#include "ClassLogFile.h"
Expand Down Expand Up @@ -135,8 +137,6 @@ void CheckUpdate()
}




static bool ota_update_task(std::string fn)
{
esp_err_t err;
Expand Down Expand Up @@ -289,6 +289,7 @@ static bool diagnostic(void)
return true;
}


void CheckOTAUpdate(void)
{
ESP_LOGI(TAG, "Start CheckOTAUpdateCheck...");
Expand Down Expand Up @@ -361,7 +362,6 @@ void CheckOTAUpdate(void)
}



esp_err_t handler_ota_update(httpd_req_t *req)
{
#ifdef DEBUG_DETAIL_ON
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
}


Expand All @@ -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");
Expand Down
17 changes: 10 additions & 7 deletions code/components/jomjol_mqtt/interface_mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -270,7 +270,7 @@ int MQTT_Init() {
}


void MQTTdestroy_client() {
void MQTTdestroy_client(bool _disable = false) {
if (client) {
if (mqtt_connected) {
MQTTdestroySubscribeFunction();
Expand All @@ -282,6 +282,9 @@ void MQTTdestroy_client() {
client = NULL;
mqtt_initialized = false;
}

if (_disable) // Disable MQTT service, avoid restart with MQTTPublish
mqtt_configOK = false;
}


Expand Down Expand Up @@ -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);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion code/components/jomjol_mqtt/interface_mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 6 additions & 7 deletions code/components/jomjol_tfliteclass/server_tflite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
}


Expand Down
48 changes: 32 additions & 16 deletions code/components/jomjol_wlan/connect_wlan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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)
{
Expand Down Expand Up @@ -398,13 +394,15 @@ 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);
char ch; //to temporarily store the '.'
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;
Expand Down Expand Up @@ -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 = { };

Expand All @@ -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);
}

}
Expand All @@ -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");
Expand All @@ -529,24 +527,42 @@ 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;
esp_wifi_sta_get_ap_info(&ap);
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 change: 1 addition & 0 deletions code/components/jomjol_wlan/connect_wlan.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down