Skip to content

Commit d925c8e

Browse files
committed
"Add sensor scanning and caching: implement periodic sensor updates with scanSensors, cache values for CPU temperature, water level, volume, and current, and update handlers to use cached data."
1 parent eff4a8d commit d925c8e

File tree

5 files changed

+193
-10
lines changed

5 files changed

+193
-10
lines changed

src/DeviceHandler.cpp

Lines changed: 174 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
// Store last Blink Timestamp.
1313
unsigned long previousMillis = 0;
1414

15+
// Store last Scan Timestamp.
16+
unsigned long scanMillis = 0;
17+
1518
// Store last Relais Duration Timestamp.
1619
unsigned long ch1 = -1;
1720
unsigned long ch2 = -1;
@@ -37,6 +40,13 @@ float minV = 0.0;
3740
// Store Tank Volume.
3841
float tankVolume = 1000.0;
3942

43+
// Store scanned Values (Caching to save CPU)
44+
float scanCurrent = 0.00;
45+
float scanTemperature = 0.00;
46+
float scanWaterLevel = 0.00;
47+
float scanWaterVolume = 0.00;
48+
49+
4050
/**
4151
* Monitors and manages the state of relays based on their configured timers.
4252
*
@@ -109,10 +119,60 @@ void DeviceHandler::handleBlink()
109119
}
110120
}
111121

122+
123+
void DeviceHandler::scanSensors()
124+
{
125+
// Read ADC First = latestVoltage as ref.
126+
getADCValue();
127+
128+
// Read Sensor Values.
129+
scanCurrent = getCurrent(false);
130+
scanTemperature = getCPUTemperature();
131+
scanWaterLevel = getLevel();
132+
scanWaterVolume = getVolume();
133+
}
134+
135+
void DeviceHandler::handleScan()
136+
{
137+
unsigned long currentMillis = millis();
138+
139+
// Check if the interval has passed
140+
if (currentMillis - scanMillis >= SCAN_INTERVAL)
141+
{
142+
scanMillis = currentMillis;
143+
144+
scanSensors();
145+
}
146+
}
147+
148+
/**
149+
* Executes periodic tasks required for the proper functioning of the device.
150+
*
151+
* This method acts as the main loop for the `DeviceHandler` class, invoking
152+
* specific subroutines in a predefined sequence. Each subroutine manages
153+
* a distinct functionality of the device, such as blinking an LED, handling
154+
* relay states, and performing scanning operations.
155+
*
156+
* Behavior:
157+
* - Calls `handleBlink()` to manage the LED blinking operation based on the specified
158+
* blink interval.
159+
* - Calls `handleRelais()` to monitor and control the state of relays based on their
160+
* configured timers.
161+
* - Calls `handleScan()` to handle scanning-related tasks.
162+
*
163+
* Preconditions:
164+
* - The device and its components (e.g., relays, LED) must be properly initialized and
165+
* functional.
166+
* - Any required configurations (e.g., timers, intervals) must be defined and valid.
167+
*
168+
* Postconditions:
169+
* - Executes the subroutines sequentially, ensuring periodic tasks are handled as needed.
170+
*/
112171
void DeviceHandler::loop()
113172
{
114173
handleBlink();
115174
handleRelais();
175+
handleScan();
116176
}
117177

118178
/**
@@ -192,7 +252,6 @@ void DeviceHandler::setup()
192252
// Set ADC Attenuation.
193253
analogSetPinAttenuation(SENSE, ADC_11db);
194254

195-
196255
// Input Pins.
197256
pinMode(SENSE, INPUT);
198257

@@ -488,3 +547,117 @@ float DeviceHandler::getVolume()
488547
{
489548
return tankVolume * (getLevel() / 100.0f);
490549
}
550+
551+
/**
552+
* Retrieves the cached value of the water volume measurement.
553+
*
554+
* This method returns the most recently scanned water volume, stored in the `scanWaterVolume`
555+
* variable. The value represents the last known measurement without triggering a new
556+
* volume scanning operation. It is useful for applications where frequent or real-time
557+
* volume updates are not required, thus saving computational resources.
558+
*
559+
* @return The cached water volume as a floating-point value. The value is the last recorded
560+
* measurement of the water volume or the default initialized value if no update
561+
* has occurred.
562+
*/
563+
float DeviceHandler::getVolumeCached()
564+
{
565+
return scanWaterVolume;
566+
}
567+
568+
/**
569+
* Retrieves the cached value of the CPU temperature.
570+
*
571+
* This method provides the most recently scanned value of the CPU temperature,
572+
* stored in the `scanTemperature` variable. The value is updated periodically
573+
* by other components or processes, ensuring low-latency access to temperature
574+
* data without performing a new scan.
575+
*
576+
* Preconditions:
577+
* - The `scanTemperature` variable must contain an updated temperature reading.
578+
*
579+
* Postconditions:
580+
* - Returns the cached CPU temperature value as a floating-point number.
581+
*
582+
* @return The cached value of the CPU temperature in degrees Celsius.
583+
*/
584+
float DeviceHandler::getCPUTemperatureCached()
585+
{
586+
return scanTemperature;
587+
}
588+
589+
/**
590+
* Retrieves the cached value of the water level measurement.
591+
*
592+
* This method returns the most recently stored value of the water level percentage,
593+
* which was previously measured and cached. The cached value provides quick access
594+
* without requiring a new measurement or sensor scan.
595+
*
596+
* Preconditions:
597+
* - The `scanWaterLevel` variable must hold a valid cached water level percentage, as
598+
* updated by the appropriate scanning or sensing process.
599+
*
600+
* Postconditions:
601+
* - The cached water level percentage remains unchanged after this method call.
602+
*
603+
* Behavior:
604+
* - The method simply fetches and returns the current cached value of water level
605+
* from the `scanWaterLevel` variable without initiating any additional sensor reads.
606+
*
607+
* @return The cached water level percentage as a floating-point value.
608+
*/
609+
float DeviceHandler::getLevelCached()
610+
{
611+
return scanWaterLevel;
612+
}
613+
614+
/**
615+
* Retrieves the most recently cached current value measured by the device.
616+
*
617+
* This method returns the current measurement previously scanned and stored
618+
* in the internal cache. It provides quick access to the last recorded value
619+
* without initiating a new sensor reading.
620+
*
621+
* Preconditions:
622+
* - The `scanCurrent` variable must have been updated with a valid measurement
623+
* from a prior sensor scan operation.
624+
*
625+
* Postconditions:
626+
* - No sensor is queried or updated as this method only fetches the cached value.
627+
*
628+
* Behavior:
629+
* - The method simply returns the value of the `scanCurrent` variable, representing
630+
* the last recorded current measurement in floating-point format.
631+
*
632+
* @return The cached current measurement as a float value.
633+
*/
634+
float DeviceHandler::getCurrentCached()
635+
{
636+
return scanCurrent;
637+
}
638+
639+
/**
640+
* Retrieves the most recently cached ADC voltage value.
641+
*
642+
* This method provides the last stored analog-to-digital converter (ADC)
643+
* voltage reading without initiating a new measurement. It serves as a
644+
* faster alternative to obtaining the latest ADC value when repeated
645+
* readings are unnecessary or when the cached value suffices.
646+
*
647+
* Preconditions:
648+
* - The `latestVoltage` variable must have been updated previously
649+
* through a measurement or initialization process.
650+
*
651+
* Postconditions:
652+
* - The returned value reflects the last stored ADC voltage reading,
653+
* which may not represent the current state of the ADC.
654+
*
655+
* Behavior:
656+
* - Always returns the cached ADC voltage value stored in `latestVoltage`.
657+
*
658+
* @return The cached ADC voltage value as a floating-point number.
659+
*/
660+
float DeviceHandler::getADCValueCached()
661+
{
662+
return latestVoltage;
663+
}

src/DeviceHandler.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
//
44
#include <Arduino.h>
55

6+
#include "ArduinoJson/Document/JsonDocument.hpp"
7+
68
#ifndef DEVICEHANDLER_H
79
#define DEVICEHANDLER_H
810

@@ -12,6 +14,8 @@ class DeviceHandler
1214
private:
1315
static void handleRelais();
1416
static void handleBlink();
17+
static void scanSensors();
18+
static void handleScan();
1519

1620
public:
1721
static void loop();
@@ -26,6 +30,11 @@ class DeviceHandler
2630
static int getDuration(int i);
2731
static float getLevel();
2832
static float getVolume();
33+
static float getVolumeCached();
34+
static float getCPUTemperatureCached();
35+
static float getLevelCached();
36+
static float getCurrentCached();
37+
static float getADCValueCached();
2938
};
3039

3140

src/InternalConfig.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#define VERSION "1.3.0"
1212
#define DEBUG true
1313
#define BLINK_INTERVAL 250
14+
#define SCAN_INTERVAL 1000
1415
#define MQTT_INTERVAL 1000
1516

1617
/**

src/MQTTHandler.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ void MQTTHandler::loop()
160160
previousMillisMQTT = currentMillis;
161161

162162
// Voltage (Float)
163-
publish("waterlevel/voltage", String(DeviceHandler::getADCValue(), 2).c_str());
163+
publish("waterlevel/voltage", String(DeviceHandler::getADCValueCached(), 2).c_str());
164164

165165
// Channel 1 (Bool)
166166
publish("waterlevel/channel/1/state", DeviceHandler::getState(1) ? "1" : "0");
@@ -175,13 +175,13 @@ void MQTTHandler::loop()
175175
publish("waterlevel/channel/2/duration", String(DeviceHandler::getDuration(2)).c_str());
176176

177177
// CPU Temperature
178-
publish("waterlevel/cpu", String(DeviceHandler::getCPUTemperature(), 1).c_str());
178+
publish("waterlevel/cpu", String(DeviceHandler::getCPUTemperatureCached(), 1).c_str());
179179

180180
// Level (Float)
181-
publish("waterlevel/level", String(DeviceHandler::getLevel(), 1).c_str());
181+
publish("waterlevel/level", String(DeviceHandler::getLevelCached(), 1).c_str());
182182

183183
// Volume (Float)
184-
publish("waterlevel/volume", String(DeviceHandler::getVolume(), 1).c_str());
184+
publish("waterlevel/volume", String(DeviceHandler::getVolumeCached(), 1).c_str());
185185
}
186186
}
187187
else

src/WebHandler.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,19 +179,19 @@ void WebHandler::handleAPICall(AsyncWebServerRequest* request, JsonVariant json)
179179

180180

181181
// Set ADC Voltage.
182-
doc["adc"] = DeviceHandler::getADCValue();
182+
doc["adc"] = DeviceHandler::getADCValueCached();
183183

184184
// Set Current (eq. 4-20mA).
185-
doc["current"] = DeviceHandler::getCurrent(false);
185+
doc["current"] = DeviceHandler::getCurrentCached();
186186

187187
// Set Tank Level in %.
188-
doc["level"] = DeviceHandler::getLevel();
188+
doc["level"] = DeviceHandler::getLevelCached();
189189

190190
// Set Volume in L.
191-
doc["volume"] = DeviceHandler::getVolume();
191+
doc["volume"] = DeviceHandler::getVolumeCached();
192192

193193
// Set CPU Temperature.
194-
doc["cpu"] = DeviceHandler::getCPUTemperature();
194+
doc["cpu"] = DeviceHandler::getCPUTemperatureCached();
195195

196196
// Set CPU Frequency.
197197
doc["frequency"] = ESP.getCpuFreqMHz();

0 commit comments

Comments
 (0)