diff --git a/platformio.ini b/platformio.ini index 02e9444..bf247d0 100644 --- a/platformio.ini +++ b/platformio.ini @@ -12,3 +12,8 @@ platform = espressif8266 board = esp07 framework = arduino + +lib_deps = + SPI + Wire + DHT sensor library \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 08ef094..a71eb29 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,162 +1,78 @@ -#include -#include +#include "Arduino.h" +#include +#include +#include -#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; -bool error = false; +// See guide for details on sensor wiring and usage: +// https://learn.adafruit.com/dht/overview -/** pin config */ -static const uint8_t WasserSensorPin = 14; -static const uint8_t DruckSensorPin = 12; -static const uint8_t SchuetzPin = 13; +DHT_Unified dht(DHTPIN, DHTTYPE); -/** time config */ -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(); -} +uint32_t delayMS; 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.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(); - - - //anhängen der Pin-Interrupts - attachInterrupt(digitalPinToInterrupt(DruckSensorPin), DruckschalterInt, CHANGE); - attachInterrupt(digitalPinToInterrupt(WasserSensorPin), WasserSensorInt, CHANGE); - - - Serial.println("startup sequence complete!\n"); - digitalWrite(LED_BUILTIN, HIGH); - - WifiManager mang = WifiManager(); - mang.init(); + // Initialize device. + Serial.println("DHTxx Unified Sensor Example"); + dht.begin(); + Serial.println(F("DHTxx Unified Sensor Example")); + // Print temperature sensor details. + sensor_t sensor; + dht.temperature().getSensor(&sensor); + Serial.println(F("------------------------------------")); + Serial.println(F("Temperature Sensor")); + Serial.print (F("Sensor Type: ")); Serial.println(sensor.name); + 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")); + Serial.print (F("Min Value: ")); Serial.print(sensor.min_value); Serial.println(F("°C")); + Serial.print (F("Resolution: ")); Serial.print(sensor.resolution); Serial.println(F("°C")); + Serial.println(F("------------------------------------")); + // Print humidity sensor details. + dht.humidity().getSensor(&sensor); + Serial.println(F("Humidity Sensor")); + Serial.print (F("Sensor Type: ")); Serial.println(sensor.name); + 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("%")); + 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() { - + // 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("%")); + } } \ No newline at end of file