This commit is contained in:
lukas 2020-06-20 09:13:55 +02:00
parent 1ba2aad7e7
commit 9ccec50c37
2 changed files with 71 additions and 150 deletions

View File

@ -12,3 +12,8 @@
platform = espressif8266 platform = espressif8266
board = esp07 board = esp07
framework = arduino framework = arduino
lib_deps =
SPI
Wire
DHT sensor library

View File

@ -1,162 +1,78 @@
#include <Arduino.h> #include "Arduino.h"
#include <Ticker.h> #include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include "WifiManager.h" #define DHTPIN 3 // Digital pin connected to the DHT sensor
// Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 --
// Pin 15 can work but DHT must be disconnected during program upload.
#define VERSION "v1.0" // Uncomment the type of sensor in use:
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
bool allow; // See guide for details on sensor wiring and usage:
bool error = false; // https://learn.adafruit.com/dht/overview
/** pin config */ DHT_Unified dht(DHTPIN, DHTTYPE);
static const uint8_t WasserSensorPin = 14;
static const uint8_t DruckSensorPin = 12;
static const uint8_t SchuetzPin = 13;
/** time config */ uint32_t delayMS;
static const int abschaltzeit = 7200; //sek
static const int maxpumpdauer = 600; //sek
// ticker fuer kein-wasser abschaltung
Ticker status;
//pumpendauer maximum ticker
Ticker pumpendauer;
int turnontime = -1;
void pumpeSchalten(bool on) {
// digitalWrite(4,on);
if (on) {
Serial.println("versuche Pumpe EIN zuschalten");
} else {
Serial.println("versuche Pumpe AUS zuschalten");
}
if (allow && !error) {
if (on) {
pumpendauer.once(maxpumpdauer + 1, []() { //erlaube keine einschaltung von mehr als 60 sek
if (millis() - turnontime >= maxpumpdauer * 1000 && turnontime != -1) {
//error zu lange
Serial.println("\n\npumpe lief mehr als 10 Minuten durchgaengig");
pumpeSchalten(false);
error = true;
}
});
}
turnontime = millis();
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() {
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);
}
}
int i = abschaltzeit; //todo better
void WasserSensorCheck() {
if (digitalRead(WasserSensorPin) == LOW) {
Serial.println("Wasser Sensor AUS");
//kein Wasser dh timer auf 10min stellen
allow = false;
Serial.println("Schalte pumpe aus");
pumpeSchalten(false);
Serial.println("warte 30min");
status.detach();
i = abschaltzeit;
status.attach(5, []() {
i -= 5;
Serial.print("noch ");
Serial.print(i);
Serial.println(" Sekunden verbleibend");
if (i <= 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() {
WasserSensorCheck();
}
void setup() { void setup() {
digitalWrite(SchuetzPin, LOW); //pumpe anfangs sofort abschalten
pinMode(SchuetzPin, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
pinMode(WasserSensorPin, INPUT);
pinMode(DruckSensorPin, INPUT);
Serial.begin(9600); Serial.begin(9600);
Serial.println("\n\n\n\nstartup of ESP"); // Initialize device.
Serial.print("Version: "); Serial.println("DHTxx Unified Sensor Example");
Serial.println(VERSION); dht.begin();
Serial.println(F("DHTxx Unified Sensor Example"));
// Print temperature sensor details.
//initial measurement of water state sensor_t sensor;
delay(1000); dht.temperature().getSensor(&sensor);
//allow = digitalRead(WasserSensorPin); Serial.println(F("------------------------------------"));
allow = true; Serial.println(F("Temperature Sensor"));
WasserSensorCheck(); Serial.print (F("Sensor Type: ")); Serial.println(sensor.name);
DruckschalterInt(); Serial.print (F("Driver Ver: ")); Serial.println(sensor.version);
Serial.print (F("Unique ID: ")); Serial.println(sensor.sensor_id);
Serial.print (F("Max Value: ")); Serial.print(sensor.max_value); Serial.println(F("°C"));
//anhängen der Pin-Interrupts Serial.print (F("Min Value: ")); Serial.print(sensor.min_value); Serial.println(F("°C"));
attachInterrupt(digitalPinToInterrupt(DruckSensorPin), DruckschalterInt, CHANGE); Serial.print (F("Resolution: ")); Serial.print(sensor.resolution); Serial.println(F("°C"));
attachInterrupt(digitalPinToInterrupt(WasserSensorPin), WasserSensorInt, CHANGE); Serial.println(F("------------------------------------"));
// Print humidity sensor details.
dht.humidity().getSensor(&sensor);
Serial.println("startup sequence complete!\n"); Serial.println(F("Humidity Sensor"));
digitalWrite(LED_BUILTIN, HIGH); Serial.print (F("Sensor Type: ")); Serial.println(sensor.name);
Serial.print (F("Driver Ver: ")); Serial.println(sensor.version);
WifiManager mang = WifiManager(); Serial.print (F("Unique ID: ")); Serial.println(sensor.sensor_id);
mang.init(); Serial.print (F("Max Value: ")); Serial.print(sensor.max_value); Serial.println(F("%"));
Serial.print (F("Min Value: ")); Serial.print(sensor.min_value); Serial.println(F("%"));
Serial.print (F("Resolution: ")); Serial.print(sensor.resolution); Serial.println(F("%"));
Serial.println(F("------------------------------------"));
// Set delay between sensor readings based on sensor details.
delayMS = sensor.min_delay / 1000;
} }
void loop() { void loop() {
// Delay between measurements.
delay(delayMS);
// Get temperature event and print its value.
sensors_event_t event;
dht.temperature().getEvent(&event);
if (isnan(event.temperature)) {
Serial.println(F("Error reading temperature!"));
}
else {
Serial.print(F("Temperature: "));
Serial.print(event.temperature);
Serial.println(F("°C"));
}
// Get humidity event and print its value.
dht.humidity().getEvent(&event);
if (isnan(event.relative_humidity)) {
Serial.println(F("Error reading humidity!"));
}
else {
Serial.print(F("Humidity: "));
Serial.print(event.relative_humidity);
Serial.println(F("%"));
}
} }