Skip to content

Commit

Permalink
Merge pull request #12 from cyijun/next
Browse files Browse the repository at this point in the history
Merge Next Branch to Main Branch
  • Loading branch information
cyijun authored Jul 8, 2024
2 parents 87924bf + d71230e commit c77716a
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 31 deletions.
17 changes: 17 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
set(COMPONENT_SRCDIRS
"src"
)

set(COMPONENT_ADD_INCLUDEDIRS
"src"
)

set(COMPONENT_REQUIRES
"arduino-esp32"
"mqtt"
)

register_component()

target_compile_definitions(${COMPONENT_TARGET} PUBLIC -DESP32)
target_compile_options(${COMPONENT_TARGET} PRIVATE -fno-rtti)
3 changes: 3 additions & 0 deletions component.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
COMPONENT_ADD_INCLUDEDIRS := src
COMPONENT_SRCDIRS := src
CXXFLAGS += -fno-rtti
11 changes: 5 additions & 6 deletions examples/HelloToMyself/HelloToMyself.ino
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void loop()
delay(2000);
}

void onConnectionEstablishedCallback(esp_mqtt_client_handle_t client)
void onMqttConnect(esp_mqtt_client_handle_t client)
{
if (mqttClient.isMyTurn(client)) // can be omitted if only one client
{
Expand All @@ -51,8 +51,7 @@ void onConnectionEstablishedCallback(esp_mqtt_client_handle_t client)
}
}

esp_err_t handleMQTT(esp_mqtt_event_handle_t event)
{
mqttClient.onEventCallback(event);
return ESP_OK;
}
void handleMQTT(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data){
auto *event = static_cast<esp_mqtt_event_handle_t>(event_data);
mqttClient.onEventCallback(event);
}
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=ESP32MQTTClient
version=0.3.0
version=1.0.0
author=Chen Yijun <cyjason2000@163.com>
maintainer=Chen Yijun <cyjason2000@163.com>
sentence=A thread-safe MQTT library based on the <mqtt_client.h> of ESP-IDF instead of PubSubClient.
Expand Down
48 changes: 26 additions & 22 deletions src/ESP32MQTTClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ ESP32MQTTClient::ESP32MQTTClient(/* args */)
_mqttConnected = false;
_mqttMaxInPacketSize = 1024;
_mqttMaxOutPacketSize = _mqttMaxInPacketSize;
_mqtt_config.event_handle = handleMQTT;
}

ESP32MQTTClient::~ESP32MQTTClient()
Expand Down Expand Up @@ -34,27 +33,27 @@ void ESP32MQTTClient::enableLastWillMessage(const char *topic, const char *messa

void ESP32MQTTClient::disableAutoReconnect()
{
_mqtt_config.disable_auto_reconnect = true;
_mqtt_config.network.disable_auto_reconnect = true;
}

void ESP32MQTTClient::setTaskPrio(int prio)
{
_mqtt_config.task_prio = prio;
_mqtt_config.task.priority = prio;
}

void ESP32MQTTClient::setClientCert(const char * clientCert)
{
_mqtt_config.client_cert_pem = clientCert;
_mqtt_config.credentials.authentication.certificate = clientCert;
}

void ESP32MQTTClient::setCaCert(const char * caCert)
{
_mqtt_config.cert_pem = caCert;
_mqtt_config.broker.verification.certificate = caCert;
}

void ESP32MQTTClient::setKey(const char * clientKey)
{
_mqtt_config.client_key_pem = clientKey;
_mqtt_config.credentials.authentication.key = clientKey;

}
// =============== Public functions for interaction with thus lib =================
Expand Down Expand Up @@ -188,7 +187,7 @@ bool ESP32MQTTClient::unsubscribe(const String &topic)

void ESP32MQTTClient::setKeepAlive(uint16_t keepAliveSeconds)
{
_mqtt_config.keepalive = keepAliveSeconds;
_mqtt_config.session.keepalive = keepAliveSeconds;
}

// ================== Private functions ====================-
Expand Down Expand Up @@ -272,20 +271,21 @@ bool ESP32MQTTClient::loopStart()

// explicitly set the server/port here in case they were not provided in the constructor

_mqtt_config.uri = _mqttUri;
_mqtt_config.client_id = _mqttClientName;
_mqtt_config.username = _mqttUsername;
_mqtt_config.password = _mqttPassword;
_mqtt_config.lwt_topic = _mqttLastWillTopic;
_mqtt_config.lwt_msg = _mqttLastWillMessage;
_mqtt_config.lwt_qos = _mqttLastWillQos;
_mqtt_config.lwt_retain = _mqttLastWillRetain;
_mqtt_config.lwt_msg_len = strlen(_mqttLastWillMessage);
_mqtt_config.disable_clean_session = _disableMQTTCleanSession;
_mqtt_config.out_buffer_size = _mqttMaxOutPacketSize;
_mqtt_config.buffer_size = _mqttMaxInPacketSize;
_mqtt_config.broker.address.uri = _mqttUri;
_mqtt_config.credentials.client_id = _mqttClientName;
_mqtt_config.credentials.username = _mqttUsername;
_mqtt_config.credentials.authentication.password = _mqttPassword;
_mqtt_config.session.last_will.topic = _mqttLastWillTopic;
_mqtt_config.session.last_will.msg = _mqttLastWillMessage;
_mqtt_config.session.last_will.qos = _mqttLastWillQos;
_mqtt_config.session.last_will.retain = _mqttLastWillRetain;
_mqtt_config.session.last_will.msg_len = strlen(_mqttLastWillMessage);
_mqtt_config.session.disable_clean_session = _disableMQTTCleanSession;
_mqtt_config.buffer.out_size = _mqttMaxOutPacketSize;
_mqtt_config.buffer.size = _mqttMaxInPacketSize;

_mqtt_client = esp_mqtt_client_init(&_mqtt_config);
esp_mqtt_client_register_event(_mqtt_client, MQTT_EVENT_ANY, handleMQTT, this);
esp_mqtt_client_start(_mqtt_client);
}
else
Expand Down Expand Up @@ -397,16 +397,20 @@ void ESP32MQTTClient::onEventCallback(esp_mqtt_event_handle_t event)
switch (event->event_id)
{
case MQTT_EVENT_CONNECTED:
log_i("onMqttConnect");
if (_enableSerialLogs)
log_i("MQTT -->> onMqttConnect");
setConnectionState(true);
onConnectionEstablishedCallback(_mqtt_client);
onMqttConnect(_mqtt_client);
break;
case MQTT_EVENT_DATA:
if (_enableSerialLogs)
log_i("MQTT -->> onMqttEventData");
onMessageReceivedCallback(String(event->topic).substring(0, event->topic_len).c_str(), event->data, event->data_len);
break;
case MQTT_EVENT_DISCONNECTED:
setConnectionState(false);
log_i("%s disconnected (%fs)", _mqttUri, millis() / 1000.0);
if (_enableSerialLogs)
log_i("MQTT -->> %s disconnected (%fs)", _mqttUri, millis() / 1000.0);
break;
case MQTT_EVENT_ERROR:
printError(event->error_handle);
Expand Down
25 changes: 23 additions & 2 deletions src/ESP32MQTTClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,18 @@
#include <Arduino.h>
#include <mqtt_client.h>

void onConnectionEstablishedCallback(esp_mqtt_client_handle_t client);
esp_err_t handleMQTT(esp_mqtt_event_handle_t event);
void onMqttConnect(esp_mqtt_client_handle_t client);
/*
* @brief Event handler registered to receive MQTT events
*
* This function is called by the MQTT client event loop.
*
* @param handler_args user data registered to the event.
* @param base Event base for the handler(always MQTT Base).
* @param event_id The id for the received event.
* @param event_data The data for the event, esp_mqtt_event_handle_t.
*/
void handleMQTT(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data);

typedef std::function<void(const String &message)> MessageReceivedCallback;
typedef std::function<void(const String &topicStr, const String &message)> MessageReceivedCallbackWithTopic;
Expand Down Expand Up @@ -85,6 +95,17 @@ class ESP32MQTTClient
_mqttPassword = password;
};

inline void setURL(const char *url, const uint16_t port, const char *username = "", const char *password = "")
{ // Allow setting the MQTT info manually (must be done in setup())
char *uri=(char *)malloc(200);
sprintf(uri,"mqtt://%s:%u", url, port);
if (_enableSerialLogs)
log_i("MQTT uri %s\n", uri);
_mqttUri = uri;
_mqttUsername = username;
_mqttPassword = password;
};

inline bool isConnected() const { return _mqttConnected; };
inline bool isMyTurn(esp_mqtt_client_handle_t client) const { return _mqtt_client==client; }; // Return true if mqtt is connected

Expand Down

0 comments on commit c77716a

Please sign in to comment.