Skip to content

Commit

Permalink
Streamlined MQTT interlock when not activated & interlock MQTT when W…
Browse files Browse the repository at this point in the history
…IFI is not connected (#1556)

* streamlined mqtt interlock when disabled

* Disconnect mqtt client before reboot

* Interlock MQTT with WIFI

* Update

* loglevel to DEBUG

* Update

* mqtt msg id incremental

* new ENABLE_MQTT includes

* Loglevel to DEBUG

* Loglevel

* Update interface_mqtt.cpp
  • Loading branch information
Slider0007 authored Dec 12, 2022
1 parent 93f0f6b commit b85e3b1
Show file tree
Hide file tree
Showing 12 changed files with 186 additions and 129 deletions.
6 changes: 6 additions & 0 deletions code/components/jomjol_fileserver_ota/server_ota.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
#include "server_tflite.h"
#include "server_file.h"
#include "server_GPIO.h"
#ifdef ENABLE_MQTT
#include "interface_mqtt.h"
#endif //ENABLE_MQTT


#include "ClassLogFile.h"
Expand Down Expand Up @@ -601,6 +604,9 @@ void doReboot(){
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();
Expand Down
4 changes: 2 additions & 2 deletions code/components/jomjol_flowcontroll/ClassFlowImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ void ClassFlowImage::RemoveOldLogs()
if (!isLogImage)
return;

ESP_LOGI(TAG, "remove old images");
ESP_LOGD(TAG, "remove old images");
if (logfileRetentionInDays == 0) {
return;
}
Expand Down Expand Up @@ -132,7 +132,7 @@ void ClassFlowImage::RemoveOldLogs()
}
}
}
ESP_LOGI(TAG, "Image folder deleted: %d | Image folder not deleted: %d", deleted, notDeleted);
ESP_LOGD(TAG, "Image folder deleted: %d | Image folder not deleted: %d", deleted, notDeleted);
closedir(dir);
}

26 changes: 8 additions & 18 deletions code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,36 +189,26 @@ string ClassFlowMQTT::GetMQTTMainTopic()
}


bool ClassFlowMQTT::Start(float AutoIntervall) {

// printf("URI: %s, MAINTOPIC: %s", uri.c_str(), maintopic.c_str());

if ((uri.length() == 0) || (maintopic.length() == 0))
{
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "MQTT not started because URI or Maintopic is not set. MQTT will be disabled.");
MQTTdisable();
return false;
}

bool ClassFlowMQTT::Start(float AutoIntervall)
{
roundInterval = AutoIntervall; // Minutes
keepAlive = roundInterval * 60 * 2.5; // Seconds, make sure it is greater thatn 2 rounds!

std::stringstream stream;
stream << std::fixed << std::setprecision(1) << "Digitizer interval is " << roundInterval <<
" minutes => setting MQTT LWT timeout to " << ((float)keepAlive/60) << " minutes.";
LogFile.WriteToFile(ESP_LOG_INFO, TAG, stream.str());
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, stream.str());

mqttServer_setParameter(flowpostprocessing->GetNumbers(), keepAlive, roundInterval);

MQTT_Configure(uri, clientname, user, password, maintopic, LWT_TOPIC, LWT_CONNECTED, LWT_DISCONNECTED,
keepAlive, SetRetainFlag, (void *)&GotConnected);
bool MQTTConfigCheck = MQTT_Configure(uri, clientname, user, password, maintopic, LWT_TOPIC, LWT_CONNECTED,
LWT_DISCONNECTED, keepAlive, SetRetainFlag, (void *)&GotConnected);

if (!MQTT_Init()) {
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Init at startup failed! Retry with next publish call");
if (!MQTTConfigCheck) {
return false;
}

return true;
return (MQTT_Init() == 1);
}


Expand All @@ -237,7 +227,7 @@ bool ClassFlowMQTT::doFlow(string zwtime)

publishSystemData();

if (flowpostprocessing)
if (flowpostprocessing && getMQTTisConnected())
{
std::vector<NumberPost*>* NUMBERS = flowpostprocessing->GetNumbers();

Expand Down
4 changes: 2 additions & 2 deletions code/components/jomjol_logfile/ClassLogFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ void ClassLogFile::RemoveOldLogFile()
return;
}

ESP_LOGI(TAG, "Remove old log files");
ESP_LOGD(TAG, "Remove old log files");

time_t rawtime;
struct tm* timeinfo;
Expand Down Expand Up @@ -350,7 +350,7 @@ void ClassLogFile::RemoveOldDataLog()
return;
}

ESP_LOGI(TAG, "Remove old data files");
ESP_LOGD(TAG, "Remove old data files");

time_t rawtime;
struct tm* timeinfo;
Expand Down
2 changes: 1 addition & 1 deletion code/components/jomjol_mqtt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ FILE(GLOB_RECURSE app_sources ${CMAKE_CURRENT_SOURCE_DIR}/*.*)

idf_component_register(SRCS ${app_sources}
INCLUDE_DIRS "."
REQUIRES tflite-lib mqtt jomjol_tfliteclass jomjol_helper jomjol_mqtt jomjol_wlan)
REQUIRES tflite-lib mqtt jomjol_tfliteclass jomjol_helper jomjol_mqtt jomjol_wlan)
Loading

0 comments on commit b85e3b1

Please sign in to comment.