Skip to content

Commit

Permalink
MQTT fix
Browse files Browse the repository at this point in the history
  • Loading branch information
DevNullGamer authored and jniebuhr committed Feb 1, 2025
1 parent 3d14266 commit 0ce7c95
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 17 deletions.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ build_flags =
-std=c++17
-std=gnu++17
-DLV_CONF_INCLUDE_SIMPLE
-DLV_CONF_PATH=${platformio.src_dir}/display/lv_conf.h
-DLV_CONF_PATH="${platformio.src_dir}"/display/lv_conf.h
-DDISABLE_ALL_LIBRARY_WARNINGS
-DLV_CONF_SUPPRESS_DEFINE_CHECK
-DARDUINO_USB_CDC_ON_BOOT=1
Expand Down
29 changes: 19 additions & 10 deletions src/display/plugins/MQTTPlugin.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
#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;

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);
}
}

void MQTTPlugin::setup(Controller *controller, PluginManager *pluginManager) {
pluginManager->on("controller:wifi:connect", [this, controller](Event& event) {
Settings settings = controller->getSettings();
String ip = settings.getHomeAssistantIP();
String clientId = "GaggiMate";
String haUser = settings.getHomeAssistantUser();
String haPassword = settings.getHomeAssistantPassword();
char hostname[ip.length() + 2];
ip.toCharArray(hostname, ip.length());
client.begin(hostname, settings.getHomeAssistantPort(), net);
client.connect(clientId.c_str(), haUser.c_str(), haPassword.c_str());
connect(controller);
String mac = WiFi.macAddress();
mac.replace(":", "_");
const char* cmac = mac.c_str();
char topic[50], json[500];
sprintf(topic, "homeassistant/device/%s/config", cmac);
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);
client.publish(topic, json);
});
Expand Down
1 change: 1 addition & 0 deletions src/display/plugins/MQTTPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
class MQTTPlugin : public Plugin {
public:
void setup(Controller *controller, PluginManager *pluginManager) override;
void connect(Controller *controller);
void loop() override {};
private:
MQTTClient client;
Expand Down
4 changes: 2 additions & 2 deletions src/display/plugins/WebUIPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ void WebUIPlugin::handleSettings(AsyncWebServerRequest *request) {
settings->setSmartGrindToggle(request->hasArg("smartGrindToggle"));
settings->setHomeAssistant(request->hasArg("homeAssistant"));
if (request->hasArg("haUser"))
settings->setHomeAssistantIP(request->arg("haUser"));
settings->setHomeAssistantUser(request->arg("haUser"));
if (request->hasArg("haPassword"))
settings->setHomeAssistantIP(request->arg("haPassword"));
settings->setHomeAssistantPassword(request->arg("haPassword"));
if (request->hasArg("haIP"))
settings->setHomeAssistantIP(request->arg("haIP"));
if (request->hasArg("haPort"))
Expand Down
3 changes: 3 additions & 0 deletions web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="color-scheme" content="light dark" />
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />
<link rel="shortcut icon" type="image/svg+xml" href="/gm.svg" />
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
Expand Down
6 changes: 3 additions & 3 deletions web/src/pages/Settings/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ export function Settings() {
name="haUser"
type="text"
className="input-field"
placeholder="0"
placeholder="user"
value={formData.haUser}
onChange={onChange('haUser')}
/>
Expand All @@ -424,8 +424,8 @@ export function Settings() {
name="haPassword"
type="password"
className="input-field"
placeholder="0"
value={formData.haUser}
placeholder="password"
value={formData.haPassword}
onChange={onChange('haPassword')}
/>
</div>
Expand Down

0 comments on commit 0ce7c95

Please sign in to comment.