Add thermostat control for heat and fan with cycle protection
Turns the weather dashboard into an actual thermostat. Relays are not
fitted yet -- the GPIOs are driven and can be metered at the UART1-OUT
plug (idle 3.3V, energized 0V, active-low module assumed).
Control loop (main/control/thermostat.c) is pure logic: no ESP-IDF, no
network, no clock of its own. The caller passes a temperature and a
millisecond timestamp, which is what lets the simulator and the test
harness run the identical decision code.
Equipment protections, all verified by tools/thermostat_test.c:
deadband 1.0F stops the burner chattering at the setpoint
minimum on 3 min a shorter call is all ignition wear, no heat
minimum off 5 min back-to-back ignitions are what kill igniters
cycles/hour 5 standard gas default; catches a flapping sensor
boot lockout 60 s a boot loop must not machine-gun the gas valve
That test drives the state machine with synthetic time, so two hours of
furnace behaviour runs in milliseconds. It exits non-zero on failure and
is worth re-running after any edit to thermostat.c -- this switches a gas
valve.
Two deliberate decisions:
- G is never energized during a heat call. The furnace runs its own
blower with a fan-on delay that waits for the heat exchanger and a
fan-off delay that scavenges residual heat; driving G ourselves would
blow cold air at people and throw that heat away. G is manual
circulation only.
- The control loop starts before every early return in app_main. Those
returns are all "no WiFi" or "no HA token", and none of them is a
reason to leave a house unheated.
Relay pins are IO20 heat / IO19 fan on the UART1-OUT plug, measured on
this board since Elecrow documents the GPIO numbers but not the connector
pin order. Heat is on IO20 because it is USB D+, the line carrying the
hardware pull-up -- with an active-low module it fails toward "no heat"
rather than "heat stuck on" if the USB PHY is ever re-enabled. Both pins
were confirmed free of the onboard mic before use.
CONFIG_USJ_ENABLE_USB_SERIAL_JTAG and CONFIG_ESP_PHY_ENABLE_USB are now
off to release them; the console is on UART0 via the CH340 regardless.
UI: the indoor card gains a status dot, mode and fan buttons, and a
setpoint column with finger-sized up/down targets. The reading grid drops
from six cells to four to make room -- wind was the one to cut, since the
station anemometer has been dead since 2026-07-27 and that cell has never
shown a number. Wind now rides on the outdoor cell's sub-line and will
reappear on its own if the sensor is fixed. Sunrise and sunset share a
cell, showing whichever comes next.
Known weakness, documented in README and top of TODO: indoor temperature
comes from Home Assistant over the network, so an HA outage ages the
reading until the 15-minute failsafe forces heat off. A local I2C sensor
removes the failure mode entirely and is the next thing to add. The
setpoint also resets to 68F on reboot, because persisting it means an NVS
write and a flash write while the panel scans faults the bounce-buffer
ISR.
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
1 parent
ae5d12b3a2
commit
3aabcac90a
16 files changed
+1420
-53
No files matched your search
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Host implementation of relay.h -- a fake furnace.
|
||||
*
|
||||
* Lets the real control loop run on the desktop: hysteresis, minimum on and
|
||||
* off times and the cycles-per-hour ceiling all behave exactly as they will on
|
||||
* the device, with the relay changes printed instead of switching 24VAC.
|
||||
*
|
||||
* Timestamps are wall-clock relative to start-up so a log can be read against
|
||||
* the protection timings in thermostat.h without arithmetic.
|
||||
*/
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "relay.h"
|
||||
|
||||
static bool relay_state[RELAY_COUNT];
|
||||
static struct timespec t0;
|
||||
|
||||
static double seconds_since_start(void)
|
||||
{
|
||||
struct timespec now;
|
||||
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||
return (double)(now.tv_sec - t0.tv_sec) +
|
||||
(double)(now.tv_nsec - t0.tv_nsec) / 1e9;
|
||||
}
|
||||
|
||||
esp_err_t relay_init(void)
|
||||
{
|
||||
clock_gettime(CLOCK_MONOTONIC, &t0);
|
||||
for (int i = 0; i < RELAY_COUNT; i++) {
|
||||
relay_state[i] = false;
|
||||
}
|
||||
fprintf(stderr, "[relay-sim] both relays idle\n");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
void relay_set(relay_id_t id, bool energized)
|
||||
{
|
||||
if (id < 0 || id >= RELAY_COUNT || relay_state[id] == energized) {
|
||||
return;
|
||||
}
|
||||
relay_state[id] = energized;
|
||||
fprintf(stderr, "[relay-sim] %7.1fs %-8s -> %s\n",
|
||||
seconds_since_start(), relay_name(id), energized ? "ON" : "OFF");
|
||||
}
|
||||
|
||||
bool relay_get(relay_id_t id)
|
||||
{
|
||||
if (id < 0 || id >= RELAY_COUNT) {
|
||||
return false;
|
||||
}
|
||||
return relay_state[id];
|
||||
}
|
||||
|
||||
const char *relay_name(relay_id_t id)
|
||||
{
|
||||
switch (id) {
|
||||
case RELAY_HEAT: return "heat(W)";
|
||||
case RELAY_FAN: return "fan(G)";
|
||||
default: return "?";
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user