-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
43 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,67 @@ | ||
#include "MQTTPlugin.h" | ||
#include "../core/Controller.h" | ||
|
||
void MQTTPlugin::connect(Controller *controller) { | ||
Settings settings = controller->getSettings(); | ||
String ip = settings.getHomeAssistantIP(); | ||
int haPort = settings.getHomeAssistantPort(); | ||
String clientId = "GaggiMate"; | ||
String haUser = settings.getHomeAssistantUser(); | ||
String haPassword = settings.getHomeAssistantPassword(); | ||
int keepAline = 60; | ||
|
||
bool MQTTPlugin::connect(Controller *controller) { | ||
const Settings settings = controller->getSettings(); | ||
const String ip = settings.getHomeAssistantIP(); | ||
const int haPort = settings.getHomeAssistantPort(); | ||
const String clientId = "GaggiMate"; | ||
const String haUser = settings.getHomeAssistantUser(); | ||
const String haPassword = settings.getHomeAssistantPassword(); | ||
|
||
client.begin(ip.c_str(), haPort, net); | ||
while (!client.connect(clientId.c_str(), haUser.c_str(), haPassword.c_str())) { | ||
void setKeepAlive(int keepAlive); | ||
Serial.print("."); | ||
delay(5000); | ||
printf("Connecting to MQTT"); | ||
for (int i = 0; i < MQTT_CONNECTION_RETRIES; i++) { | ||
if (client.connect(clientId.c_str(), haUser.c_str(), haPassword.c_str())) { | ||
printf("\n"); | ||
return true; | ||
} | ||
printf("."); | ||
delay(MQTT_CONNECTION_DELAY); | ||
} | ||
printf("\nConnection to MQTT failed.\n"); | ||
return false; | ||
} | ||
|
||
void MQTTPlugin::setup(Controller *controller, PluginManager *pluginManager) { | ||
pluginManager->on("controller:wifi:connect", [this, controller](Event& event) { | ||
connect(controller); | ||
pluginManager->on("controller:wifi:connect", [this, controller](const Event&) { | ||
if (!connect(controller)) | ||
return; | ||
String mac = WiFi.macAddress(); | ||
mac.replace(":", "_"); | ||
const char* cmac = mac.c_str(); | ||
char topic[50], json[500]; | ||
sprintf(topic, "gaggimate/%s/config", cmac); | ||
sprintf(json, R"***({"dev":{"ids":"%s","name":"GaggiMate","mf":"GaggiMate","mdl":"GaggiMate","sw":"1.0","sn":"%s","hw":"1.0"},"o":{"name":"GaggiMate","sw":"v0.3.0","url":"https://gaggimate.eu/"},"cmps":{"boiler":{"p":"sensor","device_class":"temperature","unit_of_measurement":"°C","value_template":"{{ value_json.temperature }}","unique_id":"boiler0Tmp","state_topic":"gaggimate/%s/boilers/0/temperature"}},"state_topic":"gaggimate/%s/state","qos":2})***", cmac, cmac, cmac, cmac); | ||
char topic[50]; | ||
char json[500]; | ||
snprintf(topic, sizeof(topic), "gaggimate/%s/config", cmac); | ||
snprintf(json, sizeof(json), R"***({"dev":{"ids":"%s","name":"GaggiMate","mf":"GaggiMate","mdl":"GaggiMate","sw":"1.0","sn":"%s","hw":"1.0"},"o":{"name":"GaggiMate","sw":"v0.3.0","url":"https://gaggimate.eu/"},"cmps":{"boiler":{"p":"sensor","device_class":"temperature","unit_of_measurement":"°C","value_template":"{{ value_json.temperature }}","unique_id":"boiler0Tmp","state_topic":"gaggimate/%s/boilers/0/temperature"}},"state_topic":"gaggimate/%s/state","qos":2})***", cmac, cmac, cmac, cmac); | ||
client.publish(topic, json); | ||
}); | ||
|
||
pluginManager->on("boiler:currentTemperature:change", [this](Event &event) { | ||
pluginManager->on("boiler:currentTemperature:change", [this](Event const &event) { | ||
if (!client.connected()) | ||
return; | ||
String mac = WiFi.macAddress(); | ||
mac.replace(":", "_"); | ||
const char* cmac = mac.c_str(); | ||
char topic[50], json[50]; | ||
sprintf(topic, "gaggimate/%s/boilers/0/temperature", cmac); | ||
char topic[50]; | ||
char json[50]; | ||
snprintf(topic, sizeof(topic), "gaggimate/%s/boilers/0/temperature", cmac); | ||
float temp = event.getFloat("value"); | ||
sprintf(json, R"***({"temperature":%02f})***", temp); | ||
snprintf(json, sizeof(json), R"***({"temperature":%02f})***", temp); | ||
client.publish(topic, json); | ||
}); | ||
|
||
pluginManager->on("boiler:targetTemperature:change", [this](Event &event) { | ||
pluginManager->on("boiler:targetTemperature:change", [this](const Event &event) { | ||
if (!client.connected()) | ||
return; | ||
String mac = WiFi.macAddress(); | ||
mac.replace(":", "_"); | ||
const char* cmac = mac.c_str(); | ||
char topic[50], json[50]; | ||
sprintf(topic, "gaggimate/%s/boilers/0/targetTemperature", cmac); | ||
char topic[50]; | ||
char json[50]; | ||
snprintf(topic, sizeof(topic), "gaggimate/%s/boilers/0/targetTemperature", cmac); | ||
float temp = event.getFloat("value"); | ||
sprintf(json, R"***({"temperature":%02f})***", temp); | ||
snprintf(json, sizeof(json), R"***({"temperature":%02f})***", temp); | ||
client.publish(topic, json); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters