aboutsummaryrefslogtreecommitdiff
path: root/hydroponics_broker.ino
diff options
context:
space:
mode:
authorQrius <[email protected]>2024-12-12 19:57:28 +0100
committerQrius <[email protected]>2024-12-12 19:57:28 +0100
commit0c587af8de3f160a0136dc145fcd5eded72f02d1 (patch)
tree178299912c8b02ad1a00f010fa725579acccde19 /hydroponics_broker.ino
parentecbf17105fcea50742ae156704d21c5e4ff5a843 (diff)
downloadhydroponics_broker-master.tar.gz
hydroponics_broker-master.zip
average out thingsHEADmaster
Diffstat (limited to 'hydroponics_broker.ino')
-rw-r--r--hydroponics_broker.ino38
1 files 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;}"
"</style></head>"
"<body>"
- "<div><strong>Sensors</strong><span>Initialized: <em>%d</em></span><span>Last sample: <em>%dms</em> ago</span></div><hr>"
+ "<div><strong>Sensors</strong><span>Initialized: <em>%d</em></span><span>Last sample: <em>%.1fs</em> ago</span></div><hr>"
"<div><strong>Temperature</strong><span><em>%.1fC</em> ~ <em>%.1fC</em></span><span>MQTT: <em>%d</em></span></div><hr>"
"<div><strong>TDS</strong><span><em>%.0f</em> ~ <em>%.0f</em></span><span>MQTT: <em>%d</em></span></div><hr>"
"<div><strong>EC</strong><span><em>%.1f</em> ~ <em>%.1f</em></span><span>MQTT: <em>%d</em></span></div><hr>"
- "<div><strong>MQTT</strong><span>Last <em>%dms</em> ago</span></div><hr>"
+ "<div><strong>MQTT</strong><span>Last <em>%.1fs</em> ago</span></div><hr>"
"<form method='POST' action='/update' enctype='multipart/form-data'><input type='file' name='update'><input type='submit' value='Update'></form>"
"</body>"
"</html>";
@@ -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<iFilterLen; i++)
+ bTab[i] = bArray[i];
+ int i, j, bTemp;
+ for (j = 0; j < iFilterLen - 1; j++) {
+ for (i = 0; i < iFilterLen - j - 1; i++) {
+ if (bTab[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;
+}