Compare commits

...

2 Commits

2 changed files with 1 additions and 71 deletions

View File

@@ -1,72 +1,5 @@
import gc
class MemoryMonitor:
"""Monitor and display Pico W memory usage."""
def __init__(self, interval=300):
"""
Initialize memory monitor.
Args:
interval: Seconds between memory checks (default: 300 = 5 minutes)
"""
self.interval = interval
self.last_check = 0
self.initial_free = None # Track initial free memory
def should_run(self, current_time):
"""Check if it's time to run memory check."""
if current_time - self.last_check >= self.interval:
self.last_check = current_time
return True
return False
def run(self):
"""Check and display memory usage."""
# Force garbage collection before checking
gc.collect()
# Get memory stats
free = gc.mem_free()
allocated = gc.mem_alloc()
total = free + allocated
# Store initial free memory on first run
if self.initial_free is None:
self.initial_free = free
# Calculate memory leak (if any)
leaked = self.initial_free - free if self.initial_free else 0
# Print memory report
print("\n" + "="*50)
print("Pico W Memory (RAM):")
print("="*50)
print("Total: {:.1f} KB".format(total / 1024))
print("Used: {:.1f} KB ({:.1f}%)".format(
allocated / 1024,
(allocated/total)*100
))
print("Free: {:.1f} KB ({:.1f}%)".format(
free / 1024,
(free/total)*100
))
# Show memory leak warning if detected
if leaked > 5120: # More than 5 KB leaked
print("⚠️ Leaked: {:.1f} KB since startup".format(leaked / 1024))
print("="*50 + "\n")
# Return memory info for other uses (like web server)
return {
'total_kb': total / 1024,
'used_kb': allocated / 1024,
'free_kb': free / 1024,
'usage_percent': (allocated/total)*100,
'leaked_kb': leaked / 1024 if leaked > 0 else 0
}
def check_memory_once():
"""One-time memory check (for startup diagnostics)."""
gc.collect()

View File

@@ -27,7 +27,7 @@ from scripts.air_conditioning import ACController
from scripts.heating import HeaterController
from scripts.web_server import TempWebServer
from scripts.scheduler import ScheduleMonitor # NEW: Import scheduler for time-based temp changes
from scripts.memory_check import MemoryMonitor, check_memory_once # NEW: Import memory checker
from scripts.memory_check import check_memory_once # Just the function
# ===== START: Configuration Loading =====
# Load saved settings from config.json file on Pico
@@ -249,9 +249,6 @@ monitors = [
# Schedule monitor: Changes temp targets based on time of day
schedule_monitor,
# Memory monitor: Checks RAM usage every 5 minutes
MemoryMonitor(interval=300), # 300 seconds = 5 minutes
# AC monitor: Automatically turns AC on/off based on temperature
ac_monitor,