feat: Add temperature validation in TemperatureMonitor and implement aggressive garbage collection in main loop

This commit is contained in:
Aaron 2025-11-09 11:54:12 -05:00
parent dae6971112
commit 229bde85e9
3 changed files with 18 additions and 16 deletions

View File

@ -77,6 +77,12 @@ class TemperatureMonitor(Monitor):
temp = list(temps.values())[0] # Get first temp reading temp = list(temps.values())[0] # Get first temp reading
# ===== ADD THIS: Validate temperature is reasonable =====
if temp < -50 or temp > 150: # Sanity check (outside normal range)
print("⚠️ Warning: {} sensor returned invalid temp: {:.1f}°F".format(self.label, temp))
return # Don't cache invalid reading
# ===== END: Validation =====
# Cache the reading for web server (avoid blocking reads) # Cache the reading for web server (avoid blocking reads)
self.last_temp = temp self.last_temp = temp
self.last_read_time = current_time self.last_read_time = current_time

View File

@ -32,5 +32,6 @@
} }
], ],
"schedule_enabled": true, "schedule_enabled": true,
"permanent_hold": false "permanent_hold": false,
"temp_hold_start_time": null
} }

25
main.py
View File

@ -376,20 +376,8 @@ while True:
# Web requests # Web requests
web_server.check_requests(sensors, ac_monitor, heater_monitor, schedule_monitor, config) web_server.check_requests(sensors, ac_monitor, heater_monitor, schedule_monitor, config)
# ===== RETRY NTP SYNC (if initial failed) =====
if not ntp_synced and retry_ntp_attempts < max_ntp_attempts:
if time.time() % 10 < 1: # Every 10 seconds
if sync_ntp_time(TIMEZONE_OFFSET):
ntp_synced = True
last_ntp_sync = time.time()
print("NTP sync succeeded on retry #{} (UTC{:+d})".format(retry_ntp_attempts + 1, TIMEZONE_OFFSET))
else:
retry_ntp_attempts += 1
print("NTP retry {} failed".format(retry_ntp_attempts))
# ===== PERIODIC RE-SYNC (every 24 hours) ===== # ===== PERIODIC RE-SYNC (every 24 hours) =====
# Re-sync once a day to correct clock drift if ntp_synced and (time.time() - last_ntp_sync) > 86400:
if ntp_synced and (time.time() - last_ntp_sync) > 86400: # 24 hours in seconds
print("24-hour re-sync due...") print("24-hour re-sync due...")
if sync_ntp_time(TIMEZONE_OFFSET): if sync_ntp_time(TIMEZONE_OFFSET):
last_ntp_sync = time.time() last_ntp_sync = time.time()
@ -397,8 +385,15 @@ while True:
else: else:
print("Daily NTP re-sync failed (will retry tomorrow)") print("Daily NTP re-sync failed (will retry tomorrow)")
# ===== END: PERIODIC RE-SYNC ===== # ===== END: PERIODIC RE-SYNC =====
# Enable garbage collection to free memory
gc.collect() # ===== ADD THIS: AGGRESSIVE GARBAGE COLLECTION =====
current_time = time.time()
if int(current_time) % 5 == 0: # Every 5 seconds
gc.collect()
# Optional: Print memory stats occasionally
if int(current_time) % 60 == 0: # Every minute
print("💾 Memory free: {} KB".format(gc.mem_free() // 1024))
# ===== END: AGGRESSIVE GC =====
time.sleep(0.1) time.sleep(0.1)
except KeyboardInterrupt: except KeyboardInterrupt: