#include "DHT.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define DHTPIN 2
#define DHTTYPE DHT22
#define DHTPIN1 3
#define DHTTYPE1 DHT22
#define joyX A0
#define joyY A1
DHT dht(DHTPIN, DHTTYPE);
DHT dht1(DHTPIN1, DHTTYPE1);
const int relayPin= 4;
float relHumOut;
float tempOut;
float relHumIn;
float tempIn;
float absHumOut;
float absHumIn;
double satVaporPressureOut;
float satVaporPressureIn;
double powerNumOut;
float powerNumIn;
double altitude = 30;
double universal_gas_constant = 8.314;
double molecular_weight_water_vapor = 18.016;
double actual_vapor_pressure(double temperature, double relative_humidity) {
const double hPa = 6.11;
const double a = 17.27;
const double b = 237.7;
return (relative_humidity / 100) * (hPa * exp(a * temperature / (temperature + b)));
}
double dew_point(double temperature, double relative_humidity) {
double actual_vap = actual_vapor_pressure(temperature, relative_humidity);
return (237.3 * log(actual_vap / 6.11)) / (17.27 - log(actual_vap / 6.11));
}
double absolute_humidity(double temperature, double humidity, double dew_point){
double satVapPre = (6.1078 * pow(10, ((7.5 * temperature) / (237.3 + dew_point))));
double actVapPre = (satVapPre * (humidity / 100));
double firstPart = 216.7 * actVapPre;
double secondPart = temperature + 273.15;
return firstPart / secondPart;
}
void dew_point_scrn(){
lcd.setCursor(0, 0);
lcd.print("DPOut: ");
lcd.setCursor(0, 1);
lcd.print(String(dew_point(tempOut, relHumOut)));
lcd.setCursor(10, 0);
lcd.print("DPin: ");
lcd.setCursor(10, 1);
lcd.print(String(dew_point(tempIn, relHumIn)));
}
void temp_scrn(){
lcd.setCursor(0, 0);
lcd.print("TempOut: ");
lcd.setCursor(0, 1);
lcd.print(tempOut);
lcd.setCursor(10, 0);
lcd.print("TempIn: ");
lcd.setCursor(10, 1);
lcd.print(tempIn);
}
void hum_scrn(){
lcd.setCursor(0, 0);
lcd.print("HumOut: ");
lcd.setCursor(0, 1);
lcd.print(relHumOut);
lcd.setCursor(10, 0);
lcd.print("HumIn: ");
lcd.setCursor(10, 1);
lcd.print(relHumIn);
}
void abs_hum_scrn(){
lcd.setCursor(0, 0);
lcd.print("AbsOut: ");
lcd.setCursor(0, 1);
lcd.print(absolute_humidity(tempOut, relHumOut, dew_point(tempOut, relHumOut)));
lcd.setCursor(10, 0);
lcd.print("AbsIn: ");
lcd.setCursor(10, 1);
lcd.print(absolute_humidity(tempIn, relHumIn, dew_point(tempIn, relHumIn)));
}
void lueften(double afau, double afin, double tau, double tin){
Serial.println(afau);
Serial.println(afin);
double afdiff = afau - afin;
Serial.println(afdiff);
if(tau < 3.0){
lcd.print("Nicht lueften");
}else if( -1 < afdiff < 1){
lcd.print("Kleine Diff.");
}else if(afau > afin){
lcd.print("Nicht lueften");
}else{
lcd.print("Bitte lueften");
}
}
//int display_mode = 0;
//int prev_x_value = 0;
/*void scrn_change() {
int xValue = analogRead(joyX);
if (xValue != prev_x_value) {
// Update the display mode based on the value of xValue
if (xValue == 1023) {
display_mode = 0;
} else if (xValue < 768 && xValue > 0) {
display_mode = 1;
} else {
display_mode = 2;
}
// Call the appropriate function based on the display mode
switch (display_mode) {
case 0:
temp_scrn();
break;
case 1:
hum_scrn();
break;
case 2:
dew_point_scrn();
break;
}
// Update the prev_x_value variable with the current xValue
prev_x_value = xValue;
}
}*/
void setup() {
Serial.begin(9600);
dht.begin();
dht1.begin();
lcd.init();
lcd.backlight();
lcd.clear();
pinMode(buttonPin, INPUT);
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(relayPin, OUTPUT);
}
void loop() {
relHumOut = dht.readHumidity();
tempOut = dht.readTemperature();
relHumIn = dht1.readHumidity();
tempIn = dht1.readTemperature();
double x = 0;
lueften(absolute_humidity(tempOut, relHumOut, dew_point(tempOut, relHumOut)), absolute_humidity(tempIn, relHumIn, dew_point(tempIn, relHumIn)), tempOut, tempIn);
digitalWrite(relayPin, HIGH);
delay(3000);
}