From 0c587af8de3f160a0136dc145fcd5eded72f02d1 Mon Sep 17 00:00:00 2001 From: Qrius Date: Thu, 12 Dec 2024 19:57:28 +0100 Subject: average out things --- hydroponics_broker.ino | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/hydroponics_broker.ino b/hydroponics_broker.ino index a22bd42..a2e0b43 100644 --- a/hydroponics_broker.ino +++ b/hydroponics_broker.ino @@ -10,11 +10,11 @@ #define SENSOR_SAMPLE_INTERVAL 5000 #define MQTT_PUSH_INTERVAL 60000 -#define SAMPLE_HISTORY_N 60 // 60*5s => 5m running average +#define SAMPLE_HISTORY_N 20 // 60*5s => 5m running average #define PIN_ONEWIRE 4 #define PIN_TDS 34 #define VREF 3.3 -float ec_calibration = 1.0f; +float ec_calibration = 0.98f; #define CTEMP temperatures[sensors_current_i] #define CTDS tds[sensors_current_i] @@ -67,11 +67,11 @@ const char *serverIndex PROGMEM = "div {display:flex;gap:1em;}" "" "" - "
SensorsInitialized: %dLast sample: %dms ago

" + "
SensorsInitialized: %dLast sample: %.1fs ago

" "
Temperature%.1fC ~ %.1fCMQTT: %d

" "
TDS%.0f ~ %.0fMQTT: %d

" "
EC%.1f ~ %.1fMQTT: %d

" - "
MQTTLast %dms ago

" + "
MQTTLast %.1fs ago

" "
" "" ""; @@ -135,11 +135,11 @@ void setup() { char buffer[strlen(serverIndex)+64]; sprintf( buffer, serverIndex, - sensors_initialized, now - lastSensorSample, + sensors_initialized, (now - lastSensorSample)/1000, CTEMP, temperatures_average, temperature_mqtt_last_publish_status, CTDS, tds_average, tds_mqtt_last_publish_status, CEC, ec_average, ec_mqtt_last_publish_status, - now - lastMqttPublish + (now - lastMqttPublish)/1000 ); server.send(200, "text/html", buffer); }); @@ -190,7 +190,8 @@ void updateSensorValues() { temperatures_total -= CTEMP; CTEMP = sensors.getTempCByIndex(0); temperatures_total += CTEMP; - temperatures_average = temperatures_total / SAMPLE_HISTORY_N; + //temperatures_average = temperatures_total / SAMPLE_HISTORY_N; + temperatures_average = getMedianNum(temperatures, SAMPLE_HISTORY_N); // TDS 1/EC 1 ec_total -= CEC; @@ -286,3 +287,26 @@ int getMedianNum(int bArray[], int iFilterLen){ } return bTemp; } + +float fgetMedianNum(float bArray[], int iFilterLen){ + float bTab[iFilterLen]; + for (byte i = 0; i bTab[i + 1]) { + bTemp = bTab[i]; + bTab[i] = bTab[i + 1]; + bTab[i + 1] = bTemp; + } + } + } + if ((iFilterLen & 1) > 0){ + bTemp = bTab[(iFilterLen - 1) / 2]; + } + else { + bTemp = (bTab[iFilterLen / 2] + bTab[iFilterLen / 2 - 1]) / 2; + } + return bTemp; +} -- cgit v1.2.3