From ee91b36c667c1473fb9bfd5af2d229ad685282db Mon Sep 17 00:00:00 2001 From: lukas Date: Wed, 9 Dec 2020 19:53:59 +0100 Subject: [PATCH] prevent time variable overflow --- src/GeneralInfoService.cpp | 17 ++++++++++------- src/GeneralInfoService.h | 6 +++--- src/Timer.cpp | 10 ++++++++++ src/Timer.h | 18 ++++++++++++++++++ src/main.cpp | 13 +++++++------ 5 files changed, 48 insertions(+), 16 deletions(-) create mode 100644 src/Timer.cpp create mode 100644 src/Timer.h diff --git a/src/GeneralInfoService.cpp b/src/GeneralInfoService.cpp index 431ac61..7062e40 100644 --- a/src/GeneralInfoService.cpp +++ b/src/GeneralInfoService.cpp @@ -4,6 +4,7 @@ #include "GeneralInfoService.h" #include "Pins.h" +#include "Timer.h" GeneralInfoService::GeneralInfoService(AsyncWebServer *server, float *lasthum, float *lasttemp) : hum(lasthum), @@ -21,10 +22,12 @@ void GeneralInfoService::reply(AsyncWebServerRequest *request) { root["temp"] = *temp; root["hum"] = *hum; - root["lastpumptime"] = lastPumpTime != 0 ? (millis() - lastPumpTime) / 1000 : 0; - root["lastWaterOutage"] = lastWaterOutage != 0 ? (millis() - lastWaterOutage) / 1000 : 0; - root["lastPumpDuration"] = lastPumpDuration != 0 ? lastPumpDuration / 1000 : 0; - root["runtime"] = millis() / 1000; + const uint32_t systime = Timer::getSystemSeconds(); + + root["lastpumptime"] = lastPumpTime != 0 ? systime - lastPumpTime : 0; + root["lastWaterOutage"] = lastWaterOutage != 0 ? systime - lastWaterOutage : 0; + root["lastPumpDuration"] = lastPumpDuration != 0 ? lastPumpDuration : 0; + root["runtime"] = systime; root["watersensor"] = digitalRead(WasserSensorPin) ? true : false; root["pressuresensor"] = digitalRead(DruckSensorPin) ? true : false; @@ -35,14 +38,14 @@ void GeneralInfoService::reply(AsyncWebServerRequest *request) { request->send(response); } -void GeneralInfoService::setlastPumpTime(unsigned long lastPumpTime) { +void GeneralInfoService::setlastPumpTime(uint32_t lastPumpTime) { this->lastPumpTime = lastPumpTime; } -void GeneralInfoService::setlastWaterOutage(unsigned long lastWaterOutage) { +void GeneralInfoService::setlastWaterOutage(uint32_t lastWaterOutage) { this->lastWaterOutage = lastWaterOutage; } -void GeneralInfoService::setPumpDuration(unsigned long lastPumpDuration) { +void GeneralInfoService::setPumpDuration(uint32_t lastPumpDuration) { this->lastPumpDuration = lastPumpDuration; } diff --git a/src/GeneralInfoService.h b/src/GeneralInfoService.h index fee1adf..b2947d1 100644 --- a/src/GeneralInfoService.h +++ b/src/GeneralInfoService.h @@ -23,19 +23,19 @@ public: * sets the last time when the pump was on * @param lastPumpTime pump time in ms */ - void setlastPumpTime(unsigned long lastPumpTime); + void setlastPumpTime(uint32_t lastPumpTime); /** * sets the last time of a water outage * @param lastWaterOutage last wateroutage time in ms */ - void setlastWaterOutage(unsigned long lastWaterOutage); + void setlastWaterOutage(uint32_t lastWaterOutage); /** * sets the duration of the last pump cycle * @param lastPumpDuration duration in ms */ - void setPumpDuration(unsigned long lastPumpDuration); + void setPumpDuration(uint32_t lastPumpDuration); private: void reply(AsyncWebServerRequest* request); diff --git a/src/Timer.cpp b/src/Timer.cpp new file mode 100644 index 0000000..fe18308 --- /dev/null +++ b/src/Timer.cpp @@ -0,0 +1,10 @@ +// +// Created by lukas on 09.12.20. +// + +#include +#include "Timer.h" + +uint32_t Timer::getSystemSeconds() { + return (uint32_t)(micros64() / 1000000); +} diff --git a/src/Timer.h b/src/Timer.h new file mode 100644 index 0000000..3f84212 --- /dev/null +++ b/src/Timer.h @@ -0,0 +1,18 @@ +// +// Created by lukas on 09.12.20. +// + +#ifndef PUMPENSTEUERUNG_TIMER_H +#define PUMPENSTEUERUNG_TIMER_H + +class Timer { +public: + /** + * get system time in seconds + * @return system time in seconds + */ + static uint32_t getSystemSeconds(); +}; + + +#endif //PUMPENSTEUERUNG_TIMER_H diff --git a/src/main.cpp b/src/main.cpp index 47f7452..e40605a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,6 +5,7 @@ #include "Pins.h" #include "GeneralInfoService.h" #include "SettingsService.h" +#include "Timer.h" bool allow; bool error = false; @@ -18,7 +19,7 @@ Ticker pumpendauer; //WifiManager mang; Heating mHeat; -unsigned long turnontime = 0; +uint32_t turnontime = 0; AsyncWebServer server(80); ESP8266React esp8266React(&server); @@ -31,7 +32,7 @@ void pumpeSchalten(bool on) { if (on) { Serial.println("versuche Pumpe EIN zuschalten"); // refresh last pump counter - generalinfo.setlastPumpTime(millis()); + generalinfo.setlastPumpTime(Timer::getSystemSeconds()); } else { Serial.println("versuche Pumpe AUS zuschalten"); } @@ -40,7 +41,7 @@ void pumpeSchalten(bool on) { if (on) { pumpendauer.once((float)settingsservice.getSettings()->maxpumpduration + 1, []() { //erlaube keine einschaltung von mehr als 60 sek - if (millis() - turnontime >= (unsigned)settingsservice.getSettings()->maxpumpduration * 1000 && turnontime != 0) { + if (Timer::getSystemSeconds() - turnontime >= (unsigned)settingsservice.getSettings()->maxpumpduration && turnontime != 0) { //error zu lange Serial.println("\n\npumpe lief mehr als 10 Minuten durchgaengig"); pumpeSchalten(false); @@ -48,11 +49,11 @@ void pumpeSchalten(bool on) { } }); } else { - generalinfo.setPumpDuration(millis() - turnontime); + generalinfo.setPumpDuration(Timer::getSystemSeconds() - turnontime); } // save pump start time - turnontime = millis(); + turnontime = Timer::getSystemSeconds(); digitalWrite(SchuetzPin, on); Serial.println("[Erfolg] pumpe wird geschalten"); @@ -90,7 +91,7 @@ void WasserSensorCheck() { //kein Wasser dh timer auf 10min stellen // refresh wateroutage counter - generalinfo.setlastWaterOutage(millis()); + generalinfo.setlastWaterOutage(Timer::getSystemSeconds()); allow = false; Serial.println("Schalte pumpe aus");