Skip to content

Commit

Permalink
update espMqttclient
Browse files Browse the repository at this point in the history
  • Loading branch information
technyon committed Mar 2, 2024
1 parent e92d0e9 commit 7219ddd
Show file tree
Hide file tree
Showing 36 changed files with 667 additions and 210 deletions.
2 changes: 1 addition & 1 deletion Config.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#define NUKI_HUB_VERSION "8.32"
#define NUKI_HUB_VERSION "8.33-pre-6"

#define GITHUB_LATEST_RELEASE_URL "https://github.com/technyon/nuki_hub/releases/latest"
#define GITHUB_LATEST_RELEASE_API_URL "https://api.github.com/repos/technyon/nuki_hub/releases/latest"
Expand Down
17 changes: 17 additions & 0 deletions lib/espMqttClient/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
set(COMPONENT_SRCDIRS
"src" "src/Packets" "src/Transport"
)

set(COMPONENT_ADD_INCLUDEDIRS
"src" "src/Packets" "src/Transport"
)

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

register_component()

target_compile_definitions(${COMPONENT_TARGET} PUBLIC -DESP32)
target_compile_options(${COMPONENT_TARGET} PRIVATE -fno-rtti)
5 changes: 3 additions & 2 deletions lib/espMqttClient/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ Aims to be a non-blocking, fully compliant MQTT 3.1.1 client.
- TCP and TCP/TLS using standard WiFiClient and WiFiClientSecure connections
- Virtually unlimited incoming and outgoing payload sizes
- Readable and understandable code
- Fully async clients available via [AsyncTCP](https://github.com/me-no-dev/AsyncTCP) or [ESPAsnycTCP](https://github.com/me-no-dev/ESPAsyncTCP) (no TLS supported)
- Fully async clients available via [AsyncTCP](https://github.com/esphome/AsyncTCP) or [ESPAsnycTCP](https://github.com/esphome/ESPAsyncTCP) (no TLS supported)
- Supported platforms:
- Espressif ESP8266 and ESP32 using the Arduino framework
- Espressif ESP32 using the ESP IDF, see [esp idf component](https://docs.espressif.com/projects/arduino-esp32/en/latest/esp-idf_component.html)
- Basic Linux compatibility*. This includes WSL on Windows

> Linux compatibility is mainly for automatic testing. It relies on a quick and dirty Arduino-style `Client` with a POSIX TCP client underneath and Arduino-style `IPAddress` class. These are lacking many features needed for proper Linux support.
> Linux compatibility is mainly for automatic testing. It relies on a quick and dirty Arduino-style `Client` with a POSIX TCP client underneath and Arduino-style `ClientPosixIPAddress` class. These are lacking many features needed for proper Linux support.
# Documentation

Expand Down
3 changes: 3 additions & 0 deletions lib/espMqttClient/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
28 changes: 21 additions & 7 deletions lib/espMqttClient/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
- TCP and TCP/TLS using standard WiFiClient and WiFiClientSecure connections
- Virtually unlimited incoming and outgoing payload sizes
- Readable and understandable code
- Fully async clients available via [AsyncTCP](https://github.com/me-no-dev/AsyncTCP) or [ESPAsnycTCP](https://github.com/me-no-dev/ESPAsyncTCP) (no TLS supported)
- Fully async clients available via [AsyncTCP](https://github.com/esphome/AsyncTCP) or [ESPAsnycTCP](https://github.com/esphome/ESPAsyncTCP) (no TLS supported)
- Supported platforms:
- Espressif ESP8266 and ESP32 using the Arduino framework
- Basic Linux compatibility*. This includes WSL on Windows
Expand Down Expand Up @@ -232,25 +232,25 @@ Add a publish acknowledged event handler. Function signature: `void(uint16_t pac
bool connected()
```

Returns if the client is currently fully connected to the broker or not. During connecting or disconnecting, it will return false.
Returns `true` if the client is currently fully connected to the broker. During connecting or disconnecting, it will return `false`.

```cpp
bool disconnected()
```

Returns if the client is currently disconnected to the broker or not. During disconnecting or connecting, it will return false.
Returns `true` if the client is currently disconnected from the broker. During disconnecting or connecting, it will return `false`.

```cpp
void connect()
bool connect()
```

Connect to the server.
Start the connect procedure. Returns `true` if successful. A positive return value doesn not mean the client is already connected.

```cpp
void disconnect(bool force = false)
bool disconnect(bool force = false)
```
Disconnect from the server.
Start the disconnect procedure, return `true` if successful. A positive return value doesn not mean the client is already disconnected.
When disconnecting with `force` false, the client first tries to handle all the outgoing messages in the queue and disconnect cleanly afterwards. During this time, no incoming PUBLISH messages are handled.
- **`force`**: Whether to force the disconnection. Defaults to `false` (clean disconnection).
Expand Down Expand Up @@ -341,10 +341,20 @@ const char* getClientId() const

Retuns the client ID.

```cpp
size_t queueSize();
```

Returns the amount of elements, regardless of type, in the queue.

# Compile time configuration

A number of constants which influence the behaviour of the client can be set at compile time. You can set these options in the `Config.h` file or pass the values as compiler flags. Because these options are compile-time constants, they are used for all instances of `espMqttClient` you create in your program.

### EMC_TX_TIMEOUT 10000

Timeout in milliseconds before a (qos > 0) message will be retransmitted.

### EMC_RX_BUFFER_SIZE 1440

The client copies incoming data into a buffer before parsing. This sets the buffer size.
Expand Down Expand Up @@ -388,6 +398,10 @@ The (maximum) length of the client ID. (Keep in mind that this is a c-string. Yo

Only used on ESP32. Sets the stack size (in words) of the MQTT client worker task.

### EMC_MULTIPLE_CALLBACKS

This macro is by default not enabled so you can add a single callbacks to an event. Assigning a second will overwrite the existing callback. When enabling multiple callbacks, multiple callbacks (with uint32_t id) can be assigned. Removing is done by referencing the id.

### EMC_USE_WATCHDOG 0

(ESP32 only)
Expand Down
148 changes: 148 additions & 0 deletions lib/espMqttClient/examples/notask-esp32/notask-esp32.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
#include <WiFi.h>

#include <espMqttClient.h>

#define WIFI_SSID "yourSSID"
#define WIFI_PASSWORD "yourpass"

#define MQTT_HOST IPAddress(192, 168, 1, 10)
#define MQTT_PORT 1883

espMqttClient mqttClient(espMqttClientTypes::UseInternalTask::NO);
bool reconnectMqtt = false;
uint32_t lastReconnect = 0;

void connectToWiFi() {
Serial.println("Connecting to Wi-Fi...");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
}

void connectToMqtt() {
Serial.println("Connecting to MQTT...");
if (!mqttClient.connect()) {
reconnectMqtt = true;
lastReconnect = millis();
Serial.println("Connecting failed.");
} else {
reconnectMqtt = false;
}
}

void WiFiEvent(WiFiEvent_t event) {
Serial.printf("[WiFi-event] event: %d\n", event);
switch(event) {
case SYSTEM_EVENT_STA_GOT_IP:
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
connectToMqtt();
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
Serial.println("WiFi lost connection");
break;
default:
break;
}
}

void onMqttConnect(bool sessionPresent) {
Serial.println("Connected to MQTT.");
Serial.print("Session present: ");
Serial.println(sessionPresent);
uint16_t packetIdSub = mqttClient.subscribe("foo/bar", 2);
Serial.print("Subscribing at QoS 2, packetId: ");
Serial.println(packetIdSub);
mqttClient.publish("foo/bar", 0, true, "test 1");
Serial.println("Publishing at QoS 0");
uint16_t packetIdPub1 = mqttClient.publish("foo/bar", 1, true, "test 2");
Serial.print("Publishing at QoS 1, packetId: ");
Serial.println(packetIdPub1);
uint16_t packetIdPub2 = mqttClient.publish("foo/bar", 2, true, "test 3");
Serial.print("Publishing at QoS 2, packetId: ");
Serial.println(packetIdPub2);
}

void onMqttDisconnect(espMqttClientTypes::DisconnectReason reason) {
Serial.printf("Disconnected from MQTT: %u.\n", static_cast<uint8_t>(reason));

if (WiFi.isConnected()) {
reconnectMqtt = true;
lastReconnect = millis();
}
}

void onMqttSubscribe(uint16_t packetId, const espMqttClientTypes::SubscribeReturncode* codes, size_t len) {
Serial.println("Subscribe acknowledged.");
Serial.print(" packetId: ");
Serial.println(packetId);
for (size_t i = 0; i < len; ++i) {
Serial.print(" qos: ");
Serial.println(static_cast<uint8_t>(codes[i]));
}
}

void onMqttUnsubscribe(uint16_t packetId) {
Serial.println("Unsubscribe acknowledged.");
Serial.print(" packetId: ");
Serial.println(packetId);
}

void onMqttMessage(const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total) {
(void) payload;
Serial.println("Publish received.");
Serial.print(" topic: ");
Serial.println(topic);
Serial.print(" qos: ");
Serial.println(properties.qos);
Serial.print(" dup: ");
Serial.println(properties.dup);
Serial.print(" retain: ");
Serial.println(properties.retain);
Serial.print(" len: ");
Serial.println(len);
Serial.print(" index: ");
Serial.println(index);
Serial.print(" total: ");
Serial.println(total);
}

void onMqttPublish(uint16_t packetId) {
Serial.println("Publish acknowledged.");
Serial.print(" packetId: ");
Serial.println(packetId);
}

void setup() {
Serial.begin(115200);
Serial.println();
Serial.println();

WiFi.setAutoConnect(false);
WiFi.setAutoReconnect(true);
WiFi.onEvent(WiFiEvent);

mqttClient.onConnect(onMqttConnect);
mqttClient.onDisconnect(onMqttDisconnect);
mqttClient.onSubscribe(onMqttSubscribe);
mqttClient.onUnsubscribe(onMqttUnsubscribe);
mqttClient.onMessage(onMqttMessage);
mqttClient.onPublish(onMqttPublish);
mqttClient.setServer(MQTT_HOST, MQTT_PORT);

connectToWiFi();
}

void loop() {
static uint32_t currentMillis = millis();

if (reconnectMqtt && currentMillis - lastReconnect > 5000) {
connectToMqtt();
}

// We used to option not to use the internal task
// so we need to call the loop-method ourselves.
// During connecting it may block.
// Creating a separate task yourself is obviously
// also a possibility.
mqttClient.loop();
}
8 changes: 8 additions & 0 deletions lib/espMqttClient/examples/simple-esp32-idf/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# The following lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)

SET(SDKCONFIG ${CMAKE_BINARY_DIR}/sdkconfig)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(simple-esp32-idf)
3 changes: 3 additions & 0 deletions lib/espMqttClient/examples/simple-esp32-idf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This example is for use with [Arduino as a component](https://espressif-docs.readthedocs-hosted.com/projects/arduino-esp32/en/latest/esp-idf_component.html) in the ESP-IDF framework.

Be sure to follow [this section](https://espressif-docs.readthedocs-hosted.com/projects/arduino-esp32/en/latest/esp-idf_component.html#adding-local-library) about adding libraries to your project.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
idf_component_register(
SRCS "main.cpp"
INCLUDE_DIRS "")
Loading

0 comments on commit 7219ddd

Please sign in to comment.