prevent time variable overflow

This commit is contained in:
lukas 2020-12-09 19:53:59 +01:00
parent 1547854edf
commit ee91b36c66
5 changed files with 48 additions and 16 deletions

View File

@ -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;
}

View File

@ -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);

10
src/Timer.cpp Normal file
View File

@ -0,0 +1,10 @@
//
// Created by lukas on 09.12.20.
//
#include <Arduino.h>
#include "Timer.h"
uint32_t Timer::getSystemSeconds() {
return (uint32_t)(micros64() / 1000000);
}

18
src/Timer.h Normal file
View File

@ -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

View File

@ -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");