diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..5b0a9d8 --- /dev/null +++ b/CMakeLists.txt @@ -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) diff --git a/component.mk b/component.mk new file mode 100644 index 0000000..bb5bb16 --- /dev/null +++ b/component.mk @@ -0,0 +1,3 @@ +COMPONENT_ADD_INCLUDEDIRS := src +COMPONENT_SRCDIRS := src +CXXFLAGS += -fno-rtti diff --git a/examples/HelloToMyself/HelloToMyself.ino b/examples/HelloToMyself/HelloToMyself.ino index d2c2196..ad21e42 100644 --- a/examples/HelloToMyself/HelloToMyself.ino +++ b/examples/HelloToMyself/HelloToMyself.ino @@ -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 { @@ -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(event_data); + mqttClient.onEventCallback(event); +} \ No newline at end of file diff --git a/library.properties b/library.properties index 55de263..96d221a 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=ESP32MQTTClient -version=0.3.0 +version=1.0.0 author=Chen Yijun maintainer=Chen Yijun sentence=A thread-safe MQTT library based on the of ESP-IDF instead of PubSubClient. diff --git a/src/ESP32MQTTClient.cpp b/src/ESP32MQTTClient.cpp index fdd8bff..414517f 100644 --- a/src/ESP32MQTTClient.cpp +++ b/src/ESP32MQTTClient.cpp @@ -5,7 +5,6 @@ ESP32MQTTClient::ESP32MQTTClient(/* args */) _mqttConnected = false; _mqttMaxInPacketSize = 1024; _mqttMaxOutPacketSize = _mqttMaxInPacketSize; - _mqtt_config.event_handle = handleMQTT; } ESP32MQTTClient::~ESP32MQTTClient() @@ -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 ================= @@ -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 ====================- @@ -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 @@ -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); diff --git a/src/ESP32MQTTClient.h b/src/ESP32MQTTClient.h index f6779e4..c886c9a 100644 --- a/src/ESP32MQTTClient.h +++ b/src/ESP32MQTTClient.h @@ -4,8 +4,18 @@ #include #include -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 MessageReceivedCallback; typedef std::function MessageReceivedCallbackWithTopic; @@ -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