Compare commits
No commits in common. "f4be1a7f7d47d754f2f8d7532370501c87de9640" and "1fb3511ed55a8ac9b1f539c9c5313f1b465852ca" have entirely different histories.
f4be1a7f7d
...
1fb3511ed5
@ -1,143 +0,0 @@
|
|||||||
import time
|
|
||||||
|
|
||||||
class ScheduleMonitor:
|
|
||||||
"""Monitor that checks and applies temperature schedules."""
|
|
||||||
|
|
||||||
def __init__(self, ac_monitor, heater_monitor, config, interval=60):
|
|
||||||
"""
|
|
||||||
Initialize schedule monitor.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
ac_monitor: ACMonitor instance
|
|
||||||
heater_monitor: HeaterMonitor instance
|
|
||||||
config: Configuration dict with schedules
|
|
||||||
interval: How often to check schedule (seconds)
|
|
||||||
"""
|
|
||||||
self.ac_monitor = ac_monitor
|
|
||||||
self.heater_monitor = heater_monitor
|
|
||||||
self.config = config
|
|
||||||
self.interval = interval
|
|
||||||
self.last_check = 0
|
|
||||||
self.current_schedule = None
|
|
||||||
self.last_applied_schedule = None
|
|
||||||
|
|
||||||
def should_run(self):
|
|
||||||
"""Check if it's time to run this monitor."""
|
|
||||||
current_time = time.time()
|
|
||||||
if current_time - self.last_check >= self.interval:
|
|
||||||
self.last_check = current_time
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
def _parse_time(self, time_str):
|
|
||||||
"""Convert time string 'HH:MM' to minutes since midnight."""
|
|
||||||
try:
|
|
||||||
parts = time_str.split(':')
|
|
||||||
hours = int(parts[0])
|
|
||||||
minutes = int(parts[1])
|
|
||||||
return hours * 60 + minutes
|
|
||||||
except:
|
|
||||||
return None
|
|
||||||
|
|
||||||
def _get_current_minutes(self):
|
|
||||||
"""Get current time in minutes since midnight."""
|
|
||||||
t = time.localtime()
|
|
||||||
return t[3] * 60 + t[4] # hours * 60 + minutes
|
|
||||||
|
|
||||||
def _find_active_schedule(self):
|
|
||||||
"""Find which schedule should be active right now."""
|
|
||||||
if not self.config.get('schedule_enabled', False):
|
|
||||||
return None
|
|
||||||
|
|
||||||
schedules = self.config.get('schedules', [])
|
|
||||||
if not schedules:
|
|
||||||
return None
|
|
||||||
|
|
||||||
current_minutes = self._get_current_minutes()
|
|
||||||
|
|
||||||
# Sort schedules by time
|
|
||||||
sorted_schedules = []
|
|
||||||
for schedule in schedules:
|
|
||||||
schedule_minutes = self._parse_time(schedule['time'])
|
|
||||||
if schedule_minutes is not None:
|
|
||||||
sorted_schedules.append((schedule_minutes, schedule))
|
|
||||||
|
|
||||||
sorted_schedules.sort()
|
|
||||||
|
|
||||||
# Find the most recent schedule that has passed
|
|
||||||
active_schedule = None
|
|
||||||
for schedule_minutes, schedule in sorted_schedules:
|
|
||||||
if current_minutes >= schedule_minutes:
|
|
||||||
active_schedule = schedule
|
|
||||||
else:
|
|
||||||
break
|
|
||||||
|
|
||||||
# If no schedule found (before first schedule), use last schedule from yesterday
|
|
||||||
if active_schedule is None and sorted_schedules:
|
|
||||||
active_schedule = sorted_schedules[-1][1]
|
|
||||||
|
|
||||||
return active_schedule
|
|
||||||
|
|
||||||
def _apply_schedule(self, schedule):
|
|
||||||
"""Apply a schedule's settings to the monitors."""
|
|
||||||
if not schedule:
|
|
||||||
return
|
|
||||||
|
|
||||||
# Check if this is a different schedule than last applied
|
|
||||||
schedule_id = schedule.get('time', '') + schedule.get('name', '')
|
|
||||||
if schedule_id == self.last_applied_schedule:
|
|
||||||
return # Already applied
|
|
||||||
|
|
||||||
try:
|
|
||||||
# Update AC settings if provided
|
|
||||||
if 'ac_target' in schedule:
|
|
||||||
self.ac_monitor.target_temp = float(schedule['ac_target'])
|
|
||||||
|
|
||||||
if 'ac_swing' in schedule:
|
|
||||||
self.ac_monitor.temp_swing = float(schedule['ac_swing'])
|
|
||||||
|
|
||||||
# Update heater settings if provided
|
|
||||||
if 'heater_target' in schedule:
|
|
||||||
self.heater_monitor.target_temp = float(schedule['heater_target'])
|
|
||||||
|
|
||||||
if 'heater_swing' in schedule:
|
|
||||||
self.heater_monitor.temp_swing = float(schedule['heater_swing'])
|
|
||||||
|
|
||||||
# Log the change
|
|
||||||
schedule_name = schedule.get('name', 'Unnamed')
|
|
||||||
print("\n" + "="*50)
|
|
||||||
print("Schedule Applied: {}".format(schedule_name))
|
|
||||||
print("="*50)
|
|
||||||
print("AC Target: {}°F".format(self.ac_monitor.target_temp))
|
|
||||||
print("Heater Target: {}°F".format(self.heater_monitor.target_temp))
|
|
||||||
print("="*50 + "\n")
|
|
||||||
|
|
||||||
# Send Discord notification
|
|
||||||
try:
|
|
||||||
from scripts.discord_webhook import send_discord_message
|
|
||||||
message = "🕐 Schedule '{}' applied - AC: {}°F | Heater: {}°F".format(
|
|
||||||
schedule_name,
|
|
||||||
self.ac_monitor.target_temp,
|
|
||||||
self.heater_monitor.target_temp
|
|
||||||
)
|
|
||||||
send_discord_message(message)
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
|
|
||||||
self.last_applied_schedule = schedule_id
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
print("Error applying schedule: {}".format(e))
|
|
||||||
|
|
||||||
def run(self):
|
|
||||||
"""Check if schedule needs to be updated."""
|
|
||||||
# Find and apply active schedule
|
|
||||||
active_schedule = self._find_active_schedule()
|
|
||||||
if active_schedule:
|
|
||||||
self._apply_schedule(active_schedule)
|
|
||||||
|
|
||||||
def reload_config(self, new_config):
|
|
||||||
"""Reload configuration (called when settings are updated via web)."""
|
|
||||||
self.config = new_config
|
|
||||||
self.last_applied_schedule = None # Force re-application
|
|
||||||
print("Schedule configuration reloaded")
|
|
||||||
@ -132,46 +132,7 @@ class TempWebServer:
|
|||||||
|
|
||||||
# Get current time
|
# Get current time
|
||||||
current_time = time.localtime()
|
current_time = time.localtime()
|
||||||
time_str = "{}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}".format(
|
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}"
|
||||||
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 = """
|
||||||
@ -464,19 +425,6 @@ 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
|
||||||
@ -485,8 +433,8 @@ class TempWebServer:
|
|||||||
</html>
|
</html>
|
||||||
""".format(
|
""".format(
|
||||||
success_message=success_html,
|
success_message=success_html,
|
||||||
inside_temp="{:.1f}".format(inside_temp) if isinstance(inside_temp, float) else inside_temp,
|
inside_temp=f"{inside_temp:.1f}" if isinstance(inside_temp, float) else inside_temp,
|
||||||
outside_temp="{:.1f}".format(outside_temp) if isinstance(outside_temp, float) else outside_temp,
|
outside_temp=f"{outside_temp:.1f}" 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,
|
||||||
@ -495,9 +443,6 @@ 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
|
||||||
29
config.json
29
config.json
@ -2,32 +2,5 @@
|
|||||||
"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
94
main.py
@ -1,7 +1,6 @@
|
|||||||
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)
|
||||||
@ -25,29 +24,6 @@ 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)
|
||||||
@ -55,10 +31,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'
|
static_ip = '192.168.86.43' # Your desired static IP
|
||||||
subnet = '255.255.255.0'
|
subnet = '255.255.255.0' # Subnet mask (usually this)
|
||||||
gateway = '192.168.86.1'
|
gateway = '192.168.86.1' # Your router's IP
|
||||||
dns = '192.168.86.1'
|
dns = '192.168.86.1' # DNS server (usually same as gateway)
|
||||||
|
|
||||||
wifi.ifconfig((static_ip, subnet, gateway, dns))
|
wifi.ifconfig((static_ip, subnet, gateway, dns))
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
@ -86,7 +62,7 @@ else:
|
|||||||
web_server = TempWebServer(port=80)
|
web_server = TempWebServer(port=80)
|
||||||
web_server.start()
|
web_server.start()
|
||||||
|
|
||||||
# Sensor configuration registry
|
# Sensor configuration registry (moved from temperature_sensor.py)
|
||||||
SENSOR_CONFIG = {
|
SENSOR_CONFIG = {
|
||||||
'inside': {
|
'inside': {
|
||||||
'pin': 10,
|
'pin': 10,
|
||||||
@ -116,75 +92,53 @@ 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=30, # min run time in seconds
|
||||||
min_off_time=5
|
min_off_time=5 # min off time in seconds
|
||||||
)
|
)
|
||||||
|
|
||||||
# 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=config['ac_target'],
|
target_temp=77.0, # target temperature in Fahrenheit
|
||||||
temp_swing=config['ac_swing'],
|
temp_swing=1.0, # temp swing target_temp-temp_swing to target_temp+temp_swing
|
||||||
interval=30
|
interval=30 # check temp every x seconds
|
||||||
)
|
)
|
||||||
|
|
||||||
# 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=30, # min run time in seconds
|
||||||
min_off_time=5
|
min_off_time=5 # min off time in seconds
|
||||||
)
|
)
|
||||||
|
|
||||||
# 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=config['heater_target'],
|
target_temp=80.0, # target temperature in Fahrenheit
|
||||||
temp_swing=config['heater_swing'],
|
temp_swing=2.0, # temp swing
|
||||||
interval=30
|
interval=30 # check temp every x seconds
|
||||||
)
|
)
|
||||||
|
|
||||||
# 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),
|
WiFiMonitor(wifi, led, interval=5, reconnect_cooldown=60), # Wifi monitor, Check WiFi every 5s
|
||||||
schedule_monitor, # Add schedule monitor
|
ac_monitor, # AC monitor
|
||||||
ac_monitor,
|
heater_monitor, # Heater monitor
|
||||||
heater_monitor,
|
TemperatureMonitor( # Inside temperature monitor
|
||||||
TemperatureMonitor(
|
|
||||||
sensor=sensors['inside'],
|
sensor=sensors['inside'],
|
||||||
label=SENSOR_CONFIG['inside']['label'],
|
label=SENSOR_CONFIG['inside']['label'],
|
||||||
check_interval=10,
|
check_interval=10, # Check temp every 10 seconds
|
||||||
report_interval=30,
|
report_interval=30, # Report/log every 30 seconds
|
||||||
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(
|
TemperatureMonitor( # Outside temperature monitor
|
||||||
sensor=sensors['outside'],
|
sensor=sensors['outside'],
|
||||||
label=SENSOR_CONFIG['outside']['label'],
|
label=SENSOR_CONFIG['outside']['label'],
|
||||||
check_interval=10,
|
check_interval=10, # Check temp every 10 seconds
|
||||||
report_interval=30,
|
report_interval=30, # Report/log every 30 seconds
|
||||||
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",
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user