From 1016e96b58da268218f5b2c9eeb10e23a3e10e81 Mon Sep 17 00:00:00 2001 From: sickprodigy Date: Tue, 11 Nov 2025 16:55:27 -0500 Subject: [PATCH] feat: Add static IP configuration options to config and main files. Also remove creation of config from web_server.py because I was already doing it in main.py like it should be done, somewhere first. Fixes #25 --- Scripts/web_server.py | 31 ++++++++++++------------------- config.json | 4 ++++ main.py | 16 +++++++++++----- 3 files changed, 27 insertions(+), 24 deletions(-) diff --git a/Scripts/web_server.py b/Scripts/web_server.py index a203763..ba01fc3 100644 --- a/Scripts/web_server.py +++ b/Scripts/web_server.py @@ -9,7 +9,7 @@ class TempWebServer: self.socket = None self.sensors = {} self.last_page_render = 0 # Track last successful HTML generation - + def start(self): """Start the web server (non-blocking).""" try: @@ -231,23 +231,16 @@ class TempWebServer: import sys sys.print_exception(e) return False - + def _load_config(self): """Load configuration from file.""" try: with open('config.json', 'r') as f: return json.load(f) - except: - return { - 'ac_target': 77.0, - 'ac_swing': 1.0, - 'heater_target': 80.0, - 'heater_swing': 2.0, - 'schedules': [], - 'schedule_enabled': False, - 'permanent_hold': False - } - + except Exception as e: + print("Error loading config:", e) + raise # Or handle as appropriate + def _handle_schedule_update(self, request, sensors, ac_monitor, heater_monitor, schedule_monitor, config): """Handle schedule form submission.""" @@ -646,7 +639,7 @@ class TempWebServer: print("DEBUG: Generating status page...") # ===== FORCE GARBAGE COLLECTION BEFORE BIG ALLOCATION ===== - import gc + import gc # type: ignore gc.collect() print("DEBUG: Memory freed, {} bytes available".format(gc.mem_free())) # ===== END GARBAGE COLLECTION ===== @@ -1307,8 +1300,8 @@ class TempWebServer: time_value = schedule.get('time', '') name_value = schedule.get('name', '') - heater_value = schedule.get('heater_target', config.get('heater_target', 72.0)) - ac_value = schedule.get('ac_target', config.get('ac_target', 75.0)) + heater_value = schedule.get('heater_target', config.get('heater_target')) + ac_value = schedule.get('ac_target', config.get('ac_target')) print("DEBUG: Values: time='{}', name='{}', heater={}, ac={}".format( time_value, name_value, heater_value, ac_value)) @@ -1453,7 +1446,7 @@ class TempWebServer: ) return html - + def _build_mode_buttons(self, config): """Build mode control buttons for dashboard only.""" schedules = config.get('schedules', []) @@ -1531,7 +1524,7 @@ class TempWebServer: """ - + def _get_settings_page(self, sensors, ac_monitor, heater_monitor): """Generate advanced settings page.""" config = self._load_config() @@ -1707,7 +1700,7 @@ class TempWebServer: ) return html - + def _handle_settings_update(self, request, sensors, ac_monitor, heater_monitor, schedule_monitor, config): """Handle advanced settings update.""" try: diff --git a/config.json b/config.json index bb64f8c..7d09e86 100644 --- a/config.json +++ b/config.json @@ -1,4 +1,8 @@ { + "static_ip": "192.168.86.43", + "subnet": "255.255.255.0", + "gateway": "192.168.86.1", + "dns": "192.168.86.1", "timezone_offset": -5, "ac_target": 77.0, "ac_swing": 1.0, diff --git a/main.py b/main.py index 64965fa..c2b9927 100644 --- a/main.py +++ b/main.py @@ -91,11 +91,17 @@ def load_config(): print("No saved config found, creating default config.json...") default_config = { + 'static_ip': '192.168.86.43', + 'subnet': '255.255.255.0', + 'gateway': '192.168.86.1', + 'dns': '192.168.86.1', 'timezone_offset': -6, # Timezone offset from UTC (CST=-6, EST=-5, MST=-7, PST=-8, add 1 for DST) 'ac_target': 75.0, # Default AC target temp 'ac_swing': 1.0, # Default AC tolerance (+/- degrees) 'heater_target': 72.0, # Default heater target temp 'heater_swing': 2.0, # Default heater tolerance (+/- degrees) + 'temp_hold_duration': 3600, # Default hold duration in seconds (1 hour) + 'temp_hold_start_time': None, # No hold active at startup 'schedules': [ # Default 4 schedules { 'time': '06:00', @@ -169,11 +175,11 @@ wifi = connect_wifi(led) # Set static IP and print WiFi details if wifi and wifi.isconnected(): - # Configure static IP (easier to bookmark web interface) - static_ip = '192.168.86.43' # Change this to match your network - subnet = '255.255.255.0' - gateway = '192.168.86.1' # Usually your router IP - dns = '192.168.86.1' # Usually your router IP + # Get static IP settings from config + static_ip = config.get('static_ip') + subnet = config.get('subnet') + gateway = config.get('gateway') + dns = config.get('dns') # Apply static IP configuration wifi.ifconfig((static_ip, subnet, gateway, dns))