diff --git a/src/helpers/sensors/EnvironmentSensorManager.cpp b/src/helpers/sensors/EnvironmentSensorManager.cpp index 73842d9eeb..e2e7dbf7ea 100644 --- a/src/helpers/sensors/EnvironmentSensorManager.cpp +++ b/src/helpers/sensors/EnvironmentSensorManager.cpp @@ -889,11 +889,11 @@ void EnvironmentSensorManager::stop_gps() { void EnvironmentSensorManager::loop() { #if ENV_INCLUDE_GPS - static long next_gps_update = 0; + static uint32_t last_gps_update = 0; if (gps_active) { _location->loop(); } - if (millis() > next_gps_update) { + if ((uint32_t)(millis() - last_gps_update) >= (uint32_t)(gps_update_interval_sec * 1000)) { if(gps_active){ #ifdef RAK_WISBLOCK_GPS @@ -914,7 +914,7 @@ void EnvironmentSensorManager::loop() { } #endif } - next_gps_update = millis() + (gps_update_interval_sec * 1000); + last_gps_update = millis(); } #endif #if ENV_INCLUDE_BME680_BSEC diff --git a/src/helpers/sensors/MicroNMEALocationProvider.h b/src/helpers/sensors/MicroNMEALocationProvider.h index eec466d3aa..c39b413d84 100644 --- a/src/helpers/sensors/MicroNMEALocationProvider.h +++ b/src/helpers/sensors/MicroNMEALocationProvider.h @@ -42,7 +42,7 @@ class MicroNMEALocationProvider : public LocationProvider { int8_t _claims = 0; int _pin_reset; int _pin_en; - long next_check = 0; + uint32_t last_check = 0; long time_valid = 0; unsigned long _last_time_sync = 0; static const unsigned long TIME_SYNC_INTERVAL = 1800000; // Re-sync every 30 minutes @@ -143,10 +143,11 @@ public : if (!isValid()) time_valid = 0; - if (millis() > next_check) { - next_check = millis() + 1000; + if ((uint32_t)(millis() - last_check) >= 1000) { + last_check = millis(); // Re-enable time sync periodically when GPS has valid fix - if (!_time_sync_needed && _clock != NULL && (millis() - _last_time_sync) > TIME_SYNC_INTERVAL) { + if (!_time_sync_needed && _clock != NULL && + (uint32_t)(millis() - _last_time_sync) > TIME_SYNC_INTERVAL) { _time_sync_needed = true; } if (_time_sync_needed && time_valid > 2) { diff --git a/variants/heltec_mesh_solar/target.cpp b/variants/heltec_mesh_solar/target.cpp index d140864cd1..c4000132fe 100644 --- a/variants/heltec_mesh_solar/target.cpp +++ b/variants/heltec_mesh_solar/target.cpp @@ -65,18 +65,18 @@ bool SolarSensorManager::querySensors(uint8_t requester_permissions, CayenneLPP& } void SolarSensorManager::loop() { - static long next_gps_update = 0; + static uint32_t last_gps_update = 0; _location->loop(); - if (millis() > next_gps_update) { + if ((uint32_t)(millis() - last_gps_update) >= 1000) { if (_location->isValid()) { node_lat = ((double)_location->getLatitude())/1000000.; node_lon = ((double)_location->getLongitude())/1000000.; node_altitude = ((double)_location->getAltitude()) / 1000.0; MESH_DEBUG_PRINTLN("lat %f lon %f", node_lat, node_lon); } - next_gps_update = millis() + 1000; + last_gps_update = millis(); } } diff --git a/variants/heltec_tracker/target.cpp b/variants/heltec_tracker/target.cpp index f32c41ff47..62a28ece7f 100644 --- a/variants/heltec_tracker/target.cpp +++ b/variants/heltec_tracker/target.cpp @@ -71,18 +71,18 @@ bool HWTSensorManager::querySensors(uint8_t requester_permissions, CayenneLPP& t } void HWTSensorManager::loop() { - static long next_gps_update = 0; + static uint32_t last_gps_update = 0; _location->loop(); - if (millis() > next_gps_update) { + if ((uint32_t)(millis() - last_gps_update) >= 1000) { if (gps_active && _location->isValid()) { node_lat = ((double)_location->getLatitude())/1000000.; node_lon = ((double)_location->getLongitude())/1000000.; node_altitude = ((double)_location->getAltitude()) / 1000.0; MESH_DEBUG_PRINTLN("lat %f lon %f", node_lat, node_lon); } - next_gps_update = millis() + 1000; + last_gps_update = millis(); } } diff --git a/variants/meshadventurer/target.cpp b/variants/meshadventurer/target.cpp index 8795a4cbe7..a2d1a0e5fb 100644 --- a/variants/meshadventurer/target.cpp +++ b/variants/meshadventurer/target.cpp @@ -65,16 +65,16 @@ bool MASensorManager::querySensors(uint8_t requester_permissions, CayenneLPP& te } void MASensorManager::loop() { - static long next_gps_update = 0; + static uint32_t last_gps_update = 0; _location->loop(); - if(millis() > next_gps_update && gps_active) { + if (gps_active && (uint32_t)(millis() - last_gps_update) >= 1000) { if(_location->isValid()) { node_lat = ((double)_location->getLatitude()) / 1000000.; node_lon = ((double)_location->getLongitude()) / 1000000.; node_altitude = ((double)_location->getAltitude()) / 1000.0; MESH_DEBUG_PRINTLN("lat %f lon %f", node_lat, node_lon); } - next_gps_update = millis() + 1000; + last_gps_update = millis(); } } diff --git a/variants/nano_g2_ultra/target.cpp b/variants/nano_g2_ultra/target.cpp index 69a2772ccb..f72a7b61d2 100644 --- a/variants/nano_g2_ultra/target.cpp +++ b/variants/nano_g2_ultra/target.cpp @@ -73,7 +73,7 @@ bool NanoG2UltraSensorManager::querySensors(uint8_t requester_permissions, Cayen } void NanoG2UltraSensorManager::loop() { - static long next_gps_update = 0; + static uint32_t last_gps_update = 0; if (!gps_active) { return; // GPS is not active, skip further processing @@ -81,7 +81,7 @@ void NanoG2UltraSensorManager::loop() { _location->loop(); - if (millis() > next_gps_update) { + if ((uint32_t)(millis() - last_gps_update) >= 1000) { if (_location->isValid()) { node_lat = ((double)_location->getLatitude()) / 1000000.; node_lon = ((double)_location->getLongitude()) / 1000000.; @@ -91,7 +91,7 @@ void NanoG2UltraSensorManager::loop() { MESH_DEBUG_PRINTLN("INVALID location, waiting for fix"); } MESH_DEBUG_PRINTLN("GPS satellites: %d", _location->satellitesCount()); - next_gps_update = millis() + 1000; + last_gps_update = millis(); } } diff --git a/variants/t1000-e/t1000e_sensors.cpp b/variants/t1000-e/t1000e_sensors.cpp index 85298d3a82..8717ad54ca 100644 --- a/variants/t1000-e/t1000e_sensors.cpp +++ b/variants/t1000-e/t1000e_sensors.cpp @@ -32,11 +32,12 @@ static int8_t ntc_temp2[136] = { }; static float get_heater_temperature(unsigned int vcc_volt, unsigned int ntc_volt) { + if (ntc_volt == 0) { + return 0.0f; // avoid division by zero on open circuit / sensor fault + } int i = 0; - float Vout = 0, Rt = 0, temp = 0; - Vout = ntc_volt; - - Rt = (HEATER_NTC_RP * vcc_volt) / Vout - HEATER_NTC_RP; + float Rt = 0, temp = 0; + Rt = (HEATER_NTC_RP * (float)vcc_volt) / ntc_volt - HEATER_NTC_RP; for (i = 0; i < 136; i++) { if (Rt >= ntc_res2[i]) { @@ -44,7 +45,17 @@ static float get_heater_temperature(unsigned int vcc_volt, unsigned int ntc_volt } } - temp = ntc_temp2[i - 1] + 1 * (ntc_res2[i - 1] - Rt) / (float)(ntc_res2[i - 1] - ntc_res2[i]); + if (i <= 0) { + return (float)ntc_temp2[0]; + } + if (i >= 136) { + return (float)ntc_temp2[135]; + } + int denom = ntc_res2[i - 1] - ntc_res2[i]; + if (denom == 0) { + return (float)ntc_temp2[i - 1]; + } + temp = ntc_temp2[i - 1] + 1 * (ntc_res2[i - 1] - Rt) / (float)denom; temp = (temp * 100 + 5) / 100; return temp; diff --git a/variants/t1000-e/target.cpp b/variants/t1000-e/target.cpp index 4253282708..429c9e9d8a 100644 --- a/variants/t1000-e/target.cpp +++ b/variants/t1000-e/target.cpp @@ -147,18 +147,18 @@ bool T1000SensorManager::querySensors(uint8_t requester_permissions, CayenneLPP& } void T1000SensorManager::loop() { - static long next_gps_update = 0; + static uint32_t last_gps_update = 0; + constexpr uint32_t GPS_UPDATE_INTERVAL_MS = 1000; _nmea->loop(); - if (millis() > next_gps_update) { + if ((uint32_t)(millis() - last_gps_update) >= GPS_UPDATE_INTERVAL_MS) { if (gps_active && _nmea->isValid()) { node_lat = ((double)_nmea->getLatitude())/1000000.; node_lon = ((double)_nmea->getLongitude())/1000000.; node_altitude = ((double)_nmea->getAltitude()) / 1000.0; - //Serial.printf("lat %f lon %f\r\n", _lat, _lon); } - next_gps_update = millis() + 1000; + last_gps_update = millis(); } } diff --git a/variants/thinknode_m1/target.cpp b/variants/thinknode_m1/target.cpp index 69306fc0e1..c19f9ca846 100644 --- a/variants/thinknode_m1/target.cpp +++ b/variants/thinknode_m1/target.cpp @@ -69,17 +69,17 @@ bool ThinkNodeM1SensorManager::querySensors(uint8_t requester_permissions, Cayen } void ThinkNodeM1SensorManager::loop() { - static long next_gps_update = 0; - static long last_switch_check = 0; + static uint32_t last_gps_update = 0; + static uint32_t last_switch_check = 0; // Check GPS switch state every second - if (millis() - last_switch_check > 1000) { + if ((uint32_t)(millis() - last_switch_check) >= 1000) { bool current_switch_state = digitalRead(PIN_GPS_SWITCH); - + // Detect switch state change if (current_switch_state != last_gps_switch_state) { last_gps_switch_state = current_switch_state; - + if (current_switch_state == HIGH) { // Switch is ON MESH_DEBUG_PRINTLN("GPS switch ON"); start_gps(); @@ -88,7 +88,7 @@ void ThinkNodeM1SensorManager::loop() { stop_gps(); } } - + last_switch_check = millis(); } @@ -98,14 +98,14 @@ void ThinkNodeM1SensorManager::loop() { _location->loop(); - if (millis() > next_gps_update) { + if ((uint32_t)(millis() - last_gps_update) >= 1000) { if (_location->isValid()) { node_lat = ((double)_location->getLatitude())/1000000.; node_lon = ((double)_location->getLongitude())/1000000.; node_altitude = ((double)_location->getAltitude()) / 1000.0; MESH_DEBUG_PRINTLN("lat %f lon %f", node_lat, node_lon); } - next_gps_update = millis() + 1000; + last_gps_update = millis(); } }