add new temperature class and rollback main
This commit is contained in:
		
							
								
								
									
										33
									
								
								src/Temperature.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								src/Temperature.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,33 @@
 | 
			
		||||
//
 | 
			
		||||
// Created by lukas on 22.08.20.
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
#include "Temperature.h"
 | 
			
		||||
 | 
			
		||||
void Temperature::init() {
 | 
			
		||||
    dht.begin();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
double Temperature::getTemp() {
 | 
			
		||||
    sensors_event_t event;
 | 
			
		||||
    dht.temperature().getEvent(&event);
 | 
			
		||||
    if (isnan(event.temperature)) {
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
    else {
 | 
			
		||||
        return event.temperature;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
double Temperature::getHum() {
 | 
			
		||||
    // Get humidity event and print its value.
 | 
			
		||||
    dht.humidity().getEvent(&event);
 | 
			
		||||
    if (isnan(event.relative_humidity)) {
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
    else {
 | 
			
		||||
 | 
			
		||||
       return event.relative_humidity;
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										21
									
								
								src/Temperature.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								src/Temperature.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,21 @@
 | 
			
		||||
//
 | 
			
		||||
// Created by lukas on 22.08.20.
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include <Adafruit_Sensor.h>
 | 
			
		||||
#include <DHT.h>
 | 
			
		||||
#include <DHT_U.h>
 | 
			
		||||
 | 
			
		||||
class Temperature {
 | 
			
		||||
public:
 | 
			
		||||
    Temperature(): dht(4, DHT22){};
 | 
			
		||||
 | 
			
		||||
    void init();
 | 
			
		||||
    double getTemp();
 | 
			
		||||
    double getHum();
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
    DHT_Unified dht;
 | 
			
		||||
};
 | 
			
		||||
							
								
								
									
										220
									
								
								src/main.cpp
									
									
									
									
									
								
							
							
						
						
									
										220
									
								
								src/main.cpp
									
									
									
									
									
								
							@@ -1,78 +1,162 @@
 | 
			
		||||
#include "Arduino.h"
 | 
			
		||||
#include <Adafruit_Sensor.h>
 | 
			
		||||
#include <DHT.h>
 | 
			
		||||
#include <DHT_U.h>
 | 
			
		||||
#include <Arduino.h>
 | 
			
		||||
#include <Ticker.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.
 | 
			
		||||
#include "WifiManager.h"
 | 
			
		||||
 | 
			
		||||
// Uncomment the type of sensor in use:
 | 
			
		||||
//#define DHTTYPE    DHT11     // DHT 11
 | 
			
		||||
#define DHTTYPE    DHT22     // DHT 22 (AM2302)
 | 
			
		||||
//#define DHTTYPE    DHT21     // DHT 21 (AM2301)
 | 
			
		||||
#define VERSION "v1.1"
 | 
			
		||||
 | 
			
		||||
// See guide for details on sensor wiring and usage:
 | 
			
		||||
//   https://learn.adafruit.com/dht/overview
 | 
			
		||||
bool allow;
 | 
			
		||||
bool error = false;
 | 
			
		||||
 | 
			
		||||
DHT_Unified dht(DHTPIN, DHTTYPE);
 | 
			
		||||
/** pin config */
 | 
			
		||||
static const uint8_t WasserSensorPin = 14;
 | 
			
		||||
static const uint8_t DruckSensorPin = 12;
 | 
			
		||||
static const uint8_t SchuetzPin = 13;
 | 
			
		||||
 | 
			
		||||
/** 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);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
uint32_t delayMS;
 | 
			
		||||
 | 
			
		||||
void setup() {
 | 
			
		||||
    Serial.begin(9600);
 | 
			
		||||
    // 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;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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() {
 | 
			
		||||
    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();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
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("%"));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user