diff --git a/README.md b/README.md index 105f3c4..17bab4e 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,16 @@ ![image](https://github.com/cziter15/ksIotFrameworkLib/assets/5003708/a17e4fe9-144c-4422-be40-90e0f402b054) +> [!IMPORTANT] +> This library supports Arduino 3.0.1 on ESP32. However, due to [Platformio statement](https://github.com/platformio/platform-espressif32/issues/1225) it will not automatically pull latest versions. +> If you want 3.0.1 version support, set your platform to my fork in your `platformio.ini` file. +> +> ```platform = https://github.com/cziter15/platform-espressif32.git``` + +> [!IMPORTANT] +> When it comes to ESP8266, latest version supported is based on SDK305. To use it, add `-DPIO_FRAMEWORK_ARDUINO_ESPRESSIF_SDK305` to your build flags. + + ## 🌱 Motivation - The goal of this project is to create a simple template or starting point to develop IoT applications using Espressif microcontrollers. diff --git a/src/ksf/comp/ksMqttConnector.cpp b/src/ksf/comp/ksMqttConnector.cpp index d709d8b..5632c0e 100644 --- a/src/ksf/comp/ksMqttConnector.cpp +++ b/src/ksf/comp/ksMqttConnector.cpp @@ -8,17 +8,31 @@ */ #if ESP32 - #include #include + #include + #if ESP_ARDUINO_VERSION_MAJOR >= 3 + #define NET_CLIENT_CLASS NetworkClient + #define NET_CLIENT_SECURE_CLASS NetworkClientSecure + #include + #include + #else + #define NET_CLIENT_CLASS WiFiClient + #define NET_CLIENT_SECURE_CLASS WiFiClientSecure + #include + #include + #endif #elif ESP8266 #include #include + #include + #define NET_CLIENT_CLASS WiFiClient + #define NET_CLIENT_SECURE_CLASS WiFiClientSecure #else #error Platform not implemented. #endif #include -#include + #include "../ksApplication.h" #include "../ksConstants.h" #include "../ksConfig.h" @@ -65,22 +79,22 @@ namespace ksf::comps { if (!fingerprint.empty()) { - auto secureClient{std::make_unique()}; + auto secureClient{std::make_unique()}; certFingerprint = std::make_unique(); if (certFingerprint->setup(secureClient.get(), fingerprint)) - wifiClientUq = std::move(secureClient); + netClientUq = std::move(secureClient); } else { - wifiClientUq = std::make_unique(); + netClientUq = std::make_unique(); } /* Whoops, it looks like fingerprint validation failed. */ - if (!wifiClientUq) + if (!netClientUq) return; - mqttClientUq = std::make_unique(*wifiClientUq.get()); + mqttClientUq = std::make_unique(*netClientUq.get()); this->login = std::move(login); this->password = std::move(password); @@ -104,9 +118,9 @@ namespace ksf::comps */ #if ESP32 - wifiClientUq->setTimeout(KSF_MQTT_TIMEOUT_SEC); + netClientUq->setTimeout(KSF_MQTT_TIMEOUT_SEC); #elif ESP8266 - wifiClientUq->setTimeout(KSF_SEC_TO_MS(KSF_MQTT_TIMEOUT_SEC)); + netClientUq->setTimeout(KSF_SEC_TO_MS(KSF_MQTT_TIMEOUT_SEC)); #else #error Platform not implemented. #endif @@ -191,23 +205,23 @@ namespace ksf::comps #endif /* Handle connection manually. */ if (IPAddress serverIP; serverIP.fromString(this->broker.c_str())) - wifiClientUq->connect(serverIP, portNumber); + netClientUq->connect(serverIP, portNumber); else - wifiClientUq->connect(this->broker.c_str(), portNumber); + netClientUq->connect(this->broker.c_str(), portNumber); /* If not connected, return. */ - if (!wifiClientUq->connected()) + if (!netClientUq->connected()) return false; /* Verify certificate fingerprint. */ - if (certFingerprint && !certFingerprint->verify(reinterpret_cast(wifiClientUq.get()))) + if (certFingerprint && !certFingerprint->verify(reinterpret_cast(netClientUq.get()))) { #ifdef APP_LOG_ENABLED app->log([&](std::string& out) { out += PSTR("[MQTT] Invalid certificate fingerprint! Disconnecting."); }); #endif - wifiClientUq->stop(); + netClientUq->stop(); return false; } @@ -217,9 +231,9 @@ namespace ksf::comps #ifdef APP_LOG_ENABLED app->log([&](std::string& out) { out += PSTR("[MQTT] Connected successfully to "); - out += wifiClientUq->remoteIP().toString().c_str(); + out += broker; out += PSTR(" on port "); - out += ksf::to_string(wifiClientUq->remotePort()); + out += std::to_string(portNumber); }); #endif diff --git a/src/ksf/comp/ksMqttConnector.h b/src/ksf/comp/ksMqttConnector.h index 8993da1..c7a9e29 100644 --- a/src/ksf/comp/ksMqttConnector.h +++ b/src/ksf/comp/ksMqttConnector.h @@ -16,7 +16,7 @@ #include "../evt/ksEvent.h" #include "../ksSimpleTimer.h" -class WiFiClient; +class Client; class PubSubClient; class ksCertFingerprint; @@ -41,7 +41,7 @@ namespace ksf #if APP_LOG_ENABLED ksApplication* app{nullptr}; //!< Application pointer. #endif - std::unique_ptr wifiClientUq; //!< Shared pointer to WiFiClient used to connect to MQTT. + std::unique_ptr netClientUq; //!< Shared pointer to WiFiClient used to connect to MQTT. std::unique_ptr mqttClientUq; //!< Shared pointer to PubSubClient used to connect to MQTT. std::weak_ptr wifiConnWp; //!< Weak pointer to WiFi connector. diff --git a/src/ksf/misc/ksWSServer.h b/src/ksf/misc/ksWSServer.h index c6575ab..1f0a418 100644 --- a/src/ksf/misc/ksWSServer.h +++ b/src/ksf/misc/ksWSServer.h @@ -10,7 +10,6 @@ #pragma once #include -#include #include #include @@ -29,7 +28,7 @@ namespace ksf::misc class ksWSServer : public WebSocketsServerCore { protected: - std::unique_ptr wsListener; //!< WS server (listener). + std::unique_ptr wsListener; //!< WS server (listener). uint64_t requriedAuthToken{0}; //!< WS auth token. ksWsServerMessageFunc_t onWebsocketTextMessage; //!< Callback function to receive messages.