Cool/Cool.ino

204 lines
6.4 KiB
Arduino
Raw Normal View History

2022-03-27 22:39:54 +02:00
/********************************************************************/
// First we include the libraries
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Controllino.h>
#include <SPI.h>
#include <Ethernet.h>
/********************************************************************/
// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 20
/********************************************************************/
// Setup a oneWire instance to communicate with any OneWire devices
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
/********************************************************************/
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
/********************************************************************/
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x09, 0x70 };
IPAddress ip(192,168,178,57);
EthernetServer server(80);
float r,t;
const int vent = 22;
const int compressor = 23;
const long interval = 5000; // interval for measurement (milliseconds)
unsigned long previousMillis = 0;
const long agitPrerun = 60000;
unsigned long previousMillisagitPrerun = 0;
const long maxCool = 1800000;
unsigned long previousMillismaxCool = 0;
const long agitPostrun = 180000;
unsigned long previousMillisagitPostrun = 0;
float temperatureA; // temperature in Celsius
float DeltaT = 1.5;
float temperatureSP = 10.0;
int stepAct = 0;
int stepActLC = 0;
int tempReached = 0;
void setup(void)
{
pinMode(vent, OUTPUT);
pinMode(compressor, OUTPUT);
// start serial port
Serial.begin(9600);
Serial.println("Dallas Temperature IC Control Library Demo");
// Start up the library
sensors.begin();
Ethernet.begin(mac, ip);
server.begin();
Serial.print("Server gestartet. IP: ");
// IP des Arduino-Servers ausgeben
Serial.println(Ethernet.localIP());
}
void loop(void)
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
/********************************************************************/
Serial.print(" Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperature readings
Serial.println("DONE");
/********************************************************************/
temperatureA = sensors.getTempCByIndex(0);
Serial.print("Temperature is: ");
Serial.println(temperatureA);
Serial.print("StepAct: ");
Serial.println(stepAct);
Serial.println((currentMillis - previousMillismaxCool)/1000);
}
if (stepAct <= 0 || stepAct > 4){
stepAct = 1;
}
else if (stepAct == 1){ // Waiting
if (temperatureA > (temperatureSP + DeltaT)){
stepAct++;
}
digitalWrite(vent, LOW);
digitalWrite(compressor, LOW);
}
else if (stepAct == 2){ // Pre run agitators for 1min.
if (stepAct != stepActLC){
previousMillisagitPrerun = currentMillis;
stepActLC = stepAct;
}
if (currentMillis - previousMillisagitPrerun >= agitPrerun) {
previousMillisagitPrerun = currentMillis;
stepAct++;
}
digitalWrite(vent, HIGH);
digitalWrite(compressor, LOW);
}
else if (stepAct == 3){ // Cooling until temperature reached, or max time
if (stepAct != stepActLC){
previousMillismaxCool = currentMillis;
stepActLC = stepAct;
}
if (currentMillis - previousMillismaxCool >= maxCool || temperatureA < temperatureSP) {
previousMillismaxCool = currentMillis;
stepAct++;
}
if (temperatureA < temperatureSP){
tempReached = 1;
}
else {
tempReached = 0;
}
digitalWrite(vent, HIGH);
digitalWrite(compressor, HIGH);
}
else if (stepAct == 4){ // Postrun
if (stepAct != stepActLC){
previousMillisagitPostrun = currentMillis;
stepActLC = stepAct;
}
if (currentMillis - previousMillisagitPostrun >= agitPostrun) {
previousMillisagitPostrun = currentMillis;
if (tempReached == 1){
stepAct = 1;
}
else{
stepAct--;
}
}
digitalWrite(vent, HIGH);
digitalWrite(compressor, LOW);
}
// ETHERNET
EthernetClient client = server.available();
// Wenn es einen Client gibt, dann...
if (client) {
Serial.println("Neuer Client");
// Jetzt solange Zeichen lesen, bis eine leere Zeile empfangen wurde
// HTTP Requests enden immer mit einer leeren Zeile
boolean currentLineIsBlank = true;
// Solange Client verbunden
while (client.connected()) {
// client.available() gibt die Anzahl der Zeichen zurück, die zum Lesen
// verfügbar sind
if (client.available()) {
// Ein Zeichen lesen und am seriellen Monitor ausgeben
char c = client.read();
Serial.write(c);
// In currentLineIsBlank merken wir uns, ob diese Zeile bisher leer war.
// Wenn die Zeile leer ist und ein Zeilenwechsel (das \n) kommt,
// dann ist die Anfrage zu Ende und wir können antworten
if (c == '\n' && currentLineIsBlank) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // Verbindung wird nach Antwort beendet
client.println("Refresh: 30"); // Seite alle 25 Sekunden neu abfragen
client.println();
// Ab hier berginnt der HTML-Code, der an den Browser geschickt wird
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<head><title>CONTROLLINO Analog Input</title></head>");
client.println("<body><h3>CONTROLLINO</h3>");
client.println("<p>");
client.print("Aktuelle Temperatur: ");
client.println(temperatureA);
client.println("</p>");
client.println("<p>");
client.print("Sollwert Temperatur: ");
client.println(temperatureSP);
client.println("</p>");
client.println("<p>");
client.print("Delta Temperatur: ");
client.println(DeltaT);
client.println("</p>");
client.write("<form method=GET>T: <input type=text name=t>");
client.write("R: <input type=text name=r><input type=submit></form>");
client.println(r,t);
client.println("</body></html>");
break;
}
if (c == '\n') {
// Zeilenwechsel, also currentLineIsBlack erstmal auf True setzen
currentLineIsBlank = true;
}
else if (c != '\r') {
// Zeile enthält Zeichen, also currentLineIsBlack auf False setzen
currentLineIsBlank = false;
}
}
}
// Kleine Pause
delay(1);
// Verbindung schliessen
client.stop();
Serial.println("Verbindung mit Client beendet.");
Serial.println("");
}
}