209 lines
6.0 KiB
C++
209 lines
6.0 KiB
C++
#include <Arduino.h>
|
|
#include <Ticker.h>
|
|
#include <ESP8266React.h>
|
|
#include "Heating.h"
|
|
#include "Pins.h"
|
|
#include "GeneralInfoService.h"
|
|
#include "SettingsService.h"
|
|
#include "Timer.h"
|
|
#include "HeatingInfoService.h"
|
|
|
|
bool allow;
|
|
bool error = false;
|
|
|
|
// ticker fuer kein-wasser abschaltung
|
|
Ticker status;
|
|
|
|
//pumpendauer maximum ticker
|
|
Ticker pumpendauer;
|
|
|
|
//WifiManager mang;
|
|
Heating mHeat;
|
|
|
|
uint32_t turnontime = 0;
|
|
uint32_t pumpcycles = 0;
|
|
|
|
AsyncWebServer server(80);
|
|
ESP8266React esp8266React(&server);
|
|
|
|
GeneralInfoService generalinfo = GeneralInfoService(&server);
|
|
HeatingInfoService heatinginfo = HeatingInfoService(&server);
|
|
SettingsService settingsservice = SettingsService(&server, esp8266React.getSecurityManager());
|
|
|
|
void pumpeSchalten(bool on) {
|
|
// digitalWrite(4,on);
|
|
if (on) {
|
|
//Serial.println("versuche Pumpe EIN zuschalten");
|
|
// refresh last pump counter
|
|
generalinfo.setlastPumpTime(Timer::getSystemSeconds());
|
|
} else {
|
|
//Serial.println("versuche Pumpe AUS zuschalten");
|
|
}
|
|
|
|
if (allow && !error) {
|
|
if (on) {
|
|
pumpcycles++;
|
|
generalinfo.setPumpCycles(pumpcycles);
|
|
|
|
// pump cycle started
|
|
mHeat.setPumCycleActive(true);
|
|
|
|
pumpendauer.once((float) settingsservice.getSettings()->maxpumpduration + 1, []() {
|
|
//erlaube keine einschaltung von mehr als 60 sek
|
|
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);
|
|
error = true;
|
|
}
|
|
});
|
|
} else {
|
|
generalinfo.setPumpDuration(Timer::getSystemSeconds() - turnontime);
|
|
|
|
// pump cycle stopped...
|
|
mHeat.setPumCycleActive(false);
|
|
}
|
|
|
|
// save pump start time
|
|
turnontime = Timer::getSystemSeconds();
|
|
|
|
digitalWrite(SchuetzPin, on);
|
|
//Serial.println("[Erfolg] pumpe wird geschalten");
|
|
} else {
|
|
//Serial.println("[FEHLGESCHLAGEN] Schalten des Schütz gesperrt durch Timeout oder Fehler-- sofortiges ausschalten der pumpe\n");
|
|
turnontime = -1;
|
|
digitalWrite(SchuetzPin, LOW);
|
|
}
|
|
}
|
|
|
|
ICACHE_RAM_ATTR void DruckschalterInt() {
|
|
schedule_function([]() {
|
|
if (digitalRead(DruckSensorPin) == HIGH) {
|
|
//pumpe einschalten
|
|
//Serial.println("\n\nDruck Sensor EIN");
|
|
if (digitalRead(WasserSensorPin)) {
|
|
//Serial.println("Wasser Sensor EIN");
|
|
pumpeSchalten(true);
|
|
} else {
|
|
//Serial.println("Wasser Sensor aus irgent einem Grund doch nicht ein -- sofort abschalten!");
|
|
pumpeSchalten(false);
|
|
}
|
|
} else {
|
|
//pumpe ausschalten
|
|
//Serial.println("\n\nDruck Sensor AUS");
|
|
pumpeSchalten(false);
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
// time counter to wait
|
|
int wateroutagewaitduration;
|
|
|
|
void WasserSensorCheck() {
|
|
if (digitalRead(WasserSensorPin) == LOW) {
|
|
//Serial.println("Wasser Sensor AUS");
|
|
//kein Wasser dh timer auf 10min stellen
|
|
|
|
// refresh wateroutage counter
|
|
generalinfo.setlastWaterOutage(Timer::getSystemSeconds());
|
|
|
|
allow = false;
|
|
//Serial.println("Schalte pumpe aus");
|
|
pumpeSchalten(false);
|
|
|
|
//Serial.println("warte 30min");
|
|
|
|
status.detach();
|
|
|
|
wateroutagewaitduration = settingsservice.getSettings()->waterOutageWaitDuration;
|
|
status.attach(5, []() {
|
|
wateroutagewaitduration -= 5;
|
|
//Serial.print("noch ");
|
|
//Serial.print(wateroutagewaitduration);
|
|
//Serial.println(" Sekunden verbleibend");
|
|
|
|
if (wateroutagewaitduration <= 0) {
|
|
if (digitalRead(WasserSensorPin)) {
|
|
allow = true;
|
|
//Serial.println("Einschalten der Pumpe wieder erlaubt.");
|
|
|
|
//pruefen ob drucksensor ein
|
|
DruckschalterInt();
|
|
} else {
|
|
//Serial.print("wassersensor immer noch kein Wasser --> verlaengern um 120min\n\n");
|
|
WasserSensorCheck();
|
|
}
|
|
status.detach();
|
|
}
|
|
});
|
|
} else {
|
|
//Serial.println("Wasser Sensor EIN");
|
|
}
|
|
}
|
|
|
|
ICACHE_RAM_ATTR void WasserSensorInt() {
|
|
schedule_function([]() {
|
|
WasserSensorCheck();
|
|
});
|
|
}
|
|
|
|
void setup() {
|
|
// set pins as output
|
|
pinMode(SchuetzPin, OUTPUT);
|
|
pinMode(LED_BUILTIN, OUTPUT);
|
|
pinMode(LuefterPin, OUTPUT);
|
|
pinMode(HeizungPin, OUTPUT);
|
|
|
|
pinMode(WasserSensorPin, INPUT);
|
|
pinMode(DruckSensorPin, INPUT);
|
|
|
|
// initialize pins
|
|
digitalWrite(LED_BUILTIN, HIGH);
|
|
digitalWrite(SchuetzPin, LOW); //pumpe anfangs sofort abschalten
|
|
digitalWrite(LuefterPin, LOW);
|
|
digitalWrite(HeizungPin, LOW);
|
|
|
|
// initilize serial connection
|
|
Serial.begin(115200);
|
|
//Serial.println("\n\n\n\nstartup of ESP");
|
|
//Serial.print("Version: ");
|
|
//Serial.println(VERSION);
|
|
|
|
|
|
//initial measurement of water state
|
|
delay(1000);
|
|
//allow = digitalRead(WasserSensorPin);
|
|
allow = true;
|
|
|
|
|
|
WasserSensorCheck();
|
|
DruckschalterInt();
|
|
// reset the pumpduration settings maybe set by pressureint function above if pressure pin not high...
|
|
generalinfo.setPumpDuration(0);
|
|
|
|
|
|
|
|
//anhängen der Pin-Interrupts
|
|
attachInterrupt(digitalPinToInterrupt(DruckSensorPin), DruckschalterInt, CHANGE);
|
|
attachInterrupt(digitalPinToInterrupt(WasserSensorPin), WasserSensorInt, CHANGE);
|
|
|
|
|
|
// initialize wifi
|
|
//Serial.println("Initializing wifi");
|
|
esp8266React.begin();
|
|
settingsservice.begin();
|
|
server.begin();
|
|
|
|
// initialize heating control
|
|
//Serial.println("initializing heating service");
|
|
mHeat.init(Heating::HUMIDITY, settingsservice.getSettings(), &heatinginfo);
|
|
|
|
//Serial.println("startup sequence complete!\n");
|
|
}
|
|
|
|
|
|
void loop() {
|
|
esp8266React.loop();
|
|
} |