Ajout stockage EEPROM

This commit is contained in:
Serge NOEL
2026-03-11 12:28:44 +01:00
parent 7a2c556fb6
commit e723bf87cb
6 changed files with 110 additions and 13 deletions

View File

@@ -22,9 +22,9 @@
<label>MQTT Password<input name="mqttpass" type="password"></label>
<label>Device ID<input name="devid" required></label>
<label>Temperature Sensor ID<input name="tempid" required></label>
<label>Confort Temp (°C)<input name="confort" type="number" value="21" step="0.1"></label>
<label>Eco Temp (°C)<input name="eco" type="number" value="18" step="0.1"></label>
<label>Boost Temp (°C)<input name="boost" type="number" value="23" step="0.1"></label>
<label>Confort Temp (°C)<input name="confort" type="number" value="20" step="0.1"></label>
<label>Eco Temp (°C)<input name="eco" type="number" value="17" step="0.1"></label>
<label>Boost Temp (°C)<input name="boost" type="number" value="22" step="0.1"></label>
<label>Hors Gel Temp (°C)<input name="horsgel" type="number" value="7" step="0.1"></label>
<button type="submit">Save & Reboot</button>
</form>

View File

@@ -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;

View File

@@ -2,8 +2,7 @@
#include "thermostat.h"
#include <ArduinoJson.h>
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

View File

@@ -1,13 +1,34 @@
#include "preset.h"
#include <EEPROM.h>
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];

View File

@@ -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;

View File

@@ -1,17 +1,77 @@
#include "web_config.h"
#include <WiFiManager.h>
#include <EEPROM.h>
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() {