Compare commits

...

4 Commits

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