#include <DHT.h>
#define DHTPIN 2
#define DHTTYPE DHT22
#define LED_R_PIN GPIO_PIN_5
#define LED_G_PIN GPIO_PIN_6
DHT dht(DHTPIN, DHTTYPE);
// Actuator Pins
const int HEATER_PIN = 8; // PA9
const int HUMIDIFIER_PIN = 10; // PA15 (D10)
int T_curr = 0;
int H_curr = 0;
int T_min_set = 200;
int T_max_set = 250;
int H_min_set = 60;
int H_max_set = 75;
void read_serial_input() {
if (Serial.available() > 0) {
String inputString = Serial.readStringUntil('\n');
inputString.trim();
inputString.toUpperCase();
int separatorIndex = inputString.indexOf('=');
if (separatorIndex > 0) {
String command = inputString.substring(0, separatorIndex);
String valueStr = inputString.substring(separatorIndex + 1);
int newValue = valueStr.toInt();
bool valueChanged = false;
if (command == "TMAX" && newValue >= 150 && newValue <= 400) {
T_max_set = newValue;
valueChanged = true;
} else if (command == "TMIN" && newValue >= 150 && newValue <= 400) {
T_min_set = newValue;
valueChanged = true;
} else if (command == "HMAX" && newValue >= 50 && newValue <= 100) {
H_max_set = newValue;
valueChanged = true;
} else if (command == "HMIN" && newValue >= 10 && newValue <= 90) {
H_min_set = newValue;
valueChanged = true;
}
// --- ЗВОРОТНИЙ ЗВ'ЯЗОК ---
if (valueChanged) {
Serial.print("\n--- NEW VALUE SET ---");
Serial.print("\n");
Serial.print(command);
Serial.print(" set to: ");
if (command.startsWith("T")) {
Serial.print(newValue / 10.0);
Serial.println(" C.");
} else {
Serial.print(newValue);
Serial.println(" %.");
}
} else {
Serial.println("\nInvalid command or value range.");
}
} else if (inputString.length() > 0) {
Serial.println("\nInvalid format. Use COMMAND=VALUE (e.g., TMAX=280).");
}
}
}
void setup() {
Serial.begin(9600);
delay(100);
Serial.println("\n--- Greenhouse Control System (FINAL ACTIVE-HIGH MODE) ---");
Serial.println("Use commands: TMAX=280, TMIN=200, HMAX=75, HMIN=60 (T values x10).");
dht.begin();
pinMode(HEATER_PIN, OUTPUT);
pinMode(HUMIDIFIER_PIN, OUTPUT);
pinMode(LED_R_PIN, OUTPUT);
pinMode(LED_G_PIN, OUTPUT);
// HEATER (Active-HIGH) початковий стан OFF (LOW)
digitalWrite(HEATER_PIN, LOW);
// HUMIDIFIER (Active-HIGH) початковий стан OFF (LOW)
digitalWrite(HUMIDIFIER_PIN, LOW);
digitalWrite(LED_R_PIN, LOW);
digitalWrite(LED_G_PIN, LOW);
}
void read_sensor_data() {
float raw_T = dht.readTemperature();
float raw_H = dht.readHumidity();
if (isnan(raw_H) || isnan(raw_T)) {
Serial.println("DHT Error! Using default values.");
T_curr = T_min_set;
H_curr = H_min_set;
} else {
T_curr = (int)(raw_T * 10);
H_curr = (int)raw_H;
}
}
void control_actuators() {
// 1. Управління Нагрівачем (Active-HIGH: HIGH=ON, LOW=OFF)
if (T_curr < T_min_set) {
digitalWrite(HEATER_PIN, HIGH); // ON
} else if (T_curr >= T_max_set) {
digitalWrite(HEATER_PIN, LOW); // OFF
}
// 2. Управління Зволожувачем (Active-HIGH: HIGH=ON, LOW=OFF)
if (H_curr < H_min_set) {
digitalWrite(HUMIDIFIER_PIN, HIGH); // ON
} else if (H_curr >= H_max_set) {
digitalWrite(HUMIDIFIER_PIN, LOW); // OFF
}
}
void critical_indication() {
digitalWrite(LED_R_PIN, LOW);
digitalWrite(LED_G_PIN, LOW);
if (T_curr > T_max_set + 10 || H_curr > H_max_set + 5) {
digitalWrite(LED_R_PIN, HIGH);
} else if (T_curr < T_min_set - 10 || H_curr < H_min_set - 5) {
digitalWrite(LED_R_PIN, HIGH);
digitalWrite(LED_G_PIN, HIGH);
} else {
digitalWrite(LED_G_PIN, HIGH);
}
}
void display_data_serial() {
Serial.println("\n--------------------------------");
Serial.println(" SYSTEM STATUS");
Serial.println("--------------------------------");
Serial.print("Current T: "); Serial.print(T_curr / 10.0); Serial.println(" C");
Serial.print("Current H: "); Serial.print(H_curr); Serial.println(" %");
Serial.println("--- SET POINTS ---");
Serial.print("T Min/Max: "); Serial.print(T_min_set / 10.0); Serial.print(" / "); Serial.print(T_max_set / 10.0); Serial.println(" C");
Serial.print("H Min/Max: "); Serial.print(H_min_set); Serial.print(" / "); Serial.print(H_max_set); Serial.println(" %");
Serial.println("--- ACTUATORS ---");
Serial.print("Heater Status: "); Serial.println(digitalRead(HEATER_PIN) == HIGH ? "ON" : "OFF");
Serial.print("Humidifier Status: "); Serial.println(digitalRead(HUMIDIFIER_PIN) == HIGH ? "ON" : "OFF");
}
void loop() {
read_serial_input();
read_sensor_data();
control_actuators();
critical_indication();
display_data_serial();
delay(10000);
}