Skip to content

Commit

Permalink
Fix up time sensor again. Change retain on some topic. Send HP status…
Browse files Browse the repository at this point in the history
… periodic.
  • Loading branch information
dzungpv committed Aug 28, 2024
1 parent 5de4313 commit 02463a3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
2 changes: 1 addition & 1 deletion main/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ unsigned long requestWifiScanTime = 0;
#define WIFI_SCAN_PERIOD 120000
unsigned lastWifiScanMillis;

const PROGMEM char *m2mqtt_version = "2024.07.16";
const PROGMEM char *m2mqtt_version = "2024.08.28";

// Define global variables for files
int HP_TX = 0; // variable for the ESP32 custom TX pin, 0 is the defautl and it use hardware serial 0
Expand Down
24 changes: 11 additions & 13 deletions main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ void initMqtt()
static_cast<espMqttClientSecure *>(mqttClient)->setServer(mqtt_server.c_str(), atoi(mqtt_port.c_str()));
static_cast<espMqttClientSecure *>(mqttClient)->setCredentials(mqtt_username.c_str(), mqtt_password.c_str());
static_cast<espMqttClientSecure *>(mqttClient)->setClientId(mqtt_client_id.c_str());
static_cast<espMqttClientSecure *>(mqttClient)->setWill(ha_availability_topic.c_str(), 1, true, mqtt_payload_unavailable);
static_cast<espMqttClientSecure *>(mqttClient)->setWill(ha_availability_topic.c_str(), 1, false, mqtt_payload_unavailable);
#endif
}
else
Expand All @@ -814,7 +814,7 @@ void initMqtt()
static_cast<espMqttClient *>(mqttClient)->setServer(mqtt_server.c_str(), atoi(mqtt_port.c_str()));
static_cast<espMqttClient *>(mqttClient)->setCredentials(mqtt_username.c_str(), mqtt_password.c_str());
static_cast<espMqttClient *>(mqttClient)->setClientId(mqtt_client_id.c_str());
static_cast<espMqttClient *>(mqttClient)->setWill(ha_availability_topic.c_str(), 1, true, mqtt_payload_unavailable);
static_cast<espMqttClient *>(mqttClient)->setWill(ha_availability_topic.c_str(), 1, false, mqtt_payload_unavailable);
}

const char *apipch = mqtt_server.c_str();
Expand Down Expand Up @@ -2071,7 +2071,7 @@ void hpSettingsChanged()
{
String mqttOutput;
serializeJson(rootInfo, mqttOutput);
if (!mqttClient->publish(ha_settings_topic.c_str(), 1, true, mqttOutput.c_str()))
if (!mqttClient->publish(ha_settings_topic.c_str(), 1, false, mqttOutput.c_str()))
{
if (_debugModeLogs)
mqttClient->publish(ha_debug_logs_topic.c_str(), 1, false, (char *)("Failed to publish hp settings"));
Expand Down Expand Up @@ -2243,7 +2243,6 @@ void hpStatusChanged(heatpumpStatus currentStatus)
events.send(currentSettings.mode, "mode", millis(), 100);
events.send(currentSettings.power, "power", millis(), 110);
rootInfo["compressorFrequency"] = currentStatus.compressorFrequency;
rootInfo["up_time"] = getUpTime();
if (mqttClient != nullptr && mqttClient->connected())
{
String mqttOutput;
Expand Down Expand Up @@ -2278,12 +2277,16 @@ void sendKeepAlive()
// send keep alive message
if (mqttClient != nullptr && mqttClient->connected())
{
if (!mqttClient->publish(ha_availability_topic.c_str(), 1, true, mqtt_payload_available))
if (!mqttClient->publish(ha_availability_topic.c_str(), 1, false, mqtt_payload_available))
{
if (_debugModeLogs)
mqttClient->publish(ha_debug_logs_topic.c_str(), 1, false, (char *)"Failed to publish avialable status");
}
sendDeviceInfo();
if (hp.isConnected())
{
hpStatusChanged(hp.getStatus());
}
}
}

Expand Down Expand Up @@ -2749,9 +2752,10 @@ void sendDeviceInfo()
// get wifi rssi
haConfigInfo["rssi"] = String(WiFi.RSSI());
haConfigInfo["bssi"] = getWifiBSSID();
haConfigInfo["up_time"] = getUpTime();
String mqttOutput;
serializeJson(haConfigInfo, mqttOutput);
mqttClient->publish(ha_system_setting_info.c_str(), 1, true, mqttOutput.c_str());
mqttClient->publish(ha_system_setting_info.c_str(), 1, false, mqttOutput.c_str());
}

void sendHaConfig()
Expand Down Expand Up @@ -3096,12 +3100,6 @@ void loop()
#endif
if (wifiConnected && mqtt_connected)
{
// only send the temperature every SEND_ROOM_TEMP_INTERVAL_MS (millis rollover tolerant)
// if (millis() - lastTempSend > SEND_ROOM_TEMP_INTERVAL_MS)
// {
// hpStatusChanged(hp.getStatus());
// lastTempSend = millis();
// }
sendKeepAlive();
}
}
Expand Down Expand Up @@ -3236,7 +3234,7 @@ void onMqttConnect(bool sessionPresent)
mqttClient->subscribe(ha_remote_temp_set_topic.c_str(), 1);
mqttClient->subscribe(ha_custom_packet.c_str(), 1);
// send online message
mqttClient->publish(ha_availability_topic.c_str(), 1, true, mqtt_payload_available);
mqttClient->publish(ha_availability_topic.c_str(), 1, false, mqtt_payload_available);
sendHaConfig();
}

Expand Down

0 comments on commit 02463a3

Please sign in to comment.