diff --git a/src/lora/lorawan.c b/src/lora/lorawan.c index b830733..ad3cbb7 100644 --- a/src/lora/lorawan.c +++ b/src/lora/lorawan.c @@ -13,6 +13,7 @@ LOG_MODULE_REGISTER(lorawan_class_a); extern bool relais_state; extern bool relais_manual_override; +extern uint8_t temperature_threshold; const static struct device *lora_dev; static struct lorawan_join_config join_cfg; static uint8_t dev_eui[8]; @@ -41,13 +42,40 @@ static void dl_callback(uint8_t port, uint8_t flags, int16_t rssi, int8_t snr, u { uint8_t relais_value = hex_data[3]; LOG_INF("Received relais1 state change: %d", relais_value); - if (get_relais_state() != relais_value) + + if (relais_value == 0) { - set_relais_state(relais_value); - LOG_INF("Set relais 1 value"); + /* r1 0: Force relais OFF and disable temperature control */ + relais_state = 0; + relais_manual_override = true; + set_relais_state(0); + LOG_INF("Relais forced OFF, temperature control disabled"); + } + else if (relais_value == 1) + { + /* r1 1: Re-enable automatic temperature control */ + relais_manual_override = false; + LOG_INF("Automatic temperature control re-enabled"); + } + else + { + LOG_WRN("Invalid relais command value: %d (valid: 0=OFF, 1=AUTO)", relais_value); + } + } + /* Temperature threshold command: t[temp] (e.g., "t18" for 18°C) */ + else if (len > 2 && hex_data[0] == 't') + { + uint8_t new_threshold = hex_data[1]; + if (new_threshold >= 10 && new_threshold <= 35) // Valid range 10-35°C + { + temperature_threshold = new_threshold; + relais_manual_override = false; // Re-enable automatic control + LOG_INF("Temperature threshold updated to %d°C, automatic control enabled", temperature_threshold); + } + else + { + LOG_WRN("Invalid temperature threshold %d°C (valid range: 10-35°C)", new_threshold); } - relais_state = relais_value; - relais_manual_override = true; } } diff --git a/src/main.c b/src/main.c index cdb76dc..7aa979e 100644 --- a/src/main.c +++ b/src/main.c @@ -15,6 +15,7 @@ LOG_MODULE_REGISTER(g2h_heat_main); volatile bool relais_state = false; volatile bool relais_manual_override = false; +volatile uint8_t temperature_threshold = 21; /* MAIN */ int main(void) @@ -46,14 +47,14 @@ int main(void) /* Set relais based on temperature if not manually overridden */ if (!relais_manual_override) { - if (temp < 21.0) { - relais_state = 1; // Heat on below 21°C + if (temp < temperature_threshold) { + relais_state = 1; // Heat on below threshold } else { - relais_state = 0; // Heat off at/above 21°C + relais_state = 0; // Heat off at/above threshold } set_relais_state(relais_state); } - printk("relais: %d (manual: %d)\n", relais_state, relais_manual_override); + printk("relais: %d (manual: %d, threshold: %d°C)\n", relais_state, relais_manual_override, temperature_threshold); /* Pack and send values */ char data[7];