33 lines
945 B
C++
33 lines
945 B
C++
#include "mqtt_handler.h"
|
|
#include "thermostat.h"
|
|
#include <ArduinoJson.h>
|
|
|
|
// Variables now defined in web_config.cpp
|
|
|
|
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.)
|
|
}
|