Add schedule management to web server and configuration loading
This commit is contained in:
94
main.py
94
main.py
@@ -1,6 +1,7 @@
|
||||
from machine import Pin
|
||||
import time
|
||||
import network
|
||||
import json
|
||||
|
||||
# Initialize pins (LED light onboard)
|
||||
led = Pin("LED", Pin.OUT)
|
||||
@@ -24,6 +25,29 @@ from scripts.temperature_sensor import TemperatureSensor
|
||||
from scripts.air_conditioning import ACController
|
||||
from scripts.heating import HeaterController
|
||||
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
|
||||
wifi = connect_wifi(led)
|
||||
@@ -31,10 +55,10 @@ wifi = connect_wifi(led)
|
||||
# Set static IP and print WiFi details
|
||||
if wifi and wifi.isconnected():
|
||||
# Configure static IP
|
||||
static_ip = '192.168.86.43' # Your desired static IP
|
||||
subnet = '255.255.255.0' # Subnet mask (usually this)
|
||||
gateway = '192.168.86.1' # Your router's IP
|
||||
dns = '192.168.86.1' # DNS server (usually same as gateway)
|
||||
static_ip = '192.168.86.43'
|
||||
subnet = '255.255.255.0'
|
||||
gateway = '192.168.86.1'
|
||||
dns = '192.168.86.1'
|
||||
|
||||
wifi.ifconfig((static_ip, subnet, gateway, dns))
|
||||
time.sleep(1)
|
||||
@@ -62,7 +86,7 @@ else:
|
||||
web_server = TempWebServer(port=80)
|
||||
web_server.start()
|
||||
|
||||
# Sensor configuration registry (moved from temperature_sensor.py)
|
||||
# Sensor configuration registry
|
||||
SENSOR_CONFIG = {
|
||||
'inside': {
|
||||
'pin': 10,
|
||||
@@ -92,53 +116,75 @@ sensors = get_configured_sensors()
|
||||
# AC Controller options
|
||||
ac_controller = ACController(
|
||||
relay_pin=15,
|
||||
min_run_time=30, # min run time in seconds
|
||||
min_off_time=5 # min off time in seconds
|
||||
min_run_time=30,
|
||||
min_off_time=5
|
||||
)
|
||||
|
||||
# Use loaded config values for AC monitor
|
||||
ac_monitor = ACMonitor(
|
||||
ac_controller=ac_controller,
|
||||
temp_sensor=sensors['inside'],
|
||||
target_temp=77.0, # target temperature in Fahrenheit
|
||||
temp_swing=1.0, # temp swing target_temp-temp_swing to target_temp+temp_swing
|
||||
interval=30 # check temp every x seconds
|
||||
target_temp=config['ac_target'],
|
||||
temp_swing=config['ac_swing'],
|
||||
interval=30
|
||||
)
|
||||
|
||||
# Heater Controller options
|
||||
heater_controller = HeaterController(
|
||||
relay_pin=16,
|
||||
min_run_time=30, # min run time in seconds
|
||||
min_off_time=5 # min off time in seconds
|
||||
min_run_time=30,
|
||||
min_off_time=5
|
||||
)
|
||||
|
||||
# Use loaded config values for heater monitor
|
||||
heater_monitor = HeaterMonitor(
|
||||
heater_controller=heater_controller,
|
||||
temp_sensor=sensors['inside'],
|
||||
target_temp=80.0, # target temperature in Fahrenheit
|
||||
temp_swing=2.0, # temp swing
|
||||
interval=30 # check temp every x seconds
|
||||
target_temp=config['heater_target'],
|
||||
temp_swing=config['heater_swing'],
|
||||
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
|
||||
monitors = [
|
||||
WiFiMonitor(wifi, led, interval=5, reconnect_cooldown=60), # Wifi monitor, Check WiFi every 5s
|
||||
ac_monitor, # AC monitor
|
||||
heater_monitor, # Heater monitor
|
||||
TemperatureMonitor( # Inside temperature monitor
|
||||
WiFiMonitor(wifi, led, interval=5, reconnect_cooldown=60),
|
||||
schedule_monitor, # Add schedule monitor
|
||||
ac_monitor,
|
||||
heater_monitor,
|
||||
TemperatureMonitor(
|
||||
sensor=sensors['inside'],
|
||||
label=SENSOR_CONFIG['inside']['label'],
|
||||
check_interval=10, # Check temp every 10 seconds
|
||||
report_interval=30, # Report/log every 30 seconds
|
||||
check_interval=10,
|
||||
report_interval=30,
|
||||
alert_high=SENSOR_CONFIG['inside']['alert_high'],
|
||||
alert_low=SENSOR_CONFIG['inside']['alert_low'],
|
||||
log_file="/temp_logs.csv",
|
||||
send_alerts_to_separate_channel=True
|
||||
),
|
||||
TemperatureMonitor( # Outside temperature monitor
|
||||
TemperatureMonitor(
|
||||
sensor=sensors['outside'],
|
||||
label=SENSOR_CONFIG['outside']['label'],
|
||||
check_interval=10, # Check temp every 10 seconds
|
||||
report_interval=30, # Report/log every 30 seconds
|
||||
check_interval=10,
|
||||
report_interval=30,
|
||||
alert_high=SENSOR_CONFIG['outside']['alert_high'],
|
||||
alert_low=SENSOR_CONFIG['outside']['alert_low'],
|
||||
log_file="/temp_logs.csv",
|
||||
|
||||
Reference in New Issue
Block a user