// Relay.ino
//
// 23.20.2022, Angelika Iskra
//
// Haier 8kW - płynne przejście z CWU na CO
//
const int relayPinCWU = 13;
const int relayPinCO = 12;
const int relayPin3D = 11;
const float BETA = 3950;
enum state {
CO = 0,
CWU = 1,
CWUToCO = 2
};
enum state currentState = CO;
boolean cwuOn = true;
boolean coOn = true;
float heatingTemperature = 42;
float heatedTankTemperature = 46;
unsigned long timestamp;
const unsigned long interval = 18000;
const unsigned int MAX_MESSAGE_LENGTH = 12;
void setup()
{
Serial.begin(9600);
pinMode(relayPinCWU, OUTPUT);
pinMode(relayPinCO, OUTPUT);
pinMode(relayPin3D, OUTPUT);
changeToCO();
}
void loop()
{
int analogValue = analogRead(A0);
float temperature = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
Serial.print("Temperatura: ");
Serial.print(temperature);
Serial.print(" ℃. Tryb: ");
switch (currentState) {
case CO: Serial.println("centralne ogrzewanie budynku (CO)."); break;
case CWU: Serial.println("grzanie wody użytkowej (CWU)."); break;
case CWUToCO: Serial.println("przełączanie z CWU na CO."); break;
default: Serial.println("nieznany.");
}
if (cwuOn && temperature < heatingTemperature && currentState == CO) {
changeToCWU();
} else if (temperature >= heatedTankTemperature && (currentState == CWU || currentState == CWUToCO)) {
changeToCO();
}
readInput();
delay(600);
}
void changeToCO() {
unsigned long currentMillis = millis();
if (currentState == CWU) {
digitalWrite(relayPin3D, LOW);
timestamp = currentMillis;
currentState = CWUToCO;
} else if (currentState == CWUToCO && currentMillis - timestamp >= interval) {
digitalWrite(relayPinCO, HIGH);
digitalWrite(relayPinCWU, LOW);
currentState = CO;
}
}
void changeToCWU() {
if (!cwuOn) return;
digitalWrite(relayPinCO, LOW);
digitalWrite(relayPinCWU, HIGH);
digitalWrite(relayPin3D, HIGH);
currentState = CWU;
}
void readInput() {
while (Serial.available() > 0)
{
static char message[MAX_MESSAGE_LENGTH];
static unsigned int message_pos = 0;
//Read the next available byte in the serial receive buffer
char inByte = Serial.read();
//Message coming in (check not terminating character) and guard for over message size
if (inByte != '\n' && (message_pos < MAX_MESSAGE_LENGTH - 1)) {
message[message_pos] = inByte;
message_pos++;
} else {
message[message_pos] = '\0';
handleInput(message);
message_pos = 0;
}
}
}
void handleInput(String message) {
Serial.println(message);
if (message == "cwu on") {
cwuOn = true;
} else if (message == "cwu off") {
cwuOn = false;
} else if (message == "co on") {
coOn = true;
} else if (message == "co off") {
coOn = false;
} else if (message.indexOf("heatingTemp") > 0) {
message.remove(0, 12);
int newTemp = (int)message;
Serial.print("newTemp ");
Serial.println(newTemp);
}
}CWU
CO
Zawór 3D