From 5ce570d770e9643e0f2d4a28f2f7082bb326883d Mon Sep 17 00:00:00 2001 From: Wolfram Pienkoss Date: Fri, 31 Oct 2025 01:24:33 +0100 Subject: [PATCH 1/9] "WiFi" replaced by "Network". --- src/Transport/{ClientAsync.cpp => ClientAsync.cpp.ignore} | 0 src/Transport/{ClientAsync.h => ClientAsync.h.ignore} | 0 src/Transport/ClientSecureSync.h | 4 ++-- src/Transport/ClientSync.h | 4 ++-- src/{espMqttClientAsync.cpp => espMqttClientAsync.cpp.ignore} | 0 src/{espMqttClientAsync.h => espMqttClientAsync.h.ignore} | 0 6 files changed, 4 insertions(+), 4 deletions(-) rename src/Transport/{ClientAsync.cpp => ClientAsync.cpp.ignore} (100%) rename src/Transport/{ClientAsync.h => ClientAsync.h.ignore} (100%) rename src/{espMqttClientAsync.cpp => espMqttClientAsync.cpp.ignore} (100%) rename src/{espMqttClientAsync.h => espMqttClientAsync.h.ignore} (100%) diff --git a/src/Transport/ClientAsync.cpp b/src/Transport/ClientAsync.cpp.ignore similarity index 100% rename from src/Transport/ClientAsync.cpp rename to src/Transport/ClientAsync.cpp.ignore diff --git a/src/Transport/ClientAsync.h b/src/Transport/ClientAsync.h.ignore similarity index 100% rename from src/Transport/ClientAsync.h rename to src/Transport/ClientAsync.h.ignore diff --git a/src/Transport/ClientSecureSync.h b/src/Transport/ClientSecureSync.h index b81681e..d4b0e06 100644 --- a/src/Transport/ClientSecureSync.h +++ b/src/Transport/ClientSecureSync.h @@ -10,7 +10,7 @@ the LICENSE file. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) -#include // includes IPAddress +#include // includes IPAddress #include "Transport.h" @@ -26,7 +26,7 @@ class ClientSecureSync : public Transport { void stop() override; bool connected() override; bool disconnected() override; - WiFiClientSecure client; + NetworkClientSecure client; }; } // namespace espMqttClientInternals diff --git a/src/Transport/ClientSync.h b/src/Transport/ClientSync.h index ccfbdba..373a092 100644 --- a/src/Transport/ClientSync.h +++ b/src/Transport/ClientSync.h @@ -10,7 +10,7 @@ the LICENSE file. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) -#include // includes IPAddress +#include // includes IPAddress #include "Transport.h" @@ -26,7 +26,7 @@ class ClientSync : public Transport { void stop() override; bool connected() override; bool disconnected() override; - WiFiClient client; + NetworkClient client; }; } // namespace espMqttClientInternals diff --git a/src/espMqttClientAsync.cpp b/src/espMqttClientAsync.cpp.ignore similarity index 100% rename from src/espMqttClientAsync.cpp rename to src/espMqttClientAsync.cpp.ignore diff --git a/src/espMqttClientAsync.h b/src/espMqttClientAsync.h.ignore similarity index 100% rename from src/espMqttClientAsync.h rename to src/espMqttClientAsync.h.ignore From e47b45d60166ac92ef20cba638d81fbae5fbbc44 Mon Sep 17 00:00:00 2001 From: Wolfram Pienkoss Date: Fri, 31 Oct 2025 02:30:29 +0100 Subject: [PATCH 2/9] Example with MQTT-PSK and Ethernet interface added. --- .../simple-esp32-ETH/simple-esp32-ETH.ino | 176 ++++++++++++++++++ 1 file changed, 176 insertions(+) create mode 100644 examples/simple-esp32-ETH/simple-esp32-ETH.ino diff --git a/examples/simple-esp32-ETH/simple-esp32-ETH.ino b/examples/simple-esp32-ETH/simple-esp32-ETH.ino new file mode 100644 index 0000000..0be1d55 --- /dev/null +++ b/examples/simple-esp32-ETH/simple-esp32-ETH.ino @@ -0,0 +1,176 @@ + + +#include +#include + +#include + +#include + +#define MQTT_HOST IPAddress(192, 168, 1, 10) +#define MQTT_PORT 8883 + +#define MQTT_PSK_ID "mqttpskid" +#define MQTT_PSK "70736B70736B70736B" + + +espMqttClientSecure mqttClient; +bool reconnectMqtt = false; +uint32_t lastReconnect = 0; + +const uint8_t + pinCsETH = GPIO_NUM_14, // Chip Select LAN-Interface + pinClkSCK = GPIO_NUM_18, // Serial Clock + pinMISO = GPIO_NUM_19, // MISO + pinMOSI = GPIO_NUM_23, // MOSI + pinIntETH = GPIO_NUM_15, // Interrupt LAN-Interface + pinRstETH = GPIO_NUM_27; // Reset LAN-Interface + +void connectToNetwork() { + Serial.println("Connecting to Network..."); + + SPI.begin(pinClkSCK, pinMISO, pinMOSI); + ETH.begin(ETH_PHY_W5500, 1, pinCsETH, -1, pinRstETH, SPI); // without IRQ +} + +void connectToMqtt() { + Serial.println("Connecting to MQTT..."); + if (!mqttClient.connect()) { + reconnectMqtt = true; + lastReconnect = millis(); + Serial.println("Connecting failed."); + } else { + reconnectMqtt = false; + } +} + +static bool eth_connected = false; + +void onEvent(arduino_event_id_t event, arduino_event_info_t info) { + switch (event) { + case ARDUINO_EVENT_ETH_START: + Serial.println("ETH Started"); + //set eth hostname here + ETH.setHostname("esp32-eth0"); + break; + case ARDUINO_EVENT_ETH_CONNECTED: Serial.println("ETH Connected"); break; + case ARDUINO_EVENT_ETH_GOT_IP: Serial.printf("ETH Got IP: '%s'\n", esp_netif_get_desc(info.got_ip.esp_netif)); Serial.println(ETH); +#if USE_TWO_ETH_PORTS + Serial.println(ETH1); +#endif + eth_connected = true; + connectToMqtt(); + break; + case ARDUINO_EVENT_ETH_LOST_IP: + Serial.println("ETH Lost IP"); + eth_connected = false; + break; + case ARDUINO_EVENT_ETH_DISCONNECTED: + Serial.println("ETH Disconnected"); + eth_connected = false; + break; + case ARDUINO_EVENT_ETH_STOP: + Serial.println("ETH Stopped"); + eth_connected = false; + 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(reason)); + + if (eth_connected) { + 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(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); + String pl = String((const char*)payload).substring(0, len); + Serial.print(" payload: "); + Serial.println(pl); + 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(); + + Network.onEvent(onEvent); + + mqttClient.onConnect(onMqttConnect); + mqttClient.onDisconnect(onMqttDisconnect); + mqttClient.onSubscribe(onMqttSubscribe); + mqttClient.onUnsubscribe(onMqttUnsubscribe); + mqttClient.onMessage(onMqttMessage); + mqttClient.onPublish(onMqttPublish); + mqttClient.setServer(MQTT_HOST, MQTT_PORT); + mqttClient.setPreSharedKey(MQTT_PSK_ID, MQTT_PSK); + + connectToNetwork(); +} + +void loop() { + static uint32_t currentMillis = millis(); + + if (reconnectMqtt && currentMillis - lastReconnect > 5000) { + Serial.println("connectToMqtt()"); + connectToMqtt(); + } +} From f94194ff6563a5db68c060fdaf6ad44f68b7bffd Mon Sep 17 00:00:00 2001 From: Wolfram Pienkoss Date: Fri, 9 Jan 2026 00:00:20 +0100 Subject: [PATCH 3/9] Some files reset to their original names. --- src/Transport/{ClientAsync.cpp.ignore => ClientAsync.cpp} | 0 src/Transport/{ClientAsync.h.ignore => ClientAsync.h} | 0 src/{espMqttClientAsync.cpp.ignore => espMqttClientAsync.cpp} | 0 src/{espMqttClientAsync.h.ignore => espMqttClientAsync.h} | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename src/Transport/{ClientAsync.cpp.ignore => ClientAsync.cpp} (100%) rename src/Transport/{ClientAsync.h.ignore => ClientAsync.h} (100%) rename src/{espMqttClientAsync.cpp.ignore => espMqttClientAsync.cpp} (100%) rename src/{espMqttClientAsync.h.ignore => espMqttClientAsync.h} (100%) diff --git a/src/Transport/ClientAsync.cpp.ignore b/src/Transport/ClientAsync.cpp similarity index 100% rename from src/Transport/ClientAsync.cpp.ignore rename to src/Transport/ClientAsync.cpp diff --git a/src/Transport/ClientAsync.h.ignore b/src/Transport/ClientAsync.h similarity index 100% rename from src/Transport/ClientAsync.h.ignore rename to src/Transport/ClientAsync.h diff --git a/src/espMqttClientAsync.cpp.ignore b/src/espMqttClientAsync.cpp similarity index 100% rename from src/espMqttClientAsync.cpp.ignore rename to src/espMqttClientAsync.cpp diff --git a/src/espMqttClientAsync.h.ignore b/src/espMqttClientAsync.h similarity index 100% rename from src/espMqttClientAsync.h.ignore rename to src/espMqttClientAsync.h From 5739892ff1bbb81ec66f5a3dd704ca0d4e00d278 Mon Sep 17 00:00:00 2001 From: Wolfram Pienkoss Date: Fri, 9 Jan 2026 01:54:46 +0100 Subject: [PATCH 4/9] Removes warning: 'void AsyncClient::close(bool)' is deprecated: Use AsyncClient::close() instead [-Wdeprecated-declarations]. --- src/Transport/ClientAsync.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Transport/ClientAsync.cpp b/src/Transport/ClientAsync.cpp index 4f8d69e..0ca9156 100644 --- a/src/Transport/ClientAsync.cpp +++ b/src/Transport/ClientAsync.cpp @@ -42,7 +42,7 @@ int ClientAsync::read(uint8_t* buf, size_t size) { } void ClientAsync::stop() { - client.close(false); + client.close(); } bool ClientAsync::connected() { From 92e76551429388a7cae6db140529914915fb9792 Mon Sep 17 00:00:00 2001 From: Wolfram Pienkoss Date: Fri, 9 Jan 2026 11:31:59 +0100 Subject: [PATCH 5/9] Warning in ClientAsync.cpp: Change limited to ESP32 and AsyncTCP versions >= 3.4.10. --- src/Transport/ClientAsync.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Transport/ClientAsync.cpp b/src/Transport/ClientAsync.cpp index 0ca9156..d9362ba 100644 --- a/src/Transport/ClientAsync.cpp +++ b/src/Transport/ClientAsync.cpp @@ -42,7 +42,11 @@ int ClientAsync::read(uint8_t* buf, size_t size) { } void ClientAsync::stop() { +#if defined(ARDUINO_ARCH_ESP32) && ASYNCTCP_VERSION_NUM >= ASYNCTCP_VERSION_VAL(3, 4, 10) client.close(); +#else + client.close(false); +#endif } bool ClientAsync::connected() { From 688b7d8c77dd2cb3ceaaaa592f3aafdf37be72b6 Mon Sep 17 00:00:00 2001 From: Wolfram Pienkoss Date: Fri, 9 Jan 2026 13:37:17 +0100 Subject: [PATCH 6/9] Network stack availability check added. --- examples/simple-esp32-ETH/simple-esp32-ETH.ino | 3 +++ src/Transport/ClientSecureSync.h | 10 +++++++++- src/Transport/ClientSync.h | 10 +++++++++- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/examples/simple-esp32-ETH/simple-esp32-ETH.ino b/examples/simple-esp32-ETH/simple-esp32-ETH.ino index 0be1d55..4867942 100644 --- a/examples/simple-esp32-ETH/simple-esp32-ETH.ino +++ b/examples/simple-esp32-ETH/simple-esp32-ETH.ino @@ -1,4 +1,7 @@ +#if __has_include() == 0 +# error "Network/Ethernet stack not available." +#endif #include #include diff --git a/src/Transport/ClientSecureSync.h b/src/Transport/ClientSecureSync.h index d4b0e06..14f327a 100644 --- a/src/Transport/ClientSecureSync.h +++ b/src/Transport/ClientSecureSync.h @@ -10,7 +10,11 @@ the LICENSE file. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) -#include // includes IPAddress +#if __has_include() +# include // includes IPAddress +#else +# include // includes IPAddress +#endif #include "Transport.h" @@ -26,7 +30,11 @@ class ClientSecureSync : public Transport { void stop() override; bool connected() override; bool disconnected() override; +# if __has_include() NetworkClientSecure client; +# else + WiFiClientSecure client; +# endif }; } // namespace espMqttClientInternals diff --git a/src/Transport/ClientSync.h b/src/Transport/ClientSync.h index 373a092..f8256fc 100644 --- a/src/Transport/ClientSync.h +++ b/src/Transport/ClientSync.h @@ -10,7 +10,11 @@ the LICENSE file. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) -#include // includes IPAddress +#if __has_include() +# include // includes IPAddress +#else +# include // includes IPAddress +#endif #include "Transport.h" @@ -26,7 +30,11 @@ class ClientSync : public Transport { void stop() override; bool connected() override; bool disconnected() override; +# if __has_include() NetworkClient client; +# else + WiFiClient client; +# endif }; } // namespace espMqttClientInternals From e792e396f027d5639bab1386e7ac6e8f0b8f9b72 Mon Sep 17 00:00:00 2001 From: Wolfram Pienkoss Date: Fri, 9 Jan 2026 14:10:06 +0100 Subject: [PATCH 7/9] Adjustment of version detection in ClientAsync.cpp for older compilers. --- src/Transport/ClientAsync.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Transport/ClientAsync.cpp b/src/Transport/ClientAsync.cpp index d9362ba..aeb95fe 100644 --- a/src/Transport/ClientAsync.cpp +++ b/src/Transport/ClientAsync.cpp @@ -42,11 +42,13 @@ int ClientAsync::read(uint8_t* buf, size_t size) { } void ClientAsync::stop() { -#if defined(ARDUINO_ARCH_ESP32) && ASYNCTCP_VERSION_NUM >= ASYNCTCP_VERSION_VAL(3, 4, 10) - client.close(); -#else - client.close(false); -#endif +#if defined(ARDUINO_ARCH_ESP32) +# if ASYNCTCP_VERSION_NUM >= ((3 << 16) | (4 << 8) | 10) + client.close(); +# else + client.close(false); +# endif // ASYNCTCP_VERSION_NUM >= ((3 << 16) | (4 << 8) | 10) +#endif // defined(ARDUINO_ARCH_ESP32) } bool ClientAsync::connected() { From 638bea72413c09052e95d0b1d45e43669efc1199 Mon Sep 17 00:00:00 2001 From: Wolfram Pienkoss Date: Fri, 9 Jan 2026 17:30:03 +0100 Subject: [PATCH 8/9] cosmetics --- src/Transport/ClientAsync.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Transport/ClientAsync.cpp b/src/Transport/ClientAsync.cpp index aeb95fe..7a3833a 100644 --- a/src/Transport/ClientAsync.cpp +++ b/src/Transport/ClientAsync.cpp @@ -44,9 +44,9 @@ int ClientAsync::read(uint8_t* buf, size_t size) { void ClientAsync::stop() { #if defined(ARDUINO_ARCH_ESP32) # if ASYNCTCP_VERSION_NUM >= ((3 << 16) | (4 << 8) | 10) - client.close(); + client.close(); # else - client.close(false); + client.close(false); # endif // ASYNCTCP_VERSION_NUM >= ((3 << 16) | (4 << 8) | 10) #endif // defined(ARDUINO_ARCH_ESP32) } From 7654646bbe2d898963a8af3f5a444e2a6ffa7a56 Mon Sep 17 00:00:00 2001 From: Wolfram Pienkoss Date: Fri, 9 Jan 2026 21:37:47 +0100 Subject: [PATCH 9/9] Linting errors removed. --- src/Transport/ClientAsync.cpp | 4 ++-- src/Transport/ClientSecureSync.h | 4 ++-- src/Transport/ClientSync.h | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Transport/ClientAsync.cpp b/src/Transport/ClientAsync.cpp index 7a3833a..c757f84 100644 --- a/src/Transport/ClientAsync.cpp +++ b/src/Transport/ClientAsync.cpp @@ -47,8 +47,8 @@ void ClientAsync::stop() { client.close(); # else client.close(false); -# endif // ASYNCTCP_VERSION_NUM >= ((3 << 16) | (4 << 8) | 10) -#endif // defined(ARDUINO_ARCH_ESP32) +# endif // ASYNCTCP_VERSION_NUM >= ((3 << 16) | (4 << 8) | 10) +#endif // defined(ARDUINO_ARCH_ESP32) } bool ClientAsync::connected() { diff --git a/src/Transport/ClientSecureSync.h b/src/Transport/ClientSecureSync.h index 14f327a..a3f2cda 100644 --- a/src/Transport/ClientSecureSync.h +++ b/src/Transport/ClientSecureSync.h @@ -11,9 +11,9 @@ the LICENSE file. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) #if __has_include() -# include // includes IPAddress +# include // includes IPAddress #else -# include // includes IPAddress +# include // includes IPAddress #endif #include "Transport.h" diff --git a/src/Transport/ClientSync.h b/src/Transport/ClientSync.h index f8256fc..064b370 100644 --- a/src/Transport/ClientSync.h +++ b/src/Transport/ClientSync.h @@ -11,9 +11,9 @@ the LICENSE file. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) #if __has_include() -# include // includes IPAddress +# include // includes IPAddress #else -# include // includes IPAddress +# include // includes IPAddress #endif #include "Transport.h"