converted serial prints into MQTT status log messages

This commit is contained in:
Ollo 2021-07-01 22:06:50 +02:00
parent f1d55ed603
commit 57d18af76d
4 changed files with 45 additions and 38 deletions

View File

@ -3,6 +3,7 @@
#define LOG_LEVEL_ERROR 0 #define LOG_LEVEL_ERROR 0
#define LOG_LEVEL_WARN 1 #define LOG_LEVEL_WARN 1
#define LOG_LEVEL_INFO 2 #define LOG_LEVEL_INFO 2
#define LOG_LEVEL_DEBUG 3
#define LOG_TANKSENSOR_FAIL_DETECT "Failed to detect and initialize distance sensor!" #define LOG_TANKSENSOR_FAIL_DETECT "Failed to detect and initialize distance sensor!"
#define LOG_TANKSENSOR_FAIL_DETECT_CODE -1 #define LOG_TANKSENSOR_FAIL_DETECT_CODE -1
@ -12,3 +13,13 @@
#define LOG_BACKUP_FAILED "Backup error" #define LOG_BACKUP_FAILED "Backup error"
#define LOG_BACKUP_FAILED_CODE -2 #define LOG_BACKUP_FAILED_CODE -2
#define LOG_PUMP_BUTNOTANK_MESSAGE "Want to pump but no water"
#define LOG_PUMP_BUTNOTANK_CODE -3
#define LOG_PUMP_AND_DOWNLOADMODE "Download mode, ignoring pump request"
#define LOG_PUMP_AND_DOWNLOADMODE_CODE 2
#define LOG_DEBUG_CODE 1001
#define LOG_NOPUMP_LOWLIGHT 100
#define LOG_NOPUMPS 101

View File

@ -125,6 +125,9 @@ public:
{ {
return this->mSetting->pPumpOnlyWhenLowLight->get(); return this->mSetting->pPumpOnlyWhenLowLight->get();
} }
void publishState(String state);
}; };
#endif #endif

View File

@ -87,6 +87,13 @@ void Plant::deactivatePump(void)
} }
} }
void Plant::publishState(String state) {
if (this->mConnected)
{
this->mPlant->setProperty("state").send(state);
}
}
void Plant::activatePump(void) void Plant::activatePump(void)
{ {
int plantId = this->mPlantId; int plantId = this->mPlantId;
@ -108,6 +115,7 @@ void Plant::advertise(void)
//FIXME add .settable(this->switchHandler) //FIXME add .settable(this->switchHandler)
this->mPlant->advertise("moist").setName("Percent").setDatatype("number").setUnit("%"); this->mPlant->advertise("moist").setName("Percent").setDatatype("number").setUnit("%");
this->mPlant->advertise("moistraw").setName("adc").setDatatype("number").setUnit("3.3/4096V"); this->mPlant->advertise("moistraw").setName("adc").setDatatype("number").setUnit("3.3/4096V");
this->mPlant->advertise("state").setName("state").setDatatype("string");
} }
/* FIXME /* FIXME

View File

@ -110,7 +110,7 @@ void espDeepSleepFor(long seconds, bool activatePump)
{ {
if (mDownloadMode) if (mDownloadMode)
{ {
Serial << "abort deepsleep, DownloadMode active" << endl; log(LOG_LEVEL_DEBUG, "abort deepsleep, DownloadMode active", LOG_DEBUG_CODE);
return; return;
} }
if (mAliveWasRead) if (mAliveWasRead)
@ -120,11 +120,11 @@ void espDeepSleepFor(long seconds, bool activatePump)
long cTime = getCurrentTime(); long cTime = getCurrentTime();
if (cTime < 100000) if (cTime < 100000)
{ {
Serial << "Wait for ntp" << endl;
delay(100); delay(100);
} }
else else
{ {
log(LOG_LEVEL_DEBUG, "NTP timeout before deepsleep", LOG_DEBUG_CODE);
break; break;
} }
} }
@ -159,19 +159,13 @@ void espDeepSleepFor(long seconds, bool activatePump)
gpio_hold_en(OUTPUT_PUMP6); gpio_hold_en(OUTPUT_PUMP6);
//FIXME fix for outher outputs //FIXME fix for outher outputs
Serial.print("Trying to sleep for ");
Serial.print(seconds);
Serial.println(" seconds");
esp_sleep_enable_timer_wakeup((seconds * 1000U * 1000U)); esp_sleep_enable_timer_wakeup((seconds * 1000U * 1000U));
Serial.flush();
if (mAliveWasRead) if (mAliveWasRead)
{ {
Homie.prepareToSleep(); Homie.prepareToSleep();
} }
else else
{ {
Serial << "Bye offline mode" << endl;
Serial.flush();
esp_deep_sleep_start(); esp_deep_sleep_start();
} }
} }
@ -179,10 +173,6 @@ void espDeepSleepFor(long seconds, bool activatePump)
//requires homie being started //requires homie being started
void readOneWireSensors() void readOneWireSensors()
{ {
Serial << "Read OneWire" << endl;
Serial.flush();
for (uint8_t i = 0; i < sensors.getDeviceCount(); i++) for (uint8_t i = 0; i < sensors.getDeviceCount(); i++)
{ {
uint8_t ds18b20Address[8]; uint8_t ds18b20Address[8];
@ -362,8 +352,6 @@ void onHomieEvent(const HomieEvent &event)
switch (event.type) switch (event.type)
{ {
case HomieEventType::READY_TO_SLEEP: case HomieEventType::READY_TO_SLEEP:
Serial << "Bye homie mode" << endl;
Serial.flush();
esp_deep_sleep_start(); esp_deep_sleep_start();
break; break;
case HomieEventType::SENDING_STATISTICS: case HomieEventType::SENDING_STATISTICS:
@ -376,10 +364,8 @@ void onHomieEvent(const HomieEvent &event)
} }
mSensorsRead = true; // MQTT is working, deactivate timeout logic mSensorsRead = true; // MQTT is working, deactivate timeout logic
Serial.printf("NTP Setup with server %s\r\n", ntpServer.get());
configTime(0, 0, ntpServer.get()); configTime(0, 0, ntpServer.get());
Serial << "publish plants mqtt" << endl;
for (int i = 0; i < MAX_PLANTS; i++) for (int i = 0; i < MAX_PLANTS; i++)
{ {
mPlants[i].postMQTTconnection(); mPlants[i].postMQTTconnection();
@ -399,7 +385,6 @@ void onHomieEvent(const HomieEvent &event)
break; break;
case HomieEventType::OTA_STARTED: case HomieEventType::OTA_STARTED:
Homie.getLogger() << "OTA started" << endl;
for (int i = 0; i < MAX_PLANTS; i++) for (int i = 0; i < MAX_PLANTS; i++)
{ {
mPlants[i].deactivatePump(); mPlants[i].deactivatePump();
@ -411,7 +396,6 @@ void onHomieEvent(const HomieEvent &event)
mDownloadMode = true; mDownloadMode = true;
break; break;
case HomieEventType::OTA_SUCCESSFUL: case HomieEventType::OTA_SUCCESSFUL:
Homie.getLogger() << "OTA successful" << endl;
digitalWrite(OUTPUT_ENABLE_SENSOR, LOW); digitalWrite(OUTPUT_ENABLE_SENSOR, LOW);
digitalWrite(OUTPUT_ENABLE_PUMP, LOW); digitalWrite(OUTPUT_ENABLE_PUMP, LOW);
ESP.restart(); ESP.restart();
@ -430,22 +414,26 @@ int determineNextPump()
Plant plant = mPlants[i]; Plant plant = mPlants[i];
if (!plant.isPumpTriggerActive()) if (!plant.isPumpTriggerActive())
{ {
Serial.printf("%d Skip deactivated pump\r\n", i); plant.publishState("deactivated");
log(LOG_LEVEL_DEBUG, String(i + " Skip deactivated pump"), LOG_DEBUG_CODE);
continue; continue;
} }
if ((rtcLastWateringPlant[i] + plant.getCooldownInSeconds()) > getCurrentTime()) if ((rtcLastWateringPlant[i] + plant.getCooldownInSeconds()) > getCurrentTime())
{ {
Serial.printf("%d Skipping due to cooldown %ld / %ld \r\n", i, rtcLastWateringPlant[i], plant.getCooldownInSeconds()); plant.publishState("cooldown");
log(LOG_LEVEL_DEBUG, String(i+" Skipping due to cooldown " + (rtcLastWateringPlant[i] + plant.getCooldownInSeconds())), LOG_DEBUG_CODE);
continue; continue;
} }
if (!isLowLight && plant.isAllowedOnlyAtLowLight()) if (!isLowLight && plant.isAllowedOnlyAtLowLight())
{ {
Serial.printf("%d No pump required: due to light\r\n", i); plant.publishState("sunny");
log(LOG_LEVEL_DEBUG, String(i+" No pump required: due to light"), LOG_DEBUG_CODE);
continue; continue;
} }
if (plant.getCurrentMoisture() == MISSING_SENSOR) if (plant.getCurrentMoisture() == MISSING_SENSOR)
{ {
Serial.printf("%d No pump possible: missing sensor \r\n", i); plant.publishState("nosensor");
log(LOG_LEVEL_DEBUG, String(i+" No pump possible: missing sensor"), LOG_DEBUG_CODE);
continue; continue;
} }
if (plant.isPumpRequired()) if (plant.isPumpRequired())
@ -459,18 +447,20 @@ int determineNextPump()
/* no time from NTP received */ /* no time from NTP received */
(getCurrentTime() < 10000)) (getCurrentTime() < 10000))
{ {
Serial.printf("%d Requested pumping\r\n", i); plant.publishState("active");
log(LOG_LEVEL_DEBUG, String(i+" Requested pumping"), LOG_DEBUG_CODE);
pumpToUse = i; pumpToUse = i;
} }
else else
{ {
Serial.printf("%d ignored due to time boundary: %d to %d (current %d)\r\n", i, plant.getHoursStart(), plant.getHoursEnd(), getCurrentHour()); plant.publishState("after-work");
log(LOG_LEVEL_DEBUG, String(i+" ignored due to time boundary: " + String(plant.getHoursStart()) + " to " + plant.getHoursEnd() + " ( current " + getCurrentHour() + " )"), LOG_DEBUG_CODE);
} }
continue; continue;
} }
else else
{ {
Serial.printf("%d No pump required: moisture acceptable %f / %ld\r\n", i, plant.getCurrentMoisture(), plant.getSettingsMoisture()); plant.publishState("wet");
} }
} }
return pumpToUse; return pumpToUse;
@ -486,10 +476,10 @@ int determineNextPump()
*/ */
bool aliveHandler(const HomieRange &range, const String &value) bool aliveHandler(const HomieRange &range, const String &value)
{ {
if (range.isRange) if (range.isRange) {
return false; // only one controller is present return false; // only one controller is present
Serial.println("aliuve handler"); }
Serial.flush();
if (value.equals("ON") || value.equals("On") || value.equals("1")) if (value.equals("ON") || value.equals("On") || value.equals("1"))
{ {
mDownloadMode = true; mDownloadMode = true;
@ -546,8 +536,6 @@ void setup()
{ {
mPlants[i].init(); mPlants[i].init();
} }
Serial.println("plants init");
Serial.flush();
// read button // read button
pinMode(BUTTON, INPUT); pinMode(BUTTON, INPUT);
@ -663,7 +651,6 @@ void setup()
} }
else else
{ {
printFile("/homie/Readme.md");
if (doesFileExist(CONFIG_FILE)) if (doesFileExist(CONFIG_FILE))
{ {
printFile(CONFIG_FILE); printFile(CONFIG_FILE);
@ -825,13 +812,13 @@ void plantcontrol()
lastPumpRunning = determineNextPump(); lastPumpRunning = determineNextPump();
if (lastPumpRunning != -1 && !hasWater) if (lastPumpRunning != -1 && !hasWater)
{ {
Serial.println("Want to pump but no water"); log(LOG_LEVEL_ERROR, LOG_PUMP_BUTNOTANK_MESSAGE, LOG_PUMP_BUTNOTANK_CODE);
} }
else if (lastPumpRunning != -1 && hasWater) else if (lastPumpRunning != -1 && hasWater)
{ {
if (mDownloadMode) if (mDownloadMode)
{ {
Serial.println("Mode 3 active, ignoring pump request"); log(LOG_LEVEL_INFO, LOG_PUMP_AND_DOWNLOADMODE, LOG_PUMP_AND_DOWNLOADMODE_CODE);
} }
else else
{ {
@ -851,19 +838,17 @@ void plantcontrol()
{ {
if (mSolarVoltage < SOLAR_CHARGE_MIN_VOLTAGE) if (mSolarVoltage < SOLAR_CHARGE_MIN_VOLTAGE)
{ {
Serial.print(mSolarVoltage); log(LOG_LEVEL_INFO, String(String(mSolarVoltage) + "V! No pumps to activate and low light, deepSleepNight"), LOG_NOPUMP_LOWLIGHT);
Serial.println("V! No pumps to activate and low light, deepSleepNight");
espDeepSleepFor(deepSleepNightTime.get(), false); espDeepSleepFor(deepSleepNightTime.get(), false);
} }
else else
{ {
Serial.println("No pumps to activate, deepSleep"); log(LOG_LEVEL_INFO, "No pumps to activate, deepSleep", LOG_NOPUMPS);
espDeepSleepFor(deepSleepTime.get(), false); espDeepSleepFor(deepSleepTime.get(), false);
} }
} }
else else
{ {
Serial.println("Running pump, watering deepsleep");
espDeepSleepFor(wateringDeepSleep.get(), true); espDeepSleepFor(wateringDeepSleep.get(), true);
} }
} }
@ -888,4 +873,4 @@ void log(int level, String message, int statusCode)
delete logTopic; delete logTopic;
} }
Serial << statusCode << "@" << level << " : " << message << endl; Serial << statusCode << "@" << level << " : " << message << endl;
} }