Compare commits

...

4 Commits

@ -22,11 +22,12 @@ DallasTemperature sensor(&oneWire);
//========== Init variables ========== //========== Init variables ==========
float temperature; float avgTemperature;
String metrics; String metrics;
bool heating; bool heating;
float reqTemp = 21.5; float reqTemp = 21.5;
float offset = 0.25; float offset = 0.25;
float correction[3] = {CORRECTION1, CORRECTION2, CORRECTION3};
//================================= //=================================
@ -128,7 +129,7 @@ void setup() {
String repply; String repply;
if (request->hasParam("temp")) { if (request->hasParam("temp")) {
repply = String(temperature); repply = String(avgTemperature);
} else if (request->hasParam("relay")) { } else if (request->hasParam("relay")) {
repply = String(heating); repply = String(heating);
} else if (THERMOSTAT && request->hasParam("reqtemp")) { } else if (THERMOSTAT && request->hasParam("reqtemp")) {
@ -165,33 +166,53 @@ void notFound(AsyncWebServerRequest *request) {
} }
void thermostatCheck() { void thermostatCheck() {
if(temperature < (reqTemp - offset)){ if (avgTemperature <= (reqTemp - offset)) {
heating = 1; heating = 1;
digitalWrite(RELAY_PIN, !heating); digitalWrite(RELAY_PIN, !heating);
}else if(temperature > (reqTemp + offset)){ } else if (avgTemperature >= (reqTemp + offset)) {
heating = 0; heating = 0;
digitalWrite(RELAY_PIN, !heating); digitalWrite(RELAY_PIN, !heating);
} }
} }
void readSensor() { void readSensor() {
float tempC = sensor.getTempCByIndex(0); uint8_t ds18Count = sensor.getDS18Count();
float tempC[ds18Count];
float temperature[ds18Count];
float avgTemperature;
int savedTemperatures = 0;
if(tempC != DEVICE_DISCONNECTED_C){ for (int i = 0; i < ds18Count; i++) {
// read the actual temperature after it's been converted tempC[i] = sensor.getTempCByIndex(i);
temperature = tempC + CORRECTION; if (tempC[i] != DEVICE_DISCONNECTED_C) {
// do what you need with the temperature here temperature[i] = tempC[i] + correction[i];
avgTemperature += temperature[i];
savedTemperatures++;
} }
metrics = "temp "; }
metrics += temperature;
metrics += "\nraw_temp "; avgTemperature = avgTemperature / savedTemperatures;
metrics += tempC;
for (int i = 0; i < ds18Count; i++) {
metrics += "\ntemp{device=\"";
metrics += i;
metrics += "\"} ";
metrics += temperature[i];
}
for (int i = 0; i < ds18Count; i++) {
metrics += "\nraw_temp{device=\"";
metrics += i;
metrics += "\"} ";
metrics += tempC[i];
}
if (THERMOSTAT) {
metrics += "\nheating "; metrics += "\nheating ";
metrics += heating; metrics += heating;
metrics += "\nreqTemp "; metrics += "\nreqTemp ";
metrics += reqTemp; metrics += reqTemp;
metrics += "\noffset "; metrics += "\noffset ";
metrics += offset; metrics += offset;
}
} }