Compare commits
2 Commits
81137a4c5e
...
050841dd78
| Author | SHA1 | Date | |
|---|---|---|---|
| 050841dd78 | |||
| 39a4952426 |
@@ -1,72 +1,5 @@
|
|||||||
import gc
|
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():
|
def check_memory_once():
|
||||||
"""One-time memory check (for startup diagnostics)."""
|
"""One-time memory check (for startup diagnostics)."""
|
||||||
gc.collect()
|
gc.collect()
|
||||||
|
|||||||
5
main.py
5
main.py
@@ -27,7 +27,7 @@ from scripts.air_conditioning import ACController
|
|||||||
from scripts.heating import HeaterController
|
from scripts.heating import HeaterController
|
||||||
from scripts.web_server import TempWebServer
|
from scripts.web_server import TempWebServer
|
||||||
from scripts.scheduler import ScheduleMonitor # NEW: Import scheduler for time-based temp changes
|
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 =====
|
# ===== START: Configuration Loading =====
|
||||||
# Load saved settings from config.json file on Pico
|
# Load saved settings from config.json file on Pico
|
||||||
@@ -250,9 +250,6 @@ monitors = [
|
|||||||
# Schedule monitor: Changes temp targets based on time of day
|
# Schedule monitor: Changes temp targets based on time of day
|
||||||
schedule_monitor,
|
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: Automatically turns AC on/off based on temperature
|
||||||
ac_monitor,
|
ac_monitor,
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user