This commit is contained in:
andrea 2024-06-28 01:01:44 +08:00
parent 1a076b226b
commit de6a48a730

View File

@ -5,15 +5,13 @@
#include <DHT.h> #include <DHT.h>
#include <ESP8266WebServer.h> #include <ESP8266WebServer.h>
#define DHT1_PIN 11 #define DHT_PIN 11
#define DHT2_PIN 12 #define MOSFET_HEATING_PIN 13
#define RELAY_COOLING_PIN 13 #define MOSFET_COOLING_PIN 14
#define RELAY_HEATING_PIN 14
#define DHT_TYPE DHT11 #define DHT_TYPE DHT11
DHT dht1(DHT1_PIN, DHT_TYPE); DHT dht(DHT_PIN, DHT_TYPE);
DHT dht2(DHT2_PIN, DHT_TYPE);
LiquidCrystal_I2C lcd(0x27, 20, 4); LiquidCrystal_I2C lcd(0x27, 20, 4);
ESP8266WebServer server(80); ESP8266WebServer server(80);
@ -28,21 +26,20 @@ float targetTemperature = 25.0;
unsigned long startTime = 0; unsigned long startTime = 0;
unsigned long duration = 0; unsigned long duration = 0;
float surfaceTemp = 0.0; float fridgeTemp = 0.0;
float airTemp = 0.0;
float surfaceHum = 0.0; bool heatingActive = false;
float airHum = 0.0; bool coolingActive = false;
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
pinMode(RELAY_COOLING_PIN, OUTPUT); pinMode(MOSFET_HEATING_PIN, OUTPUT);
pinMode(RELAY_HEATING_PIN, OUTPUT); pinMode(MOSFET_COOLING_PIN, OUTPUT);
digitalWrite(RELAY_COOLING_PIN, LOW); analogWriteRange(255); // Set PWM range to 0-255
digitalWrite(RELAY_HEATING_PIN, LOW); analogWriteFreq(20000); // Set PWM frequency to 20 kHz
dht1.begin(); dht.begin();
dht2.begin();
lcd.begin(20,4); lcd.begin(20,4);
lcd.backlight(); lcd.backlight();
@ -75,7 +72,7 @@ void setup() {
void loop() { void loop() {
server.handleClient(); server.handleClient();
updateSensors(); updateSensors();
controlRelays(); controlPeltier();
updateLCD(); updateLCD();
switch (currentMode) { switch (currentMode) {
@ -89,56 +86,59 @@ void loop() {
curveMode(); curveMode();
break; break;
case OFF: case OFF:
disableRelays(); disablePeltier();
break; break;
} }
} }
void updateSensors() { void updateSensors() {
surfaceTemp = dht1.readTemperature(); fridgeTemp = dht.readTemperature();
surfaceHum = dht1.readHumidity(); Serial.printf("Fridge Temp: %.1f C\n", fridgeTemp);
airTemp = dht2.readTemperature();
airHum = dht2.readHumidity();
Serial.printf("Surface Temp: %.1f C, Air Temp: %.1f C\n", surfaceTemp, airTemp);
} }
void enableCooling() { void controlHeating(int dutyCycle) {
digitalWrite(RELAY_HEATING_PIN, LOW); analogWrite(MOSFET_COOLING_PIN, 0); // Ensure cooling is off
delay(100); analogWrite(MOSFET_HEATING_PIN, dutyCycle);
digitalWrite(RELAY_COOLING_PIN, HIGH); heatingActive = true;
coolingActive = false;
} }
void enableHeating() { void controlCooling(int dutyCycle) {
digitalWrite(RELAY_COOLING_PIN, LOW); analogWrite(MOSFET_HEATING_PIN, 0); // Ensure heating is off
delay(100); analogWrite(MOSFET_COOLING_PIN, dutyCycle);
digitalWrite(RELAY_HEATING_PIN, HIGH); coolingActive = true;
heatingActive = false;
} }
void disableRelays() { void disablePeltier() {
digitalWrite(RELAY_COOLING_PIN, LOW); analogWrite(MOSFET_HEATING_PIN, 0);
digitalWrite(RELAY_HEATING_PIN, LOW); analogWrite(MOSFET_COOLING_PIN, 0);
heatingActive = false;
coolingActive = false;
} }
void controlRelays() { void controlPeltier() {
if (currentMode == OFF) { if (currentMode == OFF) {
disableRelays(); disablePeltier();
return; return;
} }
if (targetTemperature < 25) { if (targetTemperature < 25) {
if (airTemp > targetTemperature) { if (fridgeTemp > targetTemperature) {
enableCooling(); int dutyCycle = map(fridgeTemp - targetTemperature, 0, 10, 0, 255);
controlCooling(dutyCycle);
} else { } else {
disableRelays(); disablePeltier();
} }
} else if (targetTemperature > 25) { } else if (targetTemperature > 25) {
if (airTemp < targetTemperature) { if (fridgeTemp < targetTemperature) {
enableHeating(); int dutyCycle = map(targetTemperature - fridgeTemp, 0, 10, 0, 255);
controlHeating(dutyCycle);
} else { } else {
disableRelays(); disablePeltier();
} }
} else { } else {
disableRelays(); disablePeltier();
} }
} }
@ -150,16 +150,11 @@ void updateLCD() {
lcd.print(" C"); lcd.print(" C");
lcd.setCursor(0, 1); lcd.setCursor(0, 1);
lcd.print("Surface Temp: "); lcd.print("Fridge Temp: ");
lcd.print(surfaceTemp, 1); lcd.print(fridgeTemp, 1);
lcd.print(" C"); lcd.print(" C");
lcd.setCursor(0, 2); lcd.setCursor(0, 2);
lcd.print("Air Temp: ");
lcd.print(airTemp, 1);
lcd.print(" C");
lcd.setCursor(0, 3);
lcd.print("Mode: "); lcd.print("Mode: ");
switch (currentMode) { switch (currentMode) {
case TIME_MODE: case TIME_MODE:
@ -170,41 +165,13 @@ void updateLCD() {
break; break;
case CURVE_MODE: case CURVE_MODE:
lcd.print("Curve Mode"); lcd.print("Curve Mode");
lcd.setCursor(0, 3);
lcd.print("Elapsed: ");
lcd.print((millis() - startTime) / 1000, 1);
lcd.print(" s");
break; break;
case OFF: case OFF:
default:
lcd.print("Off"); lcd.print("Off");
break; break;
} }
} }
void timeMode() {
if (millis() - startTime >= duration * 3600 * 1000) {
currentMode = OFF;
}
controlRelays();
}
void holdMode() {
controlRelays();
}
void curveMode() {
float elapsedTime = (millis() - startTime) / 1000.0;
float gaussValue = exp(-pow((elapsedTime - duration / 2.0), 2) / (2 * pow(duration / 6.0, 2)));
targetTemperature = targetTemperature * gaussValue;
if (elapsedTime >= duration) {
currentMode = OFF;
}
controlRelays();
}
void handleRoot() { void handleRoot() {
String html = R"rawliteral( String html = R"rawliteral(
<!DOCTYPE html> <!DOCTYPE html>