#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#include <ESP32Servo.h>
#include <WiFi.h>
#include <PubSubClient.h>
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int servoPin = 15;
Servo servo;
float globalCO2 = 0; // Globale Variable für den CO2-Wert
bool servoActive = false; // Zustand des Servos
// WiFi Credentials
const char* ssid = "Wokwi-GUEST"; // Dein WiFi-SSID
const char* password = ""; // Dein WiFi-Passwort
// MQTT Server
const char* mqtt_server = "test.mosquitto.org";
WiFiClient espClient;
PubSubClient client(espClient);
// Globale Variablen für Temperatur und Feuchtigkeit
float globalTemperature = 0;
float globalHumidity = 0;
void setup() {
Serial.begin(9600);
dht.begin();
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Temp: C");
lcd.setCursor(0, 1);
lcd.print("Humi: %");
servo.attach(servoPin, 500, 2400);
// WiFi und MQTT
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
reconnect();
}
void loop() {
// Falls MQTT nicht verbunden ist, versuche zu reconnecten
if (!client.connected()) {
reconnect();
}
client.loop();
// Temperaturüberprüfung und Servosteuerung
if (globalTemperature > 25) {
if (!servoActive) {
servoActive = true;
}
// Servo kontinuierlich drehen lassen
for (int pos = 0; pos <= 180; pos++) {
servo.write(pos);
delay(15);
}
for (int pos = 180; pos >= 0; pos--) {
servo.write(pos);
delay(15);
}
} else {
if (servoActive) {
servo.write(0); // Servo auf Ausgangsposition setzen
servoActive = false;
}
}
// CO2-Warnung
if (globalCO2 > 1000) {
lcd.setCursor(0, 1);
lcd.print("Mach Fenster auf");
} else {
lcd.setCursor(0, 1);
lcd.print("Humi: %");
lcd.setCursor(7, 1);
lcd.print(globalHumidity);
}
// LCD mit Temperatur und Feuchtigkeit aktualisieren
lcd.setCursor(7, 0);
lcd.print(globalTemperature);
lcd.setCursor(7, 1);
lcd.print(globalHumidity);
lcd.setCursor(14, 1);
lcd.print("%");
delay(1000); // Kurze Verzögerung, um die Ausgabe lesbar zu machen
}
void callback(char* topic, byte* payload, unsigned int length) {
// Erstelle eine temporäre Kopie der Nutzdaten
char message[length + 1];
strncpy(message, (char*)payload, length);
message[length] = '\0';
String topicStr = String(topic);
if (topicStr == "/ThinkIOT/temp") {
globalTemperature = atof(message);
} else if (topicStr == "/ThinkIOT/hum") {
globalHumidity = atof(message);
} else if (topicStr == "/ThinkIOT/co2") {
globalCO2 = atof(message);
}
Serial.print("Received [");
Serial.print(topicStr);
Serial.print("]: ");
Serial.println(message);
Serial.print("Global Temperature: ");
Serial.println(globalTemperature);
Serial.print("Global Humidity: ");
Serial.println(globalHumidity);
Serial.print("Global CO2: ");
Serial.println(globalCO2);
}
void reconnect() {
while (!client.connected()) {
if (client.connect("ESP32Client")) {
client.subscribe("/ThinkIOT/temp");
client.subscribe("/ThinkIOT/hum");
client.subscribe("/ThinkIOT/co2");
} else {
delay(5000);
}
}
}