Ajout Thermostat

This commit is contained in:
Serge NOEL
2026-03-11 11:50:45 +01:00
parent d3db51dba2
commit ab74dc5fbe
14 changed files with 496 additions and 0 deletions

36
Thermostat/src/main.cpp Normal file
View File

@@ -0,0 +1,36 @@
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include "thermostat.h"
#include "mqtt_handler.h"
#include "web_config.h"
#include "preset.h"
WiFiClient espClient;
PubSubClient mqttClient(espClient);
Thermostat thermostat;
float currentTemp = 0.0;
void setup() {
Serial.begin(115200);
load_config();
setup_web_config();
WiFi.begin(wifi_ssid.c_str(), wifi_pass.c_str());
mqttClient.setServer(mqtt_server.c_str(), mqtt_port);
setup_mqtt(mqttClient);
load_presets();
thermostat.setPresetTemp(MODE_CONFORT, preset_confort);
thermostat.setPresetTemp(MODE_ECO, preset_eco);
thermostat.setPresetTemp(MODE_BOOST, preset_boost);
thermostat.setPresetTemp(MODE_HORS_GEL, preset_hors_gel);
thermostat.setMode(MODE_OFF);
}
void loop() {
handle_web_config();
mqtt_loop(mqttClient);
thermostat.update(currentTemp);
mqtt_publish_state(mqttClient, thermostat.getMode(), thermostat.getTargetTemp(), thermostat.isHeating());
delay(1000);
}

View File

@@ -0,0 +1,33 @@
#include "mqtt_handler.h"
#include "thermostat.h"
#include <ArduinoJson.h>
String mqtt_device_id = "thermo1";
String mqtt_temp_topic = "home/room/temperature/thermo1";
void setup_mqtt(PubSubClient& client) {
// Setup MQTT connection, subscribe to temp topic
client.setCallback(mqtt_callback);
client.subscribe(mqtt_temp_topic.c_str());
}
void mqtt_loop(PubSubClient& client) {
if (!client.connected()) {
// reconnect logic here
}
client.loop();
}
void mqtt_publish_state(PubSubClient& client, ThermostatMode mode, float targetTemp, bool heating) {
StaticJsonDocument<128> doc;
doc["mode"] = mode;
doc["target"] = targetTemp;
doc["heating"] = heating;
char buf[128];
size_t n = serializeJson(doc, buf);
client.publish(("home/thermostat/" + mqtt_device_id + "/state").c_str(), buf, n);
}
void mqtt_callback(char* topic, byte* payload, unsigned int length) {
// Handle incoming MQTT messages (e.g., set mode, set preset, etc.)
}

18
Thermostat/src/preset.cpp Normal file
View File

@@ -0,0 +1,18 @@
#include "preset.h"
#include <EEPROM.h>
static float presets[5] = {0, 21.0, 18.0, 23.0, 7.0};
void load_presets() {
// Load from EEPROM or SPIFFS
}
void save_presets() {
// Save to EEPROM or SPIFFS
}
float get_preset_temp(ThermostatMode mode) {
return presets[mode];
}
void set_preset_temp(ThermostatMode mode, float temp) {
presets[mode] = temp;
save_presets();
}

View File

@@ -0,0 +1,35 @@
#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_HORS_GEL] = 7.0;
presetTemps[MODE_OFF] = 0.0;
}
void Thermostat::setMode(ThermostatMode m) {
mode = m;
targetTemp = presetTemps[mode];
}
void Thermostat::setTemperature(float temp) {
targetTemp = temp;
}
void Thermostat::setPresetTemp(ThermostatMode m, float temp) {
presetTemps[m] = temp;
if (mode == m) targetTemp = temp;
}
void Thermostat::update(float currentTemp) {
if (mode == MODE_OFF) {
heating = false;
} else {
heating = (currentTemp < targetTemp);
}
}
bool Thermostat::isHeating() const { return heating; }
ThermostatMode Thermostat::getMode() const { return mode; }
float Thermostat::getTargetTemp() const { return targetTemp; }

View File

@@ -0,0 +1,29 @@
#include "web_config.h"
#include <WiFiManager.h>
String wifi_ssid, wifi_pass, mqtt_server, mqtt_user, mqtt_pass, temp_sensor_id;
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;
void save_config() {
// Save config to SPIFFS or EEPROM
}
void load_config() {
// Load config from SPIFFS or EEPROM
}
void setup_web_config() {
WiFiManager wm;
// Add custom parameters for MQTT, presets, sensor id
// On save, set shouldSaveConfigFlag = true
wm.autoConnect("ThermostatConfig");
if (shouldSaveConfigFlag) save_config();
}
void handle_web_config() {
// Handle web config portal if needed
}
bool should_save_config() { return shouldSaveConfigFlag; }