From e723bf87cbac22c0f8b8538e99c474878ff0d7e1 Mon Sep 17 00:00:00 2001 From: Serge NOEL Date: Wed, 11 Mar 2026 12:28:44 +0100 Subject: [PATCH] Ajout stockage EEPROM --- Thermostat/data/index.html | 6 ++-- Thermostat/include/web_config.h | 2 ++ Thermostat/src/mqtt_handler.cpp | 3 +- Thermostat/src/preset.cpp | 27 ++++++++++++-- Thermostat/src/thermostat.cpp | 21 +++++++++-- Thermostat/src/web_config.cpp | 64 +++++++++++++++++++++++++++++++-- 6 files changed, 110 insertions(+), 13 deletions(-) diff --git a/Thermostat/data/index.html b/Thermostat/data/index.html index 1ba7da2..50395af 100644 --- a/Thermostat/data/index.html +++ b/Thermostat/data/index.html @@ -22,9 +22,9 @@ - - - + + + diff --git a/Thermostat/include/web_config.h b/Thermostat/include/web_config.h index 61ce069..2090b97 100644 --- a/Thermostat/include/web_config.h +++ b/Thermostat/include/web_config.h @@ -17,3 +17,5 @@ extern float preset_eco; extern float preset_boost; extern float preset_hors_gel; extern String temp_sensor_id; +extern String mqtt_device_id; +extern String mqtt_temp_topic; diff --git a/Thermostat/src/mqtt_handler.cpp b/Thermostat/src/mqtt_handler.cpp index 55620c2..728653e 100644 --- a/Thermostat/src/mqtt_handler.cpp +++ b/Thermostat/src/mqtt_handler.cpp @@ -2,8 +2,7 @@ #include "thermostat.h" #include -String mqtt_device_id = "thermo1"; -String mqtt_temp_topic = "home/room/temperature/thermo1"; +// Variables now defined in web_config.cpp void setup_mqtt(PubSubClient& client) { // Setup MQTT connection, subscribe to temp topic diff --git a/Thermostat/src/preset.cpp b/Thermostat/src/preset.cpp index 4b06cce..d59a10b 100644 --- a/Thermostat/src/preset.cpp +++ b/Thermostat/src/preset.cpp @@ -1,13 +1,34 @@ #include "preset.h" #include -static float presets[5] = {0, 21.0, 18.0, 23.0, 7.0}; +static float presets[5] = {0, 20.0, 17.0, 22.0, 7.0}; +#define PRESET_EEPROM_START 0 +#define PRESET_COUNT 5 +#define PRESET_EEPROM_SIZE (PRESET_COUNT * sizeof(float)) void load_presets() { - // Load from EEPROM or SPIFFS + EEPROM.begin(PRESET_EEPROM_SIZE); + bool valid = true; + for (int i = 0; i < PRESET_COUNT; ++i) { + float value = 0; + EEPROM.get(PRESET_EEPROM_START + i * sizeof(float), value); + // Check for uninitialized EEPROM (NaN or out of range) + if (isnan(value) || value < 0 || value > 50) { + valid = false; + break; + } + presets[i] = value; + } + if (!valid) { + // If invalid, write defaults + save_presets(); + } } void save_presets() { - // Save to EEPROM or SPIFFS + for (int i = 0; i < PRESET_COUNT; ++i) { + EEPROM.put(PRESET_EEPROM_START + i * sizeof(float), presets[i]); + } + EEPROM.commit(); } float get_preset_temp(ThermostatMode mode) { return presets[mode]; diff --git a/Thermostat/src/thermostat.cpp b/Thermostat/src/thermostat.cpp index 0be578a..a9df4fe 100644 --- a/Thermostat/src/thermostat.cpp +++ b/Thermostat/src/thermostat.cpp @@ -1,27 +1,42 @@ #include "thermostat.h" Thermostat::Thermostat() : mode(MODE_OFF), targetTemp(0), heating(false) { - presetTemps[MODE_CONFORT] = 21.0; - presetTemps[MODE_ECO] = 18.0; - presetTemps[MODE_BOOST] = 23.0; + presetTemps[MODE_CONFORT] = 20.0; + presetTemps[MODE_ECO] = 17.0; + presetTemps[MODE_BOOST] = 22.0; presetTemps[MODE_HORS_GEL] = 7.0; presetTemps[MODE_OFF] = 0.0; } +/** + * Set the thermostat mode. This will automatically update the target temperature to the preset for that mode. + */ void Thermostat::setMode(ThermostatMode m) { mode = m; targetTemp = presetTemps[mode]; } +/** + * Manually set the target temperature. This does not change the mode or presets, but will override the preset for the + * current mode until the mode is changed again. + */ void Thermostat::setTemperature(float temp) { targetTemp = temp; } +/** + * Set the preset temperature for a specific mode. If the current mode is the one being updated, also update the + * target temperature. + */ void Thermostat::setPresetTemp(ThermostatMode m, float temp) { presetTemps[m] = temp; if (mode == m) targetTemp = temp; } +/** + * Update the heating state based on the current temperature and target temperature. + * this is real simple logic: if current temp is below target, turn on heating, otherwise turn it off. In a real system, you would want to add some hysteresis to prevent rapid on/off cycling. + */ void Thermostat::update(float currentTemp) { if (mode == MODE_OFF) { heating = false; diff --git a/Thermostat/src/web_config.cpp b/Thermostat/src/web_config.cpp index f8c04f4..975cf6b 100644 --- a/Thermostat/src/web_config.cpp +++ b/Thermostat/src/web_config.cpp @@ -1,17 +1,77 @@ + #include "web_config.h" #include +#include + String wifi_ssid, wifi_pass, mqtt_server, mqtt_user, mqtt_pass, temp_sensor_id; +String mqtt_device_id = "thermo1"; +String mqtt_temp_topic = "home/room/temperature/thermo1"; int mqtt_port = 1883; float preset_confort = 21.0, preset_eco = 18.0, preset_boost = 23.0, preset_hors_gel = 7.0; bool shouldSaveConfigFlag = false; +#define CFG_EEPROM_START 100 +#define CFG_EEPROM_SIZE 512 +#define CFG_STRLEN 32 + +struct ConfigData { + char wifi_ssid[CFG_STRLEN]; + char wifi_pass[CFG_STRLEN]; + char mqtt_server[CFG_STRLEN]; + char mqtt_user[CFG_STRLEN]; + char mqtt_pass[CFG_STRLEN]; + char temp_sensor_id[CFG_STRLEN]; + char mqtt_device_id[CFG_STRLEN]; + char mqtt_temp_topic[CFG_STRLEN]; + int mqtt_port; + float preset_confort; + float preset_eco; + float preset_boost; + float preset_hors_gel; +}; + void save_config() { - // Save config to SPIFFS or EEPROM + ConfigData cfg; + memset(&cfg, 0, sizeof(cfg)); + wifi_ssid.toCharArray(cfg.wifi_ssid, CFG_STRLEN); + wifi_pass.toCharArray(cfg.wifi_pass, CFG_STRLEN); + mqtt_server.toCharArray(cfg.mqtt_server, CFG_STRLEN); + mqtt_user.toCharArray(cfg.mqtt_user, CFG_STRLEN); + mqtt_pass.toCharArray(cfg.mqtt_pass, CFG_STRLEN); + temp_sensor_id.toCharArray(cfg.temp_sensor_id, CFG_STRLEN); + mqtt_device_id.toCharArray(cfg.mqtt_device_id, CFG_STRLEN); + mqtt_temp_topic.toCharArray(cfg.mqtt_temp_topic, CFG_STRLEN); + cfg.mqtt_port = mqtt_port; + cfg.preset_confort = preset_confort; + cfg.preset_eco = preset_eco; + cfg.preset_boost = preset_boost; + cfg.preset_hors_gel = preset_hors_gel; + EEPROM.begin(CFG_EEPROM_START + sizeof(ConfigData)); + EEPROM.put(CFG_EEPROM_START, cfg); + EEPROM.commit(); } void load_config() { - // Load config from SPIFFS or EEPROM + ConfigData cfg; + EEPROM.begin(CFG_EEPROM_START + sizeof(ConfigData)); + EEPROM.get(CFG_EEPROM_START, cfg); + // Only update if valid (basic check: mqtt_port in range) + if (cfg.mqtt_port > 0 && cfg.mqtt_port < 65536) { + wifi_ssid = String(cfg.wifi_ssid); + wifi_pass = String(cfg.wifi_pass); + mqtt_server = String(cfg.mqtt_server); + mqtt_user = String(cfg.mqtt_user); + mqtt_pass = String(cfg.mqtt_pass); + temp_sensor_id = String(cfg.temp_sensor_id); + mqtt_device_id = String(cfg.mqtt_device_id); + mqtt_temp_topic = String(cfg.mqtt_temp_topic); + mqtt_port = cfg.mqtt_port; + preset_confort = cfg.preset_confort; + preset_eco = cfg.preset_eco; + preset_boost = cfg.preset_boost; + preset_hors_gel = cfg.preset_hors_gel; + } } void setup_web_config() {