Skip to content

Commit

Permalink
Only query current time once in requestMeasurement
Browse files Browse the repository at this point in the history
Also enable dht debug logging in debug mode
  • Loading branch information
ToMe25 committed Feb 29, 2024
1 parent 98d8547 commit d0658ec
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
4 changes: 3 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ test_framework = unity

[debug]
build_type = debug
build_flags = -D CORE_DEBUG_LEVEL=5
build_flags =
-D CORE_DEBUG_LEVEL=5
-D DHT_DEBUG

[env:native]
platform = native
Expand Down
7 changes: 4 additions & 3 deletions src/sensors/DHTHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ bool DHTHandler::begin() {
}

bool DHTHandler::requestMeasurement() {
if (_last_request == -1 || millis() - _last_request >= MIN_INTERVAL) {
_last_request = millis();
const uint64_t now = millis();
if (_last_request == -1 || now - _last_request >= MIN_INTERVAL) {
_last_request = now;
if (!_dht.read(false)) {
_temperature = _humidity = NAN;
log_w("Failed to read data from dht.");
Expand All @@ -53,7 +54,7 @@ bool DHTHandler::requestMeasurement() {
} else {
log_i("Attempted to read sensor data before minimum delay.");
log_d("Min delay: %ums, Time since measurement: %ums",
(uint32_t) MIN_INTERVAL, (uint32_t) (millis() - _last_request));
(uint32_t) MIN_INTERVAL, (uint32_t) (now - _last_request));
return false;
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/sensors/DallasHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ bool DallasHandler::begin() {
}

bool DallasHandler::requestMeasurement() {
if (_last_request == -1 || millis() - _last_request >= MIN_INTERVAL) {
uint64_t now = millis();
if (_last_request == -1 || now - _last_request >= MIN_INTERVAL) {
// Read previous measurements, if they weren't read yet.
if (_last_request > _last_finished_request) {
getTemperature();
now = millis();
}

_last_request = millis();
_last_request = now;
DeviceAddress address;
if (!_sensors.getAddress(address, SENSOR_INDEX)) {
log_w("Failed to get address for sensor %u.", SENSOR_INDEX);
Expand All @@ -65,7 +67,7 @@ bool DallasHandler::requestMeasurement() {
} else {
log_i("Attempted to read sensor data before minimum delay.");
log_d("Min delay: %ums, Time since measurement: %ums",
(uint32_t) MIN_INTERVAL, (uint32_t) (millis() - _last_request));
(uint32_t) MIN_INTERVAL, (uint32_t) (now - _last_request));
return false;
}
}
Expand Down

0 comments on commit d0658ec

Please sign in to comment.