Compare commits

...

3 Commits

3 changed files with 116 additions and 1 deletions

3
.gitignore vendored
View File

@ -2,4 +2,5 @@
secrets.py
/pymakr-test/
.gitignore
.vscode/
.vscode/
temp_logs.csv

View File

@ -0,0 +1,68 @@
from machine import Pin
import time
class ACController:
"""Control AC unit via opto-coupler relay."""
def __init__(self, relay_pin=15, min_run_time=300, min_off_time=180):
"""
relay_pin: GPIO pin connected to opto-coupler input
min_run_time: Minimum seconds AC must run before turning off (prevent short cycling)
min_off_time: Minimum seconds AC must be off before turning on (compressor protection)
"""
self.relay = Pin(relay_pin, Pin.OUT)
self.relay.off() # Start with AC off (relay normally open)
self.min_run_time = min_run_time
self.min_off_time = min_off_time
self.is_on = False
self.last_state_change = time.ticks_ms()
def turn_on(self):
"""Turn AC on if minimum off time has elapsed."""
if self.is_on:
return True # Already on
now = time.ticks_ms()
time_since_change = time.ticks_diff(now, self.last_state_change) / 1000
if time_since_change < self.min_off_time:
remaining = int(self.min_off_time - time_since_change)
print(f"AC cooldown: {remaining}s remaining before can turn on")
return False
self.relay.on()
self.is_on = True
self.last_state_change = now
print("AC turned ON")
return True
def turn_off(self):
"""Turn AC off if minimum run time has elapsed."""
if not self.is_on:
return True # Already off
now = time.ticks_ms()
time_since_change = time.ticks_diff(now, self.last_state_change) / 1000
if time_since_change < self.min_run_time:
remaining = int(self.min_run_time - time_since_change)
print(f"AC minimum runtime: {remaining}s remaining before can turn off")
return False
self.relay.off()
self.is_on = False
self.last_state_change = now
print("AC turned OFF")
return True
def get_state(self):
"""Return current AC state."""
return self.is_on
def force_off(self):
"""Emergency shut off (bypasses timers)."""
self.relay.off()
self.is_on = False
self.last_state_change = time.ticks_ms()
print("AC FORCE OFF")

View File

@ -104,6 +104,52 @@ class TemperatureMonitor(Monitor):
except Exception as e:
print(f"Error logging temperature: {e}")
class ACMonitor(Monitor):
"""Monitor temperature and control AC automatically."""
def __init__(self, ac_controller, temp_sensor, target_temp=75.0, hysteresis=2.0, interval=30):
"""
ac_controller: ACController instance
temp_sensor: TemperatureSensor instance (inside temp)
target_temp: Target temperature in °F
hysteresis: Temperature swing allowed (prevents rapid cycling)
interval: Seconds between checks
"""
super().__init__(interval)
self.ac = ac_controller
self.sensor = temp_sensor
self.target_temp = target_temp
self.hysteresis = hysteresis
self.last_notified_state = None
def run(self):
"""Check temperature and control AC."""
temps = self.sensor.read_all_temps(unit='F')
if not temps:
return
# Use first sensor reading (assuming single inside sensor)
current_temp = list(temps.values())[0]
# Cooling logic with hysteresis
# Turn ON if: temp > target + hysteresis
# Turn OFF if: temp < target - hysteresis
if current_temp > (self.target_temp + self.hysteresis):
# Too hot, turn AC on
if self.ac.turn_on():
if not self.last_notified_state:
send_discord_message(f"❄️ AC turned ON - Current: {current_temp:.1f}°F, Target: {self.target_temp:.1f}°F")
self.last_notified_state = True
elif current_temp < (self.target_temp - self.hysteresis):
# Cool enough, turn AC off
if self.ac.turn_off():
if self.last_notified_state:
send_discord_message(f"✅ AC turned OFF - Current: {current_temp:.1f}°F, Target: {self.target_temp:.1f}°F")
self.last_notified_state = False
# Else: within hysteresis range, maintain current state
class WiFiMonitor(Monitor):
"""Monitor WiFi connection and handle reconnection."""
def __init__(self, wifi, led, interval=5, reconnect_cooldown=60):