Ajout Thermostat
This commit is contained in:
33
Thermostat/src/mqtt_handler.cpp
Normal file
33
Thermostat/src/mqtt_handler.cpp
Normal 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.)
|
||||
}
|
||||
Reference in New Issue
Block a user