Práce s více čidly

master
David Zálešák 1 year ago
parent 6cf0f9635c
commit 2ab48e73bb

@ -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,27 +166,45 @@ 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;
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++;
}
}
if(tempC != DEVICE_DISCONNECTED_C){ avgTemperature = avgTemperature / savedTemperatures;
// read the actual temperature after it's been converted
temperature = tempC + CORRECTION; for(int i=0; i<ds18Count; i++) {
// do what you need with the temperature here 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];
} }
metrics = "temp ";
metrics += temperature;
metrics += "\nraw_temp ";
metrics += tempC;
if(THERMOSTAT){ if(THERMOSTAT){
metrics += "\nheating "; metrics += "\nheating ";
metrics += heating; metrics += heating;