Files
arduino/src/MqttClient.cpp

169 lines
4.1 KiB
C++
Raw Normal View History

#ifdef ESP32
2024-04-03 07:04:55 +07:00
#include "MqttClient.h"
static void __mqtt_event_handler(void *handler_args, esp_event_base_t base,
2024-04-03 21:26:04 +07:00
int32_t event_id, void *event_data);
2024-04-03 07:04:55 +07:00
2024-04-03 21:26:04 +07:00
MqttClient::MqttClient(Stream &debugLog) : PrintLog(debugLog, "MqttClient") {}
2024-04-03 07:04:55 +07:00
MqttClient::~MqttClient() {}
bool MqttClient::begin(String uri) {
if (isBegin) {
2024-04-03 21:26:04 +07:00
logInfo("Already begin, calll 'end' and try again");
2024-04-03 07:04:55 +07:00
return true;
}
if (uri.isEmpty()) {
Serial.println("Mqtt uri is empty");
return false;
}
this->uri = uri;
2024-04-03 21:26:04 +07:00
logInfo("Init uri: " + uri);
2024-04-03 07:04:55 +07:00
/** config esp_mqtt client */
esp_mqtt_client_config_t config = {
.uri = this->uri.c_str(),
};
/** init client */
client = esp_mqtt_client_init(&config);
if (client == NULL) {
2024-04-03 21:26:04 +07:00
logError("Init client failed");
2024-04-03 07:04:55 +07:00
return false;
}
/** Register event */
2024-04-03 21:26:04 +07:00
if (esp_mqtt_client_register_event(client, MQTT_EVENT_ANY,
__mqtt_event_handler, this) != ESP_OK) {
logError("Register event failed");
2024-04-03 07:04:55 +07:00
return false;
}
if (esp_mqtt_client_start(client) != ESP_OK) {
2024-04-03 21:26:04 +07:00
logError("Client start failed");
2024-04-03 07:04:55 +07:00
return false;
}
isBegin = true;
connectionFailedCount = 0;
return true;
}
void MqttClient::end(void) {
if (!isBegin) {
2024-04-03 21:26:04 +07:00
logWarning("Already end, call 'begin' and try again");
2024-04-03 07:04:55 +07:00
return;
}
esp_mqtt_client_disconnect(client);
esp_mqtt_client_stop(client);
esp_mqtt_client_destroy(client);
client = NULL;
isBegin = false;
logInfo("end");
2024-04-03 07:04:55 +07:00
}
void MqttClient::_updateConnected(bool connected) {
this->connected = connected;
if (connected) {
connectionFailedCount = 0;
} else {
connectionFailedCount++;
2024-04-03 21:26:04 +07:00
logWarning("Connection failed count " + String(connectionFailedCount));
2024-04-03 07:04:55 +07:00
}
}
bool MqttClient::publish(const char *topic, const char *payload, int len) {
2024-04-03 07:04:55 +07:00
if (!isBegin) {
2024-04-03 21:26:04 +07:00
logError("No-initialized");
2024-04-03 07:04:55 +07:00
return false;
}
if (!connected) {
2024-04-03 21:26:04 +07:00
logError("Client disconnected");
2024-04-03 07:04:55 +07:00
return false;
}
if (esp_mqtt_client_publish(client, topic, payload, len, 0, 0) == ESP_OK) {
logInfo("Publish success");
2024-04-03 07:04:55 +07:00
return true;
}
2024-04-03 21:26:04 +07:00
logError("Publish failed");
2024-04-03 07:04:55 +07:00
return false;
}
/**
* @brief Check that URI is same as current initialized URI
*
* @param uri Target URI
* @return true Same
* @return false Difference
*/
bool MqttClient::isCurrentUri(String &uri) {
if (this->uri == uri) {
return true;
}
return false;
}
/**
* @brief Get MQTT client connected status
*
* @return true Connected
* @return false Disconnected
*/
bool MqttClient::isConnected(void) { return connected; }
/**
* @brief Get number of connection failed
*
* @return int
*/
int MqttClient::getConnectionFailedCount(void) { return connectionFailedCount; }
static void __mqtt_event_handler(void *handler_args, esp_event_base_t base,
2024-04-03 21:26:04 +07:00
int32_t event_id, void *event_data) {
2024-04-03 07:04:55 +07:00
MqttClient *mqtt = (MqttClient *)handler_args;
esp_mqtt_event_handle_t event = (esp_mqtt_event_handle_t)event_data;
esp_mqtt_client_handle_t client = event->client;
int msg_id;
switch ((esp_mqtt_event_id_t)event_id) {
case MQTT_EVENT_CONNECTED:
2024-04-03 21:26:04 +07:00
mqtt->logInfo("MQTT_EVENT_CONNECTED");
2024-04-03 07:04:55 +07:00
mqtt->_updateConnected(true);
break;
case MQTT_EVENT_DISCONNECTED:
2024-04-03 21:26:04 +07:00
mqtt->logInfo("MQTT_EVENT_DISCONNECTED");
2024-04-03 07:04:55 +07:00
mqtt->_updateConnected(false);
break;
case MQTT_EVENT_SUBSCRIBED:
break;
case MQTT_EVENT_UNSUBSCRIBED:
break;
case MQTT_EVENT_PUBLISHED:
break;
case MQTT_EVENT_DATA:
break;
case MQTT_EVENT_ERROR:
Serial.println("MQTT_EVENT_ERROR");
if (event->error_handle->error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT) {
2024-04-03 21:26:04 +07:00
mqtt->logError("Reported from esp-tls: " +
String(event->error_handle->esp_tls_last_esp_err));
mqtt->logError("Reported from tls stack: " +
String(event->error_handle->esp_tls_stack_err));
mqtt->logError("Captured as transport's socket errno: " +
String(event->error_handle->esp_transport_sock_errno));
2024-04-03 07:04:55 +07:00
}
break;
default:
Serial.printf("Other event id:%d\r\n", event->event_id);
break;
}
}
#endif /** ESP32 */