#define BLYNK_TEMPLATE_ID "TMPL4d4gjHwsR"
#define BLYNK_TEMPLATE_NAME "Gewächshaus"
#define BLYNK_AUTH_TOKEN "l56l2tFgVg14e9EY6IqO_ynR23HHO7-4"
// Inkludiere notwendige Bibliotheken
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <DHT.h>
#include <HX711.h>
#include <Wire.h>
#include <RTClib.h>
// WLAN Verbindung
const char* ssid = "FRITZ!Box 7490_EXT";
const char* pass = "brightwindow779";
// Sensor- und Aktoranschlüsse
#define DHT_PIN 14
#define DHT_TYPE DHT22
#define PUMP_1 5
#define PUMP_2 4
#define PUMP_3 12
#define PUMP_4 13
#define LED_PIN 2
#define SDA_PIN 21 // RTC SDA
#define SCL_PIN 22 // RTC SCL
//#define WATERLEVEL_SENSOR 23 //Füllstandssensor für Wassertank
//Waage 1
const int LOADCELL_DOUT_PIN_1 = 14; // Data pin (DT)
const int LOADCELL_SCK_PIN_1 = 27; // Clock pin (SCK)
//Waage 2
const int LOADCELL_DOUT_PIN_2 = 26; // Data pin (DT)
const int LOADCELL_SCK_PIN_2 = 25; // Clock pin (SCK)
//Waage 3
const int LOADCELL_DOUT_PIN_3 = 39; // Data pin (DT) - VN
const int LOADCELL_SCK_PIN_3 = 36; // Clock pin (SCK) - VP
//Waage 4
const int LOADCELL_DOUT_PIN_4 = 19; // Data pin (DT)
const int LOADCELL_SCK_PIN_4 = 18; // Clock pin (SCK)
// Blynk virtuelle Pins
#define VPIN_WEIGHT_1 V2
#define VPIN_WEIGHT_2 V3
#define VPIN_WEIGHT_3 V4
#define VPIN_WEIGHT_4 V5
#define VPIN_HUMIDITY V1
#define VPIN_TEMPERATURE V0
#define VPIN_THRESHOLD V8
#define VPIN_RUNTIME V9
#define VPIN_LED_ON_HOUR V10
#define VPIN_LED_OFF_HOUR V11
#define VPIN_PUMP_MANUAL V12
// HX711 Waagen
HX711 scale1;
HX711 scale2;
HX711 scale3;
HX711 scale4;
float calibration_factor = 192.45;
// DHT Sensor
DHT dht(DHT_PIN, DHT_TYPE);
// RTC
RTC_DS3231 rtc;
// Pumpenstatus
bool pumpActive = false;
unsigned long lastPumpTime = 0;
int threshold = 0;
int runtime = 60;
int ledOnHour = 0;
int ledOffHour = 0;
bool pumpManual = true;
void setup() {
// Serielle Kommunikation starten
Serial.begin(115200);
// Waagen initialisieren
scale1.begin(LOADCELL_DOUT_PIN_1, LOADCELL_SCK_PIN_1);
scale2.begin(LOADCELL_DOUT_PIN_2, LOADCELL_SCK_PIN_2);
scale3.begin(LOADCELL_DOUT_PIN_3, LOADCELL_SCK_PIN_3);
scale4.begin(LOADCELL_DOUT_PIN_4, LOADCELL_SCK_PIN_4);
scale1.set_scale(calibration_factor);
scale2.set_scale(calibration_factor);
scale3.set_scale(calibration_factor);
scale4.set_scale(calibration_factor);
// DHT-Sensor starten
dht.begin();
// RTC starten
if (!rtc.begin()) {
Serial.println("RTC nicht gefunden");
while (1);
}
// WLAN und Blynk starten
WiFi.begin(ssid, pass);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
// Aktoren-Pins konfigurieren
pinMode(PUMP_1, OUTPUT);
pinMode(PUMP_2, OUTPUT);
pinMode(PUMP_3, OUTPUT);
pinMode(PUMP_4, OUTPUT);
pinMode(LED_PIN, OUTPUT);
//pinMode(WATERLEVEL_SENSOR, INPUT);
// Alle Aktoren initial ausschalten
digitalWrite(PUMP_1, LOW);
digitalWrite(PUMP_2, LOW);
digitalWrite(PUMP_3, LOW);
digitalWrite(PUMP_4, LOW);
digitalWrite(LED_PIN, LOW);
}
void loop() {
Blynk.run();
checkWiFiConnection();
// DHT Messungen
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
//Wasserlevel Messung
//int WATERLEVEL_VALUE = digitalRead(WATERLEVEL_SENSOR);
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Fehler beim Lesen vom DHT-Sensor");
} else {
Blynk.virtualWrite(VPIN_HUMIDITY, humidity);
Blynk.virtualWrite(VPIN_TEMPERATURE, temperature);
}
// if (WATERLEVEL_SENSOR == HIGH) {
// } else {
//}
// Waagen Messungen
float weight1 = scale1.get_units(5);
float weight2 = scale2.get_units(5);
float weight3 = scale3.get_units(5);
float weight4 = scale4.get_units(5);
Blynk.virtualWrite(VPIN_WEIGHT_1, weight1);
Blynk.virtualWrite(VPIN_WEIGHT_2, weight2);
Blynk.virtualWrite(VPIN_WEIGHT_3, weight3);
Blynk.virtualWrite(VPIN_WEIGHT_4, weight4);
// Pumpensteuerung
if (!pumpManual && millis() - lastPumpTime > 3600000) { // 1 Stunde Wartezeit
if (weight1 < threshold || weight2 < threshold || weight3 < threshold || weight4 < threshold) {
activatePumps(runtime);
}
}
// LED Steuerung
DateTime now = rtc.now();
if (now.hour() == ledOnHour) {
digitalWrite(LED_PIN, HIGH);
}
if (now.hour() == ledOffHour) {
digitalWrite(LED_PIN, LOW);
}
}
void activatePumps(int duration) {
digitalWrite(PUMP_1, HIGH);
digitalWrite(PUMP_2, HIGH);
digitalWrite(PUMP_3, HIGH);
digitalWrite(PUMP_4, HIGH);
delay(duration * 1000);
digitalWrite(PUMP_1, LOW);
digitalWrite(PUMP_2, LOW);
digitalWrite(PUMP_3, LOW);
digitalWrite(PUMP_4, LOW);
lastPumpTime = millis();
}
BLYNK_WRITE(VPIN_THRESHOLD) {
threshold = param.asInt();
}
BLYNK_WRITE(VPIN_RUNTIME) {
runtime = param.asInt();
}
BLYNK_WRITE(VPIN_LED_ON_HOUR) {
ledOnHour = param.asInt();
}
BLYNK_WRITE(VPIN_LED_OFF_HOUR) {
ledOffHour = param.asInt();
}
BLYNK_WRITE(VPIN_PUMP_MANUAL) {
pumpManual = param.asInt();
}
void checkWiFiConnection() {
if (WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi getrennt. Versuche Verbindung wiederherzustellen...");
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi verbunden.");
Blynk.connect();
}
}