Compare commits

...

4 Commits

Author SHA1 Message Date
8bae83ea32 WiFi.disconnect on wifiOff 2025-01-16 11:55:44 +01:00
38ebd8d32a logSetup 2025-01-16 11:55:23 +01:00
dd64709884 clock startupTime FIX 2025-01-16 11:55:00 +01:00
ebacfbed02 BOOT_DELAY_SECONDS 3 2025-01-16 11:49:46 +01:00
7 changed files with 101 additions and 57 deletions

View File

@ -4,6 +4,8 @@
#include "clock.h"
#include "log.h"
#define BOOT_DELAY_SECONDS 3
void bootDelay() {
info("Waiting for WiFi...");
while (!isWiFiConnected()) {
@ -18,9 +20,9 @@ void bootDelay() {
yield();
}
info("Waiting 5 seconds for OTA update...");
info("Waiting %d seconds for OTA update...", BOOT_DELAY_SECONDS);
const auto start = millis();
while (millis() - start < 5000) {
while (millis() - start < BOOT_DELAY_SECONDS * 1000) {
wifiLoop();
yield();
}

View File

@ -22,16 +22,16 @@ void clockLoop() {
}
if (!ntpSet && isWiFiConnected()) {
configTime(CLOCK_GMT_OFFSET_SECONDS, CLOCK_DST_OFFSET_SECONDS, WiFi.gatewayIP().toString().c_str(), CLOCK_NTP_SERVER2_URL);
configTime(CLOCK_GMT_OFFSET_SECONDS, CLOCK_DST_OFFSET_SECONDS, CLOCK_NTP_SERVER2_URL, WiFi.gatewayIP().toString().c_str());
ntpSet = true;
}
const auto now = time(nullptr);
if (isCorrectTime(now)) {
clockOffset = now;
} else {
startupTime = now - clockOffset;
info("clock set after %ld seconds! So startup was at %s", clockOffset, getClockStr());
info("clock set after %ld seconds! So startup was at %s", clockOffset, getStartupStr());
} else {
clockOffset = now;
}
}
@ -39,18 +39,46 @@ bool isClockSet() {
return startupTime != 0;
}
char buffer[20];
char *getClockStr() {
const auto now = time(nullptr);
tm t{0};
char *getClockStr(const time_t epoch) {
auto now = epoch;
if (now == 0) {
now = time(nullptr);
}
tm t{};
localtime_r(&now, &t);
static char buffer[20];
snprintf(buffer, sizeof buffer, "%04d-%02d-%02d %02d:%02d:%02d", t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);
return buffer;
}
char *getStartupStr() {
return getClockStr(startupTime);
}
time_t getUptimeSeconds() {
return time(nullptr) - startupTime;
}
char *getUptimeStr() {
const auto totalSeconds = getUptimeSeconds();
const auto totalMinutes = totalSeconds / 60;
const auto totalHours = totalMinutes / 60;
const auto partSeconds = totalSeconds % 60;
const auto partMinutes = totalMinutes % 60;
const auto partHours = totalHours % 24;
const auto partDays = totalHours / 24;
static char buffer[20];
snprintf(buffer, sizeof buffer, "%dd %2dh %2dm %2ds", partDays, partHours, partMinutes, partSeconds);
return buffer;
}
bool isCorrectTime(const time_t now) {
return now < CLOCK_EPOCH_SECONDS_MIN;
return now >= CLOCK_EPOCH_SECONDS_MIN;
}
time_t clockCorrect(const time_t t) {

View File

@ -7,7 +7,13 @@ void clockLoop();
bool isClockSet();
char * getClockStr();
char *getClockStr(time_t epoch = 0);
char *getStartupStr();
time_t getUptimeSeconds();
char *getUptimeStr();
bool isCorrectTime(time_t now);

View File

@ -1,13 +1,53 @@
#include "log.h"
#include <cstdio>
#include <cstdarg>
#include "clock.h"
void doLog(LogLevel level, const char *format, va_list args);
auto logLevel = DEBUG;
void log(const LogLevel level, const char *format, const va_list args) {
void logSetup() {
delay(500);
Serial.begin(115200);
info("Startup");
}
void log(const LogLevel level, const char *format, ...) {
va_list args;
va_start(args, format);
doLog(level, format, args);
va_end(args);
}
void error(const char *format, ...) {
va_list args;
va_start(args, format);
doLog(ERROR, format, args);
va_end(args);
}
void warn(const char *format, ...) {
va_list args;
va_start(args, format);
doLog(WARN, format, args);
va_end(args);
}
void info(const char *format, ...) {
va_list args;
va_start(args, format);
doLog(INFO, format, args);
va_end(args);
}
void debug(const char *format, ...) {
va_list args;
va_start(args, format);
doLog(DEBUG, format, args);
va_end(args);
}
void doLog(const LogLevel level, const char *format, const va_list args) {
if (level > logLevel) {
return;
}
@ -34,38 +74,3 @@ void log(const LogLevel level, const char *format, const va_list args) {
yield();
}
void log(const LogLevel level, const char *format, ...) {
va_list args;
va_start(args, format);
log(level, format, args);
va_end(args);
}
void error(const char *format, ...) {
va_list args;
va_start(args, format);
log(ERROR, format, args);
va_end(args);
}
void warn(const char *format, ...) {
va_list args;
va_start(args, format);
log(WARN, format, args);
va_end(args);
}
void info(const char *format, ...) {
va_list args;
va_start(args, format);
log(INFO, format, args);
va_end(args);
}
void debug(const char *format, ...) {
va_list args;
va_start(args, format);
log(DEBUG, format, args);
va_end(args);
}

View File

@ -8,7 +8,7 @@ enum LogLevel {
DEBUG = 3
};
void log(LogLevel level, const char *format, ...);
void logSetup();
void error(const char *format, ...);
@ -18,4 +18,6 @@ void info(const char *format, ...);
void debug(const char *format, ...);
void log(LogLevel level, const char *format, ...);
#endif

View File

@ -2,9 +2,10 @@
#include "main.h"
#include "boot.h"
#include "wifi.h"
#include "http.h"
#include "log.h"
#include "mqtt.h"
#include "wifi.h"
#include "sensor/DallasSensor.h"
#include "sensor/DHT22.h"
@ -19,9 +20,7 @@ NodeHeizung node = NodeHeizung();
#endif
void setup() {
Serial.begin(115200);
delay(500);
Serial.print("Startup\n");
logSetup();
bootDelay();
node.setup();
httpSetup();

View File

@ -57,6 +57,7 @@ void wifiSetupOTA() {
void wifiOff() {
info("wifi disabled");
wifiEnabled = false;
WiFi.disconnect();
}
void wifiLoop() {
@ -64,7 +65,7 @@ void wifiLoop() {
if (wifiConnected != currentState) {
wifiConnected = currentState;
if (wifiConnected) {
info("wifi connected: %s", WiFi.localIP().toString().c_str());
info("wifi connected as %s", WiFi.localIP().toString().c_str());
wifiSetupOTA();
} else {
warn("wifi disconnected");
@ -79,6 +80,7 @@ void wifiLoop() {
wifiTryMillis = millis();
WiFiClass::hostname(wifiHost);
WiFi.setAutoReconnect(true);
info(R"(connecting to SSID "%s" with hostname "%s")", wifiSsid, wifiHost);
WiFi.begin(wifiSsid, wifiPkey);
}
} else {