Add schedule management to web server and configuration loading

This commit is contained in:
Aaron 2025-11-05 21:33:19 -05:00
parent 94fb7d3081
commit f4be1a7f7d
3 changed files with 157 additions and 29 deletions

View File

@ -132,7 +132,46 @@ class TempWebServer:
# Get current time # Get current time
current_time = time.localtime() current_time = time.localtime()
time_str = f"{current_time[0]}-{current_time[1]:02d}-{current_time[2]:02d} {current_time[3]:02d}:{current_time[4]:02d}:{current_time[5]:02d}" time_str = "{}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}".format(
current_time[0], current_time[1], current_time[2],
current_time[3], current_time[4], current_time[5]
)
# Load config to show schedules
try:
import json
with open('config.json', 'r') as f:
config = json.load(f)
except:
config = {'schedules': [], 'schedule_enabled': False}
# Build schedule display
schedule_status = "ENABLED ✅" if config.get('schedule_enabled') else "DISABLED ⚠️"
if config.get('schedules'):
schedule_cards = ""
for schedule in config.get('schedules', []):
schedule_cards += """
<div style="background: #f8f9fa; padding: 15px; border-radius: 8px;">
<div style="font-weight: bold; color: #34495e; margin-bottom: 5px;">
🕐 {time} - {name}
</div>
<div style="color: #7f8c8d; font-size: 14px;">
AC: {ac_temp}°F | Heater: {heater_temp}°F
</div>
</div>
""".format(
time=schedule.get('time', 'N/A'),
name=schedule.get('name', 'Unnamed'),
ac_temp=schedule.get('ac_target', 'N/A'),
heater_temp=schedule.get('heater_target', 'N/A')
)
else:
schedule_cards = """
<div style="text-align: center; color: #95a5a6; grid-column: 1 / -1;">
No schedules configured
</div>
"""
# Success message # Success message
success_html = """ success_html = """
@ -425,6 +464,19 @@ class TempWebServer:
</form> </form>
</div> </div>
<div class="card full-width">
<h2 style="text-align: center; color: #34495e; margin-bottom: 20px;">📅 Daily Schedule</h2>
<div style="text-align: center; margin-bottom: 15px;">
<strong>Status:</strong>
<span style="color: {schedule_color}; font-weight: bold;">
{schedule_status}
</span>
</div>
<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 15px;">
{schedule_cards}
</div>
</div>
<div class="footer"> <div class="footer">
Last updated: {time}<br> Last updated: {time}<br>
🔄 Auto-refresh every 30 seconds 🔄 Auto-refresh every 30 seconds
@ -433,8 +485,8 @@ class TempWebServer:
</html> </html>
""".format( """.format(
success_message=success_html, success_message=success_html,
inside_temp=f"{inside_temp:.1f}" if isinstance(inside_temp, float) else inside_temp, inside_temp="{:.1f}".format(inside_temp) if isinstance(inside_temp, float) else inside_temp,
outside_temp=f"{outside_temp:.1f}" if isinstance(outside_temp, float) else outside_temp, outside_temp="{:.1f}".format(outside_temp) if isinstance(outside_temp, float) else outside_temp,
ac_status=ac_status, ac_status=ac_status,
ac_class="on" if ac_status == "ON" else "off", ac_class="on" if ac_status == "ON" else "off",
heater_status=heater_status, heater_status=heater_status,
@ -443,6 +495,9 @@ class TempWebServer:
ac_swing=ac_monitor.temp_swing if ac_monitor else "N/A", ac_swing=ac_monitor.temp_swing if ac_monitor else "N/A",
heater_target=heater_monitor.target_temp if heater_monitor else "N/A", heater_target=heater_monitor.target_temp if heater_monitor else "N/A",
heater_swing=heater_monitor.temp_swing if heater_monitor else "N/A", heater_swing=heater_monitor.temp_swing if heater_monitor else "N/A",
time=time_str time=time_str,
schedule_status=schedule_status,
schedule_color="#2ecc71" if config.get('schedule_enabled') else "#95a5a6",
schedule_cards=schedule_cards
) )
return html return html

View File

@ -2,5 +2,32 @@
"ac_target": 77.0, "ac_target": 77.0,
"ac_swing": 1.0, "ac_swing": 1.0,
"heater_target": 80.0, "heater_target": 80.0,
"heater_swing": 2.0 "heater_swing": 2.0,
"schedules": [
{
"time": "06:00",
"ac_target": 75.0,
"heater_target": 78.0,
"name": "Morning"
},
{
"time": "12:00",
"ac_target": 77.0,
"heater_target": 80.0,
"name": "Midday"
},
{
"time": "18:00",
"ac_target": 76.0,
"heater_target": 79.0,
"name": "Evening"
},
{
"time": "22:00",
"ac_target": 74.0,
"heater_target": 77.0,
"name": "Night"
}
],
"schedule_enabled": true
} }

94
main.py
View File

@ -1,6 +1,7 @@
from machine import Pin from machine import Pin
import time import time
import network import network
import json
# Initialize pins (LED light onboard) # Initialize pins (LED light onboard)
led = Pin("LED", Pin.OUT) led = Pin("LED", Pin.OUT)
@ -24,6 +25,29 @@ from scripts.temperature_sensor import TemperatureSensor
from scripts.air_conditioning import ACController 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
# Load saved settings from file
def load_config():
"""Load configuration from config.json file."""
try:
with open('config.json', 'r') as f:
config = json.load(f)
print("Loaded saved settings from config.json")
return config
except:
print("No saved config found, using defaults")
return {
'ac_target': 77.0,
'ac_swing': 1.0,
'heater_target': 80.0,
'heater_swing': 2.0,
'schedules': [],
'schedule_enabled': False
}
# Load configuration
config = load_config()
# Connect to WiFi # Connect to WiFi
wifi = connect_wifi(led) wifi = connect_wifi(led)
@ -31,10 +55,10 @@ wifi = connect_wifi(led)
# Set static IP and print WiFi details # Set static IP and print WiFi details
if wifi and wifi.isconnected(): if wifi and wifi.isconnected():
# Configure static IP # Configure static IP
static_ip = '192.168.86.43' # Your desired static IP static_ip = '192.168.86.43'
subnet = '255.255.255.0' # Subnet mask (usually this) subnet = '255.255.255.0'
gateway = '192.168.86.1' # Your router's IP gateway = '192.168.86.1'
dns = '192.168.86.1' # DNS server (usually same as gateway) dns = '192.168.86.1'
wifi.ifconfig((static_ip, subnet, gateway, dns)) wifi.ifconfig((static_ip, subnet, gateway, dns))
time.sleep(1) time.sleep(1)
@ -62,7 +86,7 @@ else:
web_server = TempWebServer(port=80) web_server = TempWebServer(port=80)
web_server.start() web_server.start()
# Sensor configuration registry (moved from temperature_sensor.py) # Sensor configuration registry
SENSOR_CONFIG = { SENSOR_CONFIG = {
'inside': { 'inside': {
'pin': 10, 'pin': 10,
@ -92,53 +116,75 @@ sensors = get_configured_sensors()
# AC Controller options # AC Controller options
ac_controller = ACController( ac_controller = ACController(
relay_pin=15, relay_pin=15,
min_run_time=30, # min run time in seconds min_run_time=30,
min_off_time=5 # min off time in seconds min_off_time=5
) )
# Use loaded config values for AC monitor
ac_monitor = ACMonitor( ac_monitor = ACMonitor(
ac_controller=ac_controller, ac_controller=ac_controller,
temp_sensor=sensors['inside'], temp_sensor=sensors['inside'],
target_temp=77.0, # target temperature in Fahrenheit target_temp=config['ac_target'],
temp_swing=1.0, # temp swing target_temp-temp_swing to target_temp+temp_swing temp_swing=config['ac_swing'],
interval=30 # check temp every x seconds interval=30
) )
# Heater Controller options # Heater Controller options
heater_controller = HeaterController( heater_controller = HeaterController(
relay_pin=16, relay_pin=16,
min_run_time=30, # min run time in seconds min_run_time=30,
min_off_time=5 # min off time in seconds min_off_time=5
) )
# Use loaded config values for heater monitor
heater_monitor = HeaterMonitor( heater_monitor = HeaterMonitor(
heater_controller=heater_controller, heater_controller=heater_controller,
temp_sensor=sensors['inside'], temp_sensor=sensors['inside'],
target_temp=80.0, # target temperature in Fahrenheit target_temp=config['heater_target'],
temp_swing=2.0, # temp swing temp_swing=config['heater_swing'],
interval=30 # check temp every x seconds interval=30
) )
# Create schedule monitor
schedule_monitor = ScheduleMonitor(
ac_monitor=ac_monitor,
heater_monitor=heater_monitor,
config=config,
interval=60 # Check schedule every 60 seconds
)
# Print loaded settings
print("\n" + "="*50)
print("Current Climate Control Settings:")
print("="*50)
print(f"AC Target: {config['ac_target']}°F ± {config['ac_swing']}°F")
print(f"Heater Target: {config['heater_target']}°F ± {config['heater_swing']}°F")
print(f"Schedule: {'Enabled' if config.get('schedule_enabled') else 'Disabled'}")
if config.get('schedules'):
print(f"Schedules: {len(config.get('schedules', []))} configured")
print("="*50 + "\n")
# Set up monitors # Set up monitors
monitors = [ monitors = [
WiFiMonitor(wifi, led, interval=5, reconnect_cooldown=60), # Wifi monitor, Check WiFi every 5s WiFiMonitor(wifi, led, interval=5, reconnect_cooldown=60),
ac_monitor, # AC monitor schedule_monitor, # Add schedule monitor
heater_monitor, # Heater monitor ac_monitor,
TemperatureMonitor( # Inside temperature monitor heater_monitor,
TemperatureMonitor(
sensor=sensors['inside'], sensor=sensors['inside'],
label=SENSOR_CONFIG['inside']['label'], label=SENSOR_CONFIG['inside']['label'],
check_interval=10, # Check temp every 10 seconds check_interval=10,
report_interval=30, # Report/log every 30 seconds report_interval=30,
alert_high=SENSOR_CONFIG['inside']['alert_high'], alert_high=SENSOR_CONFIG['inside']['alert_high'],
alert_low=SENSOR_CONFIG['inside']['alert_low'], alert_low=SENSOR_CONFIG['inside']['alert_low'],
log_file="/temp_logs.csv", log_file="/temp_logs.csv",
send_alerts_to_separate_channel=True send_alerts_to_separate_channel=True
), ),
TemperatureMonitor( # Outside temperature monitor TemperatureMonitor(
sensor=sensors['outside'], sensor=sensors['outside'],
label=SENSOR_CONFIG['outside']['label'], label=SENSOR_CONFIG['outside']['label'],
check_interval=10, # Check temp every 10 seconds check_interval=10,
report_interval=30, # Report/log every 30 seconds report_interval=30,
alert_high=SENSOR_CONFIG['outside']['alert_high'], alert_high=SENSOR_CONFIG['outside']['alert_high'],
alert_low=SENSOR_CONFIG['outside']['alert_low'], alert_low=SENSOR_CONFIG['outside']['alert_low'],
log_file="/temp_logs.csv", log_file="/temp_logs.csv",