extracte time stuff, started logger

This commit is contained in:
Your Name 2021-07-01 21:19:51 +02:00
parent a1f2388c7f
commit f1d55ed603
5 changed files with 87 additions and 35 deletions

View File

@ -0,0 +1,14 @@
#pragma once
#define LOG_LEVEL_ERROR 0
#define LOG_LEVEL_WARN 1
#define LOG_LEVEL_INFO 2
#define LOG_TANKSENSOR_FAIL_DETECT "Failed to detect and initialize distance sensor!"
#define LOG_TANKSENSOR_FAIL_DETECT_CODE -1
#define LOG_BACKUP_SUCCESSFUL "Backup sucessful"
#define LOG_BACKUP_SUCCESSFUL_CODE 1
#define LOG_BACKUP_FAILED "Backup error"
#define LOG_BACKUP_FAILED_CODE -2

View File

@ -0,0 +1,4 @@
#pragma once
long getCurrentTime(void);
int getCurrentHour(void);

View File

@ -12,6 +12,7 @@
#include "PlantCtrl.h" #include "PlantCtrl.h"
#include "ControllerConfiguration.h" #include "ControllerConfiguration.h"
#include "TimeUtils.h"
Plant::Plant(int pinSensor, int pinPump, int plantId, HomieNode *plant, PlantSettings_t *setting) Plant::Plant(int pinSensor, int pinPump, int plantId, HomieNode *plant, PlantSettings_t *setting)
{ {
@ -95,6 +96,7 @@ void Plant::activatePump(void)
{ {
const String OFF = String("ON"); const String OFF = String("ON");
this->mPlant->setProperty("switch").send(OFF); this->mPlant->setProperty("switch").send(OFF);
this->mPlant->setProperty("lastPump").send(String(getCurrentTime()));
} }
} }
@ -102,6 +104,7 @@ void Plant::advertise(void)
{ {
// Advertise topics // Advertise topics
this->mPlant->advertise("switch").setName("Pump 1").setDatatype("boolean"); this->mPlant->advertise("switch").setName("Pump 1").setDatatype("boolean");
this->mPlant->advertise("lastPump").setName("lastPump").setDatatype("number").setUnit("unixtime");
//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");

18
esp32/src/TimeUtils.cpp Normal file
View File

@ -0,0 +1,18 @@
#include "TimeUtils.h"
#include <Homie.h>
long getCurrentTime()
{
struct timeval tv_now;
gettimeofday(&tv_now, NULL);
return tv_now.tv_sec;
}
int getCurrentHour()
{
struct tm info;
time_t now;
time(&now);
localtime_r(&now, &info);
return info.tm_hour;
}

View File

@ -13,7 +13,9 @@
/****************************************************************************** /******************************************************************************
* INCLUDES * INCLUDES
******************************************************************************/ ******************************************************************************/
#include "LogDefines.h"
#include "FileUtils.h" #include "FileUtils.h"
#include "TimeUtils.h"
#include "PlantCtrl.h" #include "PlantCtrl.h"
#include "ControllerConfiguration.h" #include "ControllerConfiguration.h"
#include "HomieConfiguration.h" #include "HomieConfiguration.h"
@ -38,8 +40,8 @@
#define AMOUNT_SENOR_QUERYS 8 #define AMOUNT_SENOR_QUERYS 8
#define MAX_TANK_DEPTH 2000 #define MAX_TANK_DEPTH 2000
#define TEST_TOPIC "roundtrip\0" #define TEST_TOPIC "roundtrip\0"
#define LOG_TOPIC "log\0"
#define BACKUP_TOPIC "$implementation/config/backup/set\0" #define BACKUP_TOPIC "$implementation/config/backup/set\0"
#define BACKUP_STATUS_TOPIC "$implementation/config/backup\0"
#define CONFIG_FILE "/homie/config.json" #define CONFIG_FILE "/homie/config.json"
#define CONFIG_FILE_BACKUP "/homie/config.json.bak" #define CONFIG_FILE_BACKUP "/homie/config.json.bak"
@ -54,6 +56,7 @@
* FUNCTION PROTOTYPES * FUNCTION PROTOTYPES
******************************************************************************/ ******************************************************************************/
void log(int level, String message, int code);
int determineNextPump(); int determineNextPump();
void plantcontrol(); void plantcontrol();
void readPowerSwitchedSensors(); void readPowerSwitchedSensors();
@ -101,21 +104,7 @@ Plant mPlants[MAX_PLANTS] = {
/****************************************************************************** /******************************************************************************
* LOCAL FUNCTIONS * LOCAL FUNCTIONS
******************************************************************************/ ******************************************************************************/
long getCurrentTime()
{
struct timeval tv_now;
gettimeofday(&tv_now, NULL);
return tv_now.tv_sec;
}
int getCurrentHour()
{
struct tm info;
time_t now;
time(&now);
localtime_r(&now, &info);
return info.tm_hour;
}
void espDeepSleepFor(long seconds, bool activatePump) void espDeepSleepFor(long seconds, bool activatePump)
{ {
@ -321,9 +310,11 @@ void readPowerSwitchedSensors()
for (int readCnt = 0; readCnt < 5; readCnt++) for (int readCnt = 0; readCnt < 5; readCnt++)
{ {
if(!tankSensor.timeoutOccurred()){ if (!tankSensor.timeoutOccurred())
{
uint16_t distance = tankSensor.readRangeSingleMillimeters(); uint16_t distance = tankSensor.readRangeSingleMillimeters();
if(distance < MAX_TANK_DEPTH){ if (distance < MAX_TANK_DEPTH)
{
waterRawSensor.add(distance); waterRawSensor.add(distance);
} }
} }
@ -333,7 +324,7 @@ void readPowerSwitchedSensors()
} }
else else
{ {
Serial.println("Failed to detect and initialize distance sensor!"); log(LOG_LEVEL_WARN,LOG_TANKSENSOR_FAIL_DETECT,LOG_TANKSENSOR_FAIL_DETECT_CODE);
} }
/* deactivate the sensors */ /* deactivate the sensors */
@ -351,12 +342,15 @@ void onMessage(char *incoming, char *payload, AsyncMqttClientMessageProperties p
getTopic(BACKUP_TOPIC, backupTopic); getTopic(BACKUP_TOPIC, backupTopic);
if (strcmp(incoming, backupTopic) == 0) if (strcmp(incoming, backupTopic) == 0)
{ {
if(strcmp(payload, "true") == 0){ if (strcmp(payload, "true") == 0)
{
bool backupSucessful = copyFile(CONFIG_FILE, CONFIG_FILE_BACKUP); bool backupSucessful = copyFile(CONFIG_FILE, CONFIG_FILE_BACKUP);
printFile(CONFIG_FILE_BACKUP); printFile(CONFIG_FILE_BACKUP);
getTopic(BACKUP_STATUS_TOPIC, backupStatusTopic); if(backupSucessful){
Homie.getMqttClient().publish(backupStatusTopic, 2, true, backupSucessful ? "true" : "false"); log(LOG_LEVEL_INFO,LOG_BACKUP_SUCCESSFUL, LOG_BACKUP_SUCCESSFUL_CODE);
delete backupStatusTopic; }else {
log(LOG_LEVEL_INFO,LOG_BACKUP_FAILED, LOG_BACKUP_FAILED_CODE);
}
Homie.getMqttClient().publish(backupTopic, 2, true, "false"); Homie.getMqttClient().publish(backupTopic, 2, true, "false");
} }
} }
@ -557,8 +551,6 @@ void setup()
// read button // read button
pinMode(BUTTON, INPUT); pinMode(BUTTON, INPUT);
// Power pins // Power pins
pinMode(OUTPUT_ENABLE_PUMP, OUTPUT); pinMode(OUTPUT_ENABLE_PUMP, OUTPUT);
@ -579,7 +571,6 @@ void setup()
return; return;
} }
/************************* Start One-Wire bus ***************/ /************************* Start One-Wire bus ***************/
int tempInitStartTime = millis(); int tempInitStartTime = millis();
uint8_t sensorCount = 0U; uint8_t sensorCount = 0U;
@ -613,7 +604,8 @@ void setup()
// Set default values // Set default values
//in seconds //in seconds
deepSleepTime.setDefaultValue(600).setValidator([](long candidate) { return (candidate > 0) && (candidate < (60 * 60 * 2) /** 2h max sleep */); }); deepSleepTime.setDefaultValue(600).setValidator([](long candidate)
{ return (candidate > 0) && (candidate < (60 * 60 * 2) /** 2h max sleep */); });
deepSleepNightTime.setDefaultValue(600); deepSleepNightTime.setDefaultValue(600);
wateringDeepSleep.setDefaultValue(5); wateringDeepSleep.setDefaultValue(5);
ntpServer.setDefaultValue("pool.ntp.org"); ntpServer.setDefaultValue("pool.ntp.org");
@ -628,7 +620,6 @@ void setup()
Homie.onEvent(onHomieEvent); Homie.onEvent(onHomieEvent);
//Homie.disableLogging(); //Homie.disableLogging();
Homie.setup(); Homie.setup();
mConfigured = Homie.isConfigured(); mConfigured = Homie.isConfigured();
@ -673,20 +664,22 @@ void setup()
else else
{ {
printFile("/homie/Readme.md"); printFile("/homie/Readme.md");
if (doesFileExist(CONFIG_FILE)){ if (doesFileExist(CONFIG_FILE))
{
printFile(CONFIG_FILE); printFile(CONFIG_FILE);
} }
if (doesFileExist(CONFIG_FILE_BACKUP)){ if (doesFileExist(CONFIG_FILE_BACKUP))
{
printFile(CONFIG_FILE_BACKUP); printFile(CONFIG_FILE_BACKUP);
bool restoredConfig = copyFile(CONFIG_FILE_BACKUP, CONFIG_FILE); bool restoredConfig = copyFile(CONFIG_FILE_BACKUP, CONFIG_FILE);
if(restoredConfig){ if (restoredConfig)
{
deleteFile(CONFIG_FILE_BACKUP); deleteFile(CONFIG_FILE_BACKUP);
espDeepSleepFor(1,false); espDeepSleepFor(1, false);
return; return;
} }
} }
readOneWireSensors(); readOneWireSensors();
//prevent BOD to be paranoid //prevent BOD to be paranoid
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);
@ -876,3 +869,23 @@ void plantcontrol()
} }
/** @}*/ /** @}*/
void log(int level, String message, int statusCode)
{
String buffer;
StaticJsonDocument<200> doc;
doc["level"] = level;
doc["message"] = message;
doc["statusCode"] = statusCode;
serializeJson(doc, buffer);
if (mAliveWasRead)
{
getTopic(LOG_TOPIC, logTopic)
Homie.getMqttClient()
.subscribe(logTopic, 2);
Homie.getMqttClient().publish(logTopic, 2, false, buffer.c_str());
delete logTopic;
}
Serial << statusCode << "@" << level << " : " << message << endl;
}