#include <DHT.h>
#define DHTPIN 32 // Pin, an dem der DHT22-Sensor angeschlossen ist
#define DHTTYPE DHT22 // DHT22 (AM2302) Sensor
const int pin = 12;
const int pinWlanLedOn = 27;
const int pinWlanLedOff = 14;
const int pinContact = 13;
DHT dht(DHTPIN, DHTTYPE);
// Shows the WLAN led (active = green, inactive = red)
void setWlanLed(bool on) {
if (on) {
if (digitalRead(pinWlanLedOn) == LOW) {
digitalWrite(pinWlanLedOn, HIGH);
}
if (digitalRead(pinWlanLedOff) == HIGH) {
digitalWrite(pinWlanLedOff, LOW);
}
} else {
if (digitalRead(pinWlanLedOn) == HIGH) {
digitalWrite(pinWlanLedOn, LOW);
}
if (digitalRead(pinWlanLedOff) == LOW) {
digitalWrite(pinWlanLedOff, HIGH);
}
}
}
// Shows the Contact led (if DHT sends HIGH)
void setContactLed(bool on) {
if (on) {
if (digitalRead(pinContact) == LOW) {
digitalWrite(pinContact, HIGH);
}
} else {
if (digitalRead(pinContact) == HIGH) {
digitalWrite(pinContact, LOW);
}
}
}
float* readDHT() {
float *result = new float[2];
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
if (isnan(humidity) || isnan(temperature)) {
result[0] = -1;
result[1] = -1;
} else {
result[0] = temperature;
result[1] = humidity;
}
return result;
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
pinMode(pin, INPUT); // Setzt den Pin-Modus auf Eingang mit internem Pullup-Widerstand
pinMode(pinContact, OUTPUT);
pinMode(pinWlanLedOn, OUTPUT); // Setzt den Pin-Modus auf Eingang mit internem Pullup-Widerstand
pinMode(pinWlanLedOff, OUTPUT); // Setzt den Pin-Modus auf Eingang mit internem Pullup-Widerstand
}
void loop() {
// put your main code here, to run repeatedly:
int inputValue = digitalRead(pin);
Serial.print("Input value: ");
Serial.println(inputValue);
setWlanLed(true);
delay(1000); // this speeds up the simulation
setWlanLed(false);
delay(1000); // this speeds up the simulation
float *measurements = readDHT();
Serial.print("Temperature: ");
Serial.print(measurements[0]);
Serial.print(" *C, Humidity: ");
Serial.print(measurements[1]);
Serial.println(" %");
delete[] measurements;
setContactLed(true);
}