use espMqttClient, qos2 fixed

This commit is contained in:
MichaelDvP
2023-06-03 16:36:53 +02:00
parent d9c2fe0fb9
commit 7865ddc51f
43 changed files with 3749 additions and 230 deletions

View File

@@ -1706,13 +1706,13 @@ void EMSdevice::mqtt_ha_entity_config_create() {
Mqtt::publish_ha_sensor_config(dv, name(), brand_to_char(), false, create_device_config);
dv.add_state(DeviceValueState::DV_HA_CONFIG_CREATED);
create_device_config = false; // only create the main config once
}
#ifndef EMSESP_STANDALONE
if (ESP.getFreeHeap() < (65 * 1024)) {
break;
}
// always create minimum one config
if (ESP.getMaxAllocHeap() < (6 * 1024) || (!emsesp::EMSESP::system_.PSram() && ESP.getFreeHeap() < (65 * 1024))) {
break;
}
#endif
}
}
ha_config_done(!create_device_config);

View File

@@ -1297,6 +1297,9 @@ void EMSESP::incoming_telegram(uint8_t * data, const uint8_t length) {
LOG_ERROR("Last Tx write rejected by host");
txservice_.send_poll(); // close the bus
}
} else if (tx_state == Telegram::Operation::TX_READ && length == 1) {
EMSbus::tx_state(Telegram::Operation::TX_READ); // reset Tx wait state
return;
} else if (tx_state == Telegram::Operation::TX_READ) {
// got a telegram with data in it. See if the src/dest matches that from the last one we sent and continue to process it
uint8_t src = data[0];

View File

@@ -23,7 +23,7 @@
namespace emsesp {
AsyncMqttClient * Mqtt::mqttClient_;
espMqttClient * Mqtt::mqttClient_;
// static parameters we make global
std::string Mqtt::mqtt_base_;
@@ -461,30 +461,6 @@ void Mqtt::start() {
// add the 'publish' command ('call system publish' in console or via API)
Command::add(EMSdevice::DeviceType::SYSTEM, F_(publish), System::command_publish, FL_(publish_cmd));
mqttClient_->onConnect([this](bool sessionPresent) { on_connect(); });
mqttClient_->onDisconnect([this](AsyncMqttClientDisconnectReason reason) {
// only show the error once, not every 2 seconds
if (!connecting_ && first_connect_attempted_) {
return;
}
first_connect_attempted_ = true;
connecting_ = false;
if (reason == AsyncMqttClientDisconnectReason::TCP_DISCONNECTED) {
LOG_WARNING("MQTT disconnected: TCP");
} else if (reason == AsyncMqttClientDisconnectReason::MQTT_IDENTIFIER_REJECTED) {
LOG_WARNING("MQTT disconnected: Identifier Rejected");
} else if (reason == AsyncMqttClientDisconnectReason::MQTT_SERVER_UNAVAILABLE) {
LOG_WARNING("MQTT disconnected: Server unavailable");
} else if (reason == AsyncMqttClientDisconnectReason::MQTT_MALFORMED_CREDENTIALS) {
LOG_WARNING("MQTT disconnected: Malformed credentials");
} else if (reason == AsyncMqttClientDisconnectReason::MQTT_NOT_AUTHORIZED) {
LOG_WARNING("MQTT disconnected: Not authorized");
} else {
LOG_WARNING("MQTT disconnected: code %d", reason);
}
});
// create last will topic with the base prefixed. It has to be static because asyncmqttclient destroys the reference
static char will_topic[MQTT_TOPIC_MAX_SIZE];
if (!mqtt_base_.empty()) {
@@ -493,9 +469,10 @@ void Mqtt::start() {
mqttClient_->setWill(will_topic, 1, true, "offline"); // with qos 1, retain true
mqttClient_->onMessage([this](const char * topic, const char * payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
on_message(topic, payload, len); // receiving mqtt
});
mqttClient_->onMessage(
[this](const espMqttClientTypes::MessageProperties & properties, const char * topic, const uint8_t * payload, size_t len, size_t index, size_t total) {
on_message(topic, (const char *)payload, len); // receiving mqtt
});
mqttClient_->onPublish([this](uint16_t packetId) {
on_publish(packetId); // publish
@@ -555,6 +532,30 @@ bool Mqtt::get_publish_onchange(uint8_t device_type) {
}
return false;
}
void Mqtt::on_disconnect(espMqttClientTypes::DisconnectReason reason) {
// only show the error once, not every 2 seconds
if (!connecting_) {
return;
}
connecting_ = false;
if (reason == espMqttClientTypes::DisconnectReason::TCP_DISCONNECTED) {
LOG_WARNING("MQTT disconnected: TCP");
} else if (reason == espMqttClientTypes::DisconnectReason::MQTT_UNACCEPTABLE_PROTOCOL_VERSION) {
LOG_WARNING("MQTT disconnected: Unacceptable protocol version");
} else if (reason == espMqttClientTypes::DisconnectReason::MQTT_IDENTIFIER_REJECTED) {
LOG_WARNING("MQTT disconnected: Identifier Rejected");
} else if (reason == espMqttClientTypes::DisconnectReason::MQTT_SERVER_UNAVAILABLE) {
LOG_WARNING("MQTT disconnected: Server unavailable");
} else if (reason == espMqttClientTypes::DisconnectReason::MQTT_MALFORMED_CREDENTIALS) {
LOG_WARNING("MQTT disconnected: Malformed credentials");
} else if (reason == espMqttClientTypes::DisconnectReason::MQTT_NOT_AUTHORIZED) {
LOG_WARNING("MQTT disconnected: Not authorized");
} else if (reason == espMqttClientTypes::DisconnectReason::TLS_BAD_FINGERPRINT) {
LOG_WARNING("MQTT disconnected: Server fingerprint invalid");
} else {
LOG_WARNING("MQTT disconnected: code %d", reason);
}
}
// MQTT onConnect - when an MQTT connect is established
void Mqtt::on_connect() {
@@ -680,7 +681,7 @@ void Mqtt::queue_message(const uint8_t operation, const std::string & topic, con
#ifndef EMSESP_STANDALONE
// anything below 60MB available free heap is dangerously low, so we stop adding to prevent a crash
// instead of doing a mqtt_messages_.pop_front()
if (mqtt_messages_.size() >= MAX_MQTT_MESSAGES || ESP.getFreeHeap() < (60 * 1024)) {
if (mqtt_messages_.size() >= MAX_MQTT_MESSAGES || (!emsesp::EMSESP::system_.PSram() && ESP.getFreeHeap() < (60 * 1024))) {
LOG_WARNING("Queue overflow (queue count=%d, topic=%s)", mqtt_messages_.size(), topic.c_str());
mqtt_publish_fails_++;
return; // don't add to top of queue
@@ -857,7 +858,8 @@ void Mqtt::process_queue() {
// else try and publish it
// this is where the *real* publish happens
uint16_t packet_id = mqttClient_->publish(topic, mqtt_qos_, message->retain, message->payload.c_str(), message->payload.size(), false, mqtt_message.id_);
// uint16_t packet_id = mqttClient_->publish(topic, mqtt_qos_, message->retain, message->payload.c_str(), message->payload.size(), false, mqtt_message.id_);
uint16_t packet_id = mqttClient_->publish(topic, mqtt_qos_, message->retain, message->payload.c_str());
lasttopic_ = topic;
lastpayload_ = message->payload;

View File

@@ -19,7 +19,7 @@
#ifndef EMSESP_MQTT_H_
#define EMSESP_MQTT_H_
#include <AsyncMqttClient.h>
#include <espMqttClient.h>
#include "helpers.h"
#include "system.h"
@@ -76,6 +76,7 @@ class Mqtt {
static constexpr uint8_t MQTT_TOPIC_MAX_SIZE = 128; // fixed, not a user setting anymore
static void on_connect();
static void on_disconnect(espMqttClientTypes::DisconnectReason reason);
static void subscribe(const uint8_t device_type, const std::string & topic, mqtt_sub_function_p cb);
static void subscribe(const std::string & topic);
@@ -129,7 +130,7 @@ class Mqtt {
#endif
}
static AsyncMqttClient * client() {
static espMqttClient * client() {
return mqttClient_;
}
@@ -266,7 +267,7 @@ class Mqtt {
private:
static uuid::log::Logger logger_;
static AsyncMqttClient * mqttClient_;
static espMqttClient * mqttClient_;
static uint32_t mqtt_message_id_;
static constexpr uint32_t MQTT_PUBLISH_WAIT = 100; // delay in ms between sending publishes, to account for large payloads
@@ -306,8 +307,6 @@ class Mqtt {
uint32_t last_publish_heartbeat_ = 0;
uint32_t last_publish_queue_ = 0;
bool first_connect_attempted_ = false;
static bool connecting_;
static bool initialized_;
static uint32_t mqtt_publish_fails_;