From 3b6713ede97f7e4bfac449552d3b9e464080c0d2 Mon Sep 17 00:00:00 2001 From: pennam Date: Thu, 5 Jun 2025 09:48:04 +0200 Subject: [PATCH 1/9] TCP: make begin(bool, String uint16_t, bool) private --- src/ArduinoIoTCloudTCP.cpp | 118 ++++++++++++++++++------------------- src/ArduinoIoTCloudTCP.h | 4 +- 2 files changed, 62 insertions(+), 60 deletions(-) diff --git a/src/ArduinoIoTCloudTCP.cpp b/src/ArduinoIoTCloudTCP.cpp index 31e97288..867efd2e 100644 --- a/src/ArduinoIoTCloudTCP.cpp +++ b/src/ArduinoIoTCloudTCP.cpp @@ -139,65 +139,6 @@ int ArduinoIoTCloudTCP::begin(ConnectionHandler & connection, bool const enable_ return begin(enable_watchdog, _brokerAddress, _brokerPort, auto_reconnect); } -int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress, uint16_t brokerPort, bool auto_reconnect) -{ - _enable_watchdog = enable_watchdog; - _brokerAddress = brokerAddress; - _brokerPort = brokerPort; - _auto_reconnect = auto_reconnect; - - _state = State::ConfigPhy; - - _mqttClient.setClient(_brokerClient); - -#ifdef BOARD_HAS_SECRET_KEY - if(_password.length()) - { - _mqttClient.setUsernamePassword(getDeviceId(), _password); - } -#endif - - _mqttClient.onMessage(ArduinoIoTCloudTCP::onMessage); - _mqttClient.setKeepAliveInterval(30 * 1000); - _mqttClient.setConnectionTimeout(1500); - _mqttClient.setId(getDeviceId().c_str()); - - _messageTopicOut = getTopic_messageout(); - _messageTopicIn = getTopic_messagein(); - - _thing.begin(); - _device.begin(); - -#if OTA_ENABLED && !defined(OFFLOADED_DOWNLOAD) - _ota.setClient(&_otaClient); -#endif // OTA_ENABLED && !defined(OFFLOADED_DOWNLOAD) - -#if OTA_ENABLED && defined(OTA_BASIC_AUTH) - _ota.setAuthentication(getDeviceId().c_str(), _password.c_str()); -#endif // OTA_ENABLED && !defined(OFFLOADED_DOWNLOAD) && defined(OTA_BASIC_AUTH) - -#ifdef BOARD_HAS_OFFLOADED_ECCX08 - if (String(WiFi.firmwareVersion()) < String("1.6.0")) { - DEBUG_ERROR("ArduinoIoTCloudTCP::%s In order to connect to Arduino IoT Cloud, NINA firmware needs to be >= 1.6.0, current %s", __FUNCTION__, WiFi.firmwareVersion()); - return 0; - } -#endif /* BOARD_HAS_OFFLOADED_ECCX08 */ - -#if defined (ARDUINO_UNOWIFIR4) - if (String(WiFi.firmwareVersion()) < String("0.2.0")) { - DEBUG_ERROR("ArduinoIoTCloudTCP::%s In order to connect to Arduino IoT Cloud, WiFi firmware needs to be >= 0.2.0, current %s", __FUNCTION__, WiFi.firmwareVersion()); - } -#endif - -#if NETWORK_CONFIGURATOR_ENABLED - if(_configurator != nullptr){ - _configurator->enableAgent(ConfiguratorAgent::AgentTypes::BLE,false); - _configurator->begin(); - } -#endif - return 1; -} - void ArduinoIoTCloudTCP::update() { /* Feed the watchdog. If any of the functions called below @@ -295,6 +236,65 @@ void ArduinoIoTCloudTCP::disconnect() { * PRIVATE MEMBER FUNCTIONS ******************************************************************************/ +int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress, uint16_t brokerPort, bool auto_reconnect) +{ + _enable_watchdog = enable_watchdog; + _brokerAddress = brokerAddress; + _brokerPort = brokerPort; + _auto_reconnect = auto_reconnect; + + _state = State::ConfigPhy; + + _mqttClient.setClient(_brokerClient); + +#ifdef BOARD_HAS_SECRET_KEY + if(_password.length()) + { + _mqttClient.setUsernamePassword(getDeviceId(), _password); + } +#endif + + _mqttClient.onMessage(ArduinoIoTCloudTCP::onMessage); + _mqttClient.setKeepAliveInterval(30 * 1000); + _mqttClient.setConnectionTimeout(1500); + _mqttClient.setId(getDeviceId().c_str()); + + _messageTopicOut = getTopic_messageout(); + _messageTopicIn = getTopic_messagein(); + + _thing.begin(); + _device.begin(); + +#if OTA_ENABLED && !defined(OFFLOADED_DOWNLOAD) + _ota.setClient(&_otaClient); +#endif // OTA_ENABLED && !defined(OFFLOADED_DOWNLOAD) + +#if OTA_ENABLED && defined(OTA_BASIC_AUTH) + _ota.setAuthentication(getDeviceId().c_str(), _password.c_str()); +#endif // OTA_ENABLED && !defined(OFFLOADED_DOWNLOAD) && defined(OTA_BASIC_AUTH) + +#ifdef BOARD_HAS_OFFLOADED_ECCX08 + if (String(WiFi.firmwareVersion()) < String("1.6.0")) { + DEBUG_ERROR("ArduinoIoTCloudTCP::%s In order to connect to Arduino IoT Cloud, NINA firmware needs to be >= 1.6.0, current %s", __FUNCTION__, WiFi.firmwareVersion()); + return 0; + } +#endif /* BOARD_HAS_OFFLOADED_ECCX08 */ + +#if defined (ARDUINO_UNOWIFIR4) + if (String(WiFi.firmwareVersion()) < String("0.2.0")) { + DEBUG_ERROR("ArduinoIoTCloudTCP::%s In order to connect to Arduino IoT Cloud, WiFi firmware needs to be >= 0.2.0, current %s", __FUNCTION__, WiFi.firmwareVersion()); + } +#endif + +#if NETWORK_CONFIGURATOR_ENABLED + if(_configurator != nullptr){ + _configurator->enableAgent(ConfiguratorAgent::AgentTypes::BLE,false); + _configurator->begin(); + } +#endif + return 1; +} + ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConfigPhy() { #if NETWORK_CONFIGURATOR_ENABLED diff --git a/src/ArduinoIoTCloudTCP.h b/src/ArduinoIoTCloudTCP.h index d59c6de9..49d2730c 100644 --- a/src/ArduinoIoTCloudTCP.h +++ b/src/ArduinoIoTCloudTCP.h @@ -75,7 +75,6 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass virtual void disconnect () override; int begin(ConnectionHandler & connection, bool const enable_watchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS, uint16_t brokerPort = DEFAULT_BROKER_PORT_AUTO, bool auto_reconnect = true); - int begin(bool const enable_watchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS, uint16_t brokerPort = DEFAULT_BROKER_PORT_AUTO, bool auto_reconnect = true); #if defined(BOARD_HAS_SECURE_ELEMENT) int updateCertificate(String authorityKeyIdentifier, String serialNumber, String notBefore, String notAfter, String signature); @@ -117,6 +116,9 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass #endif private: + + int begin(bool const enable_watchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS, uint16_t brokerPort = DEFAULT_BROKER_PORT_AUTO, bool auto_reconnect = true); + static const int MQTT_TRANSMIT_BUFFER_SIZE = 256; enum class State From a3d976ea19cb9892173058c1c1e7b293edf244e2 Mon Sep 17 00:00:00 2001 From: pennam Date: Thu, 5 Jun 2025 09:55:42 +0200 Subject: [PATCH 2/9] TCP: use camelcase for begin function parameters --- src/ArduinoIoTCloudTCP.cpp | 16 ++++++++-------- src/ArduinoIoTCloudTCP.h | 8 ++++---- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/ArduinoIoTCloudTCP.cpp b/src/ArduinoIoTCloudTCP.cpp index 867efd2e..7ffd9cef 100644 --- a/src/ArduinoIoTCloudTCP.cpp +++ b/src/ArduinoIoTCloudTCP.cpp @@ -80,7 +80,7 @@ ArduinoIoTCloudTCP::ArduinoIoTCloudTCP() * PUBLIC MEMBER FUNCTIONS ******************************************************************************/ -int ArduinoIoTCloudTCP::begin(ConnectionHandler & connection, bool const enable_watchdog, String brokerAddress, uint16_t brokerPort, bool auto_reconnect) +int ArduinoIoTCloudTCP::begin(ConnectionHandler & connection, bool const enableWatchdog, String brokerAddress, uint16_t brokerPort, bool autoReconnect) { _connection = &connection; _brokerAddress = brokerAddress; @@ -136,7 +136,7 @@ int ArduinoIoTCloudTCP::begin(ConnectionHandler & connection, bool const enable_ /* Setup retry timers */ _connection_attempt.begin(AIOT_CONFIG_RECONNECTION_RETRY_DELAY_ms, AIOT_CONFIG_MAX_RECONNECTION_RETRY_DELAY_ms); - return begin(enable_watchdog, _brokerAddress, _brokerPort, auto_reconnect); + return begin(enableWatchdog, _brokerAddress, _brokerPort, autoReconnect); } void ArduinoIoTCloudTCP::update() @@ -228,7 +228,7 @@ void ArduinoIoTCloudTCP::disconnect() { } _mqttClient.stop(); - _auto_reconnect = false; + _autoReconnect = false; _state = State::Disconnect; } @@ -236,12 +236,12 @@ void ArduinoIoTCloudTCP::disconnect() { * PRIVATE MEMBER FUNCTIONS ******************************************************************************/ -int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress, uint16_t brokerPort, bool auto_reconnect) +int ArduinoIoTCloudTCP::begin(bool const enableWatchdog, String brokerAddress, uint16_t brokerPort, bool autoReconnect) { - _enable_watchdog = enable_watchdog; + _enableWatchdog = enableWatchdog; _brokerAddress = brokerAddress; _brokerPort = brokerPort; - _auto_reconnect = auto_reconnect; + _autoReconnect = autoReconnect; _state = State::ConfigPhy; @@ -332,7 +332,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Init() * set a rather large timeout at first. */ #if defined (ARDUINO_ARCH_SAMD) || defined (ARDUINO_ARCH_MBED) - if (_enable_watchdog) { + if (_enableWatchdog) { /* Initialize watchdog hardware */ watchdog_enable(); /* Setup callbacks to feed the watchdog during offloaded network operations (connection/download)*/ @@ -448,7 +448,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Disconnect() DEBUG_INFO("Disconnected from Arduino IoT Cloud"); execCloudEventCallback(ArduinoIoTCloudEvent::DISCONNECT); - if(_auto_reconnect) { + if(_autoReconnect) { /* Setup timer for broker connection and restart */ _connection_attempt.begin(AIOT_CONFIG_RECONNECTION_RETRY_DELAY_ms, AIOT_CONFIG_MAX_RECONNECTION_RETRY_DELAY_ms); return State::ConnectPhy; diff --git a/src/ArduinoIoTCloudTCP.h b/src/ArduinoIoTCloudTCP.h index 49d2730c..dd12bfd5 100644 --- a/src/ArduinoIoTCloudTCP.h +++ b/src/ArduinoIoTCloudTCP.h @@ -74,7 +74,7 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass virtual void printDebugInfo() override; virtual void disconnect () override; - int begin(ConnectionHandler & connection, bool const enable_watchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS, uint16_t brokerPort = DEFAULT_BROKER_PORT_AUTO, bool auto_reconnect = true); + int begin(ConnectionHandler & connection, bool const enableWatchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS, uint16_t brokerPort = DEFAULT_BROKER_PORT_AUTO, bool autoReconnect = true); #if defined(BOARD_HAS_SECURE_ELEMENT) int updateCertificate(String authorityKeyIdentifier, String serialNumber, String notBefore, String notAfter, String signature); @@ -117,7 +117,7 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass private: - int begin(bool const enable_watchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS, uint16_t brokerPort = DEFAULT_BROKER_PORT_AUTO, bool auto_reconnect = true); + int begin(bool const enableWatchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS, uint16_t brokerPort = DEFAULT_BROKER_PORT_AUTO, bool autoReconnect = true); static const int MQTT_TRANSMIT_BUFFER_SIZE = 256; @@ -145,8 +145,8 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass uint8_t _mqtt_data_buf[MQTT_TRANSMIT_BUFFER_SIZE]; int _mqtt_data_len; bool _mqtt_data_request_retransmit; - bool _enable_watchdog; - bool _auto_reconnect; + bool _enableWatchdog; + bool _autoReconnect; #if defined(BOARD_HAS_SECRET_KEY) String _password; From 88bffe88f7e9bd33c62046290f5d911e232acf99 Mon Sep 17 00:00:00 2001 From: pennam Date: Thu, 5 Jun 2025 09:58:06 +0200 Subject: [PATCH 3/9] TCP: rename _brokerClient to _brokerTLSClient --- src/ArduinoIoTCloudTCP.cpp | 8 ++++---- src/ArduinoIoTCloudTCP.h | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/ArduinoIoTCloudTCP.cpp b/src/ArduinoIoTCloudTCP.cpp index 7ffd9cef..13372682 100644 --- a/src/ArduinoIoTCloudTCP.cpp +++ b/src/ArduinoIoTCloudTCP.cpp @@ -121,7 +121,7 @@ int ArduinoIoTCloudTCP::begin(ConnectionHandler & connection, bool const enableW } } #if !defined(BOARD_HAS_OFFLOADED_ECCX08) - _brokerClient.setEccSlot(static_cast(SElementArduinoCloudSlot::Key), _cert.bytes(), _cert.length()); + _brokerTLSClient.setEccSlot(static_cast(SElementArduinoCloudSlot::Key), _cert.bytes(), _cert.length()); #if OTA_ENABLED _otaClient.setEccSlot(static_cast(SElementArduinoCloudSlot::Key), _cert.bytes(), _cert.length()); #endif @@ -245,7 +245,7 @@ int ArduinoIoTCloudTCP::begin(bool const enableWatchdog, String brokerAddress, u _state = State::ConfigPhy; - _mqttClient.setClient(_brokerClient); + _mqttClient.setClient(_brokerTLSClient); #ifdef BOARD_HAS_SECRET_KEY if(_password.length()) @@ -316,7 +316,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Init() { /* Setup broker TLS client */ /* Setup broker TLS client */ - _brokerClient.begin(*_connection, _authMode); + _brokerTLSClient.begin(*_connection, _authMode); #if OTA_ENABLED /* Setup OTA TLS client */ @@ -393,7 +393,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectMqttBroker() _connection_attempt.retry(); #if defined(BOARD_HAS_ECCX08) && !defined(BOARD_HAS_OFFLOADED_ECCX08) - DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not connect to %s:%d Mqtt error: %d TLS error: %d", __FUNCTION__, _brokerAddress.c_str(), _brokerPort, _mqttClient.connectError(), _brokerClient.errorCode()); + DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not connect to %s:%d Mqtt error: %d TLS error: %d", __FUNCTION__, _brokerAddress.c_str(), _brokerPort, _mqttClient.connectError(), _brokerTLSClient.errorCode()); #else DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not connect to %s:%d Error: %d", __FUNCTION__, _brokerAddress.c_str(), _brokerPort, _mqttClient.connectError()); #endif diff --git a/src/ArduinoIoTCloudTCP.h b/src/ArduinoIoTCloudTCP.h index dd12bfd5..dec223b2 100644 --- a/src/ArduinoIoTCloudTCP.h +++ b/src/ArduinoIoTCloudTCP.h @@ -159,7 +159,7 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass bool _writeCertOnConnect; #endif - TLSClientMqtt _brokerClient; + TLSClientMqtt _brokerTLSClient; MqttClient _mqttClient; String _messageTopicOut; From 91441686300b1e0e544cec90da24b9c0240e6dcd Mon Sep 17 00:00:00 2001 From: pennam Date: Thu, 5 Jun 2025 09:59:21 +0200 Subject: [PATCH 4/9] TCP: rename _otaClient to _otaTLSClient --- src/ArduinoIoTCloudTCP.cpp | 6 +++--- src/ArduinoIoTCloudTCP.h | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ArduinoIoTCloudTCP.cpp b/src/ArduinoIoTCloudTCP.cpp index 13372682..0d2996ed 100644 --- a/src/ArduinoIoTCloudTCP.cpp +++ b/src/ArduinoIoTCloudTCP.cpp @@ -123,7 +123,7 @@ int ArduinoIoTCloudTCP::begin(ConnectionHandler & connection, bool const enableW #if !defined(BOARD_HAS_OFFLOADED_ECCX08) _brokerTLSClient.setEccSlot(static_cast(SElementArduinoCloudSlot::Key), _cert.bytes(), _cert.length()); #if OTA_ENABLED - _otaClient.setEccSlot(static_cast(SElementArduinoCloudSlot::Key), _cert.bytes(), _cert.length()); + _otaTLSClient.setEccSlot(static_cast(SElementArduinoCloudSlot::Key), _cert.bytes(), _cert.length()); #endif #endif _brokerPort = (brokerPort == DEFAULT_BROKER_PORT_AUTO) ? DEFAULT_BROKER_PORT_SECURE_AUTH : brokerPort; @@ -266,7 +266,7 @@ int ArduinoIoTCloudTCP::begin(bool const enableWatchdog, String brokerAddress, u _device.begin(); #if OTA_ENABLED && !defined(OFFLOADED_DOWNLOAD) - _ota.setClient(&_otaClient); + _ota.setClient(&_otaTLSClient); #endif // OTA_ENABLED && !defined(OFFLOADED_DOWNLOAD) #if OTA_ENABLED && defined(OTA_BASIC_AUTH) @@ -320,7 +320,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Init() #if OTA_ENABLED /* Setup OTA TLS client */ - _otaClient.begin(*_connection); + _otaTLSClient.begin(*_connection); #endif /* Setup TimeService */ diff --git a/src/ArduinoIoTCloudTCP.h b/src/ArduinoIoTCloudTCP.h index dec223b2..56ddc6eb 100644 --- a/src/ArduinoIoTCloudTCP.h +++ b/src/ArduinoIoTCloudTCP.h @@ -168,7 +168,7 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass String _dataTopicIn; #if OTA_ENABLED - TLSClientOta _otaClient; + TLSClientOta _otaTLSClient; ArduinoCloudOTA _ota; onOTARequestCallbackFunc _get_ota_confirmation; #endif /* OTA_ENABLED */ From 734e0618124db0c73c8eb64e852c1f66bce6fcbc Mon Sep 17 00:00:00 2001 From: pennam Date: Thu, 5 Jun 2025 10:01:24 +0200 Subject: [PATCH 5/9] rename TLSClientMqtt to TLSClientBroker --- examples/utility/Provisioning_2.0/CSRHandler.cpp | 2 +- examples/utility/Provisioning_2.0/CSRHandler.h | 4 ++-- src/ArduinoIoTCloudTCP.h | 4 ++-- .../{TLSClientMqtt.cpp => TLSClientBroker.cpp} | 4 ++-- .../utility/{TLSClientMqtt.h => TLSClientBroker.h} | 14 +++++++------- 5 files changed, 14 insertions(+), 14 deletions(-) rename src/tls/utility/{TLSClientMqtt.cpp => TLSClientBroker.cpp} (94%) rename src/tls/utility/{TLSClientMqtt.h => TLSClientBroker.h} (81%) diff --git a/examples/utility/Provisioning_2.0/CSRHandler.cpp b/examples/utility/Provisioning_2.0/CSRHandler.cpp index 1a6101e4..339d934e 100644 --- a/examples/utility/Provisioning_2.0/CSRHandler.cpp +++ b/examples/utility/Provisioning_2.0/CSRHandler.cpp @@ -85,7 +85,7 @@ bool CSRHandlerClass::begin(ConnectionHandler &connectionHandler, SecureElement _fw_version = WiFi.firmwareVersion(); #endif if(!_tlsClient){ - _tlsClient = new TLSClientMqtt(); + _tlsClient = new TLSClientBroker(); } _tlsClient->begin(*_connectionHandler); _tlsClient->setTimeout(RESPONSE_TIMEOUT); diff --git a/examples/utility/Provisioning_2.0/CSRHandler.h b/examples/utility/Provisioning_2.0/CSRHandler.h index ae5956b7..1f83e64d 100644 --- a/examples/utility/Provisioning_2.0/CSRHandler.h +++ b/examples/utility/Provisioning_2.0/CSRHandler.h @@ -10,7 +10,7 @@ #include #include #include -#include +#include #include #include "utility/LEDFeedback.h" #define JITTER_BASE 0 @@ -55,7 +55,7 @@ class CSRHandlerClass { ECP256Certificate *_certForCSR; ConnectionHandler *_connectionHandler; SecureElement *_secureElement; - TLSClientMqtt *_tlsClient; + TLSClientBroker *_tlsClient; HttpClient *_client; LEDFeedbackClass &_ledFeedback; void updateNextRequestAt(); diff --git a/src/ArduinoIoTCloudTCP.h b/src/ArduinoIoTCloudTCP.h index 56ddc6eb..908738b7 100644 --- a/src/ArduinoIoTCloudTCP.h +++ b/src/ArduinoIoTCloudTCP.h @@ -34,7 +34,7 @@ #include #endif -#include +#include #include #if OTA_ENABLED @@ -159,7 +159,7 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass bool _writeCertOnConnect; #endif - TLSClientMqtt _brokerTLSClient; + TLSClientBroker _brokerTLSClient; MqttClient _mqttClient; String _messageTopicOut; diff --git a/src/tls/utility/TLSClientMqtt.cpp b/src/tls/utility/TLSClientBroker.cpp similarity index 94% rename from src/tls/utility/TLSClientMqtt.cpp rename to src/tls/utility/TLSClientBroker.cpp index d775f868..e9cd4515 100644 --- a/src/tls/utility/TLSClientMqtt.cpp +++ b/src/tls/utility/TLSClientBroker.cpp @@ -12,7 +12,7 @@ #ifdef HAS_TCP -#include "TLSClientMqtt.h" +#include "TLSClientBroker.h" #if defined(BOARD_HAS_SECRET_KEY) #include "tls/AIoTCUPCert.h" @@ -31,7 +31,7 @@ #endif -void TLSClientMqtt::begin(ConnectionHandler & connection, ArduinoIoTAuthenticationMode authMode) { +void TLSClientBroker::begin(ConnectionHandler & connection, ArduinoIoTAuthenticationMode authMode) { #if defined(BOARD_HAS_OFFLOADED_ECCX08) /* Arduino Root CA is configured in nina-fw diff --git a/src/tls/utility/TLSClientMqtt.h b/src/tls/utility/TLSClientBroker.h similarity index 81% rename from src/tls/utility/TLSClientMqtt.h rename to src/tls/utility/TLSClientBroker.h index 689eff2f..1024700a 100644 --- a/src/tls/utility/TLSClientMqtt.h +++ b/src/tls/utility/TLSClientBroker.h @@ -25,7 +25,7 @@ enum class ArduinoIoTAuthenticationMode * Arduino NANO 33 IoT - WiFi */ #include "WiFiSSLClient.h" - class TLSClientMqtt : public WiFiBearSSLClient { + class TLSClientBroker : public WiFiBearSSLClient { #elif defined(BOARD_HAS_ECCX08) /* * Arduino MKR GSM 1400 @@ -37,31 +37,31 @@ enum class ArduinoIoTAuthenticationMode */ #include #include - class TLSClientMqtt : public BearSSLClient { + class TLSClientBroker : public BearSSLClient { #elif defined(ARDUINO_PORTENTA_C33) /* * Arduino Portenta C33 */ #include - class TLSClientMqtt : public SSLClient { + class TLSClientBroker : public SSLClient { #elif defined(ARDUINO_NICLA_VISION) /* * Arduino Nicla Vision */ #include - class TLSClientMqtt : public WiFiSSLSE050Client { + class TLSClientBroker : public WiFiSSLSE050Client { #elif defined(ARDUINO_EDGE_CONTROL) /* * Arduino Edge Control */ #include - class TLSClientMqtt : public GSMSSLClient { + class TLSClientBroker : public GSMSSLClient { #elif defined(ARDUINO_UNOR4_WIFI) /* * Arduino UNO R4 WiFi */ #include - class TLSClientMqtt : public WiFiSSLClient { + class TLSClientBroker : public WiFiSSLClient { #elif defined(BOARD_ESP) || defined(ARDUINO_RASPBERRY_PI_PICO_W) /* * ESP32* @@ -69,7 +69,7 @@ enum class ArduinoIoTAuthenticationMode * PICOW */ #include - class TLSClientMqtt : public WiFiClientSecure { + class TLSClientBroker : public WiFiClientSecure { #endif public: From 00f785ade9f546d031b6042053e42a742647fe8e Mon Sep 17 00:00:00 2001 From: pennam Date: Thu, 5 Jun 2025 11:35:58 +0200 Subject: [PATCH 6/9] TCP: add begin method to configure clients without ConnectionHandler --- src/ArduinoIoTCloudLPWAN.cpp | 2 +- src/ArduinoIoTCloudTCP.cpp | 145 ++++++++++++++++------------ src/ArduinoIoTCloudTCP.h | 9 +- src/tls/utility/TLSClientBroker.cpp | 14 ++- src/tls/utility/TLSClientBroker.h | 2 +- src/tls/utility/TLSClientOta.cpp | 15 ++- src/tls/utility/TLSClientOta.h | 19 ++-- src/utility/time/TimeService.h | 1 - 8 files changed, 121 insertions(+), 86 deletions(-) diff --git a/src/ArduinoIoTCloudLPWAN.cpp b/src/ArduinoIoTCloudLPWAN.cpp index 2e269346..a8da091c 100644 --- a/src/ArduinoIoTCloudLPWAN.cpp +++ b/src/ArduinoIoTCloudLPWAN.cpp @@ -70,7 +70,7 @@ int ArduinoIoTCloudLPWAN::begin(ConnectionHandler& connection, bool retry) { _connection = &connection; _retryEnable = retry; - _time_service.begin(nullptr); + _time_service.begin(); return 1; } diff --git a/src/ArduinoIoTCloudTCP.cpp b/src/ArduinoIoTCloudTCP.cpp index 0d2996ed..23bd9a95 100644 --- a/src/ArduinoIoTCloudTCP.cpp +++ b/src/ArduinoIoTCloudTCP.cpp @@ -80,63 +80,29 @@ ArduinoIoTCloudTCP::ArduinoIoTCloudTCP() * PUBLIC MEMBER FUNCTIONS ******************************************************************************/ -int ArduinoIoTCloudTCP::begin(ConnectionHandler & connection, bool const enableWatchdog, String brokerAddress, uint16_t brokerPort, bool autoReconnect) +int ArduinoIoTCloudTCP::begin(ConnectionHandler& connection, bool const enableWatchdog, String brokerAddress, uint16_t brokerPort, bool autoReconnect) { _connection = &connection; - _brokerAddress = brokerAddress; - - _authMode = ArduinoIoTAuthenticationMode::CERTIFICATE; -#if defined (BOARD_HAS_SECRET_KEY) - /* If board supports and sketch is configured for username and password login */ - if(_password.length()) { - _authMode = ArduinoIoTAuthenticationMode::PASSWORD; - } +#if OTA_ENABLED + return begin(_connection->getClient(), TLSClientOta::getNewClient(_connection->getInterface()), _connection->getUDP(), enableWatchdog, brokerAddress, brokerPort, autoReconnect); +#else + return begin(_connection->getClient(), _connection->getUDP(), enableWatchdog, brokerAddress, brokerPort, autoReconnect); #endif +} - /* If board is configured for certificate authentication and mTLS */ - if(_authMode == ArduinoIoTAuthenticationMode::CERTIFICATE) - { -#if defined(BOARD_HAS_SECURE_ELEMENT) - if (!_selement.begin()) - { - DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not initialize secure element.", __FUNCTION__); - #if defined(ARDUINO_UNOWIFIR4) - if (String(WiFi.firmwareVersion()) < String("0.4.1")) { - DEBUG_ERROR("ArduinoIoTCloudTCP::%s In order to read device certificate, WiFi firmware needs to be >= 0.4.1, current %s", __FUNCTION__, WiFi.firmwareVersion()); - } - #endif - return 0; - } - if (!SElementArduinoCloudDeviceId::read(_selement, getDeviceId(), SElementArduinoCloudSlot::DeviceId)) - { - DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not read device id.", __FUNCTION__); - return 0; - } - if (!_writeCertOnConnect) { - /* No update pending read certificate stored in secure element */ - if (!SElementArduinoCloudCertificate::read(_selement, _cert, SElementArduinoCloudSlot::CompressedCertificate)) - { - DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not read device certificate.", __FUNCTION__); - return 0; - } - } - #if !defined(BOARD_HAS_OFFLOADED_ECCX08) - _brokerTLSClient.setEccSlot(static_cast(SElementArduinoCloudSlot::Key), _cert.bytes(), _cert.length()); - #if OTA_ENABLED - _otaTLSClient.setEccSlot(static_cast(SElementArduinoCloudSlot::Key), _cert.bytes(), _cert.length()); - #endif - #endif - _brokerPort = (brokerPort == DEFAULT_BROKER_PORT_AUTO) ? DEFAULT_BROKER_PORT_SECURE_AUTH : brokerPort; +int ArduinoIoTCloudTCP::begin(Client& brokerClient, Client& otaClient, UDP& ntpClient, bool const enableWatchdog, String brokerAddress, uint16_t brokerPort, bool autoReconnect) +{ +#if OTA_ENABLED + _otaClient = &otaClient; #endif - } - else - { - _brokerPort = (brokerPort == DEFAULT_BROKER_PORT_AUTO) ? DEFAULT_BROKER_PORT_USER_PASS_AUTH : brokerPort; - } + return begin(brokerClient, ntpClient, enableWatchdog, brokerAddress, _brokerPort, autoReconnect); +} - /* Setup retry timers */ - _connection_attempt.begin(AIOT_CONFIG_RECONNECTION_RETRY_DELAY_ms, AIOT_CONFIG_MAX_RECONNECTION_RETRY_DELAY_ms); - return begin(enableWatchdog, _brokerAddress, _brokerPort, autoReconnect); +int ArduinoIoTCloudTCP::begin(Client& brokerClient, UDP& ntpClient, bool const enableWatchdog, String brokerAddress, uint16_t brokerPort, bool autoReconnect) +{ + _brokerClient = &brokerClient; + _ntpClient = &ntpClient; + return begin(enableWatchdog, brokerAddress, brokerPort, autoReconnect); } void ArduinoIoTCloudTCP::update() @@ -175,18 +141,21 @@ void ArduinoIoTCloudTCP::update() /* Poll the network configurator to check if it is updating the configuration. * The polling must be performed only if the the first configuration is completed. */ - #if NETWORK_CONFIGURATOR_ENABLED +#if NETWORK_CONFIGURATOR_ENABLED if(_configurator != nullptr && _state > State::Init && _configurator->update() == NetworkConfiguratorStates::UPDATING_CONFIG){ _state = State::ConfigPhy; } - #endif +#endif #if OTA_ENABLED /* OTA FSM needs to reach the Idle state before being able to run independently from * the mqttClient. The state can be reached only after the mqttClient is connected to * the broker. + * + * We also have to check that the OTA client is not null. It can happen if we don't + * use the ArduinoConnectionHandler library and the user doesn't provide it. */ - if(_state <= State::ConnectPhy){ + if ((_state <= State::ConnectPhy) || (_otaClient == nullptr)) { return; } @@ -238,11 +207,61 @@ void ArduinoIoTCloudTCP::disconnect() { int ArduinoIoTCloudTCP::begin(bool const enableWatchdog, String brokerAddress, uint16_t brokerPort, bool autoReconnect) { + _authMode = ArduinoIoTAuthenticationMode::CERTIFICATE; +#if defined (BOARD_HAS_SECRET_KEY) + /* If board supports and sketch is configured for username and password login */ + if(_password.length()) { + _authMode = ArduinoIoTAuthenticationMode::PASSWORD; + } +#endif + + /* If board is configured for certificate authentication and mTLS */ + if(_authMode == ArduinoIoTAuthenticationMode::CERTIFICATE) + { +#if defined(BOARD_HAS_SECURE_ELEMENT) + if (!_selement.begin()) + { + DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not initialize secure element.", __FUNCTION__); + #if defined(ARDUINO_UNOWIFIR4) + if (String(WiFi.firmwareVersion()) < String("0.4.1")) { + DEBUG_ERROR("ArduinoIoTCloudTCP::%s In order to read device certificate, WiFi firmware needs to be >= 0.4.1, current %s", __FUNCTION__, WiFi.firmwareVersion()); + } + #endif + return 0; + } + if (!SElementArduinoCloudDeviceId::read(_selement, getDeviceId(), SElementArduinoCloudSlot::DeviceId)) + { + DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not read device id.", __FUNCTION__); + return 0; + } + if (!_writeCertOnConnect) { + /* No update pending read certificate stored in secure element */ + if (!SElementArduinoCloudCertificate::read(_selement, _cert, SElementArduinoCloudSlot::CompressedCertificate)) + { + DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not read device certificate.", __FUNCTION__); + return 0; + } + } + #if !defined(BOARD_HAS_OFFLOADED_ECCX08) + _brokerTLSClient.setEccSlot(static_cast(SElementArduinoCloudSlot::Key), _cert.bytes(), _cert.length()); + #if OTA_ENABLED + _otaTLSClient.setEccSlot(static_cast(SElementArduinoCloudSlot::Key), _cert.bytes(), _cert.length()); + #endif + #endif + _brokerPort = (brokerPort == DEFAULT_BROKER_PORT_AUTO) ? DEFAULT_BROKER_PORT_SECURE_AUTH : brokerPort; +#endif + } + else + { + _brokerPort = (brokerPort == DEFAULT_BROKER_PORT_AUTO) ? DEFAULT_BROKER_PORT_USER_PASS_AUTH : brokerPort; + } + + /* Setup retry timers */ + _connection_attempt.begin(AIOT_CONFIG_RECONNECTION_RETRY_DELAY_ms, AIOT_CONFIG_MAX_RECONNECTION_RETRY_DELAY_ms); + _enableWatchdog = enableWatchdog; _brokerAddress = brokerAddress; - _brokerPort = brokerPort; _autoReconnect = autoReconnect; - _state = State::ConfigPhy; _mqttClient.setClient(_brokerTLSClient); @@ -283,6 +302,7 @@ int ArduinoIoTCloudTCP::begin(bool const enableWatchdog, String brokerAddress, u #if defined (ARDUINO_UNOWIFIR4) if (String(WiFi.firmwareVersion()) < String("0.2.0")) { DEBUG_ERROR("ArduinoIoTCloudTCP::%s In order to connect to Arduino IoT Cloud, WiFi firmware needs to be >= 0.2.0, current %s", __FUNCTION__, WiFi.firmwareVersion()); + return 0; } #endif @@ -302,10 +322,10 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConfigPhy() return State::Init; } - if(_configurator->update() == NetworkConfiguratorStates::CONFIGURED) { - _configurator->disconnectAgent(); - return State::Init; - } + if (_configurator->update() == NetworkConfiguratorStates::CONFIGURED) { + _configurator->disconnectAgent(); + return State::Init; + } return State::ConfigPhy; #else return State::Init; @@ -315,12 +335,11 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConfigPhy() ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Init() { /* Setup broker TLS client */ - /* Setup broker TLS client */ - _brokerTLSClient.begin(*_connection, _authMode); + _brokerTLSClient.begin(_brokerClient, _authMode); #if OTA_ENABLED /* Setup OTA TLS client */ - _otaTLSClient.begin(*_connection); + _otaTLSClient.begin(_otaClient); #endif /* Setup TimeService */ diff --git a/src/ArduinoIoTCloudTCP.h b/src/ArduinoIoTCloudTCP.h index 908738b7..9f9670a2 100644 --- a/src/ArduinoIoTCloudTCP.h +++ b/src/ArduinoIoTCloudTCP.h @@ -74,7 +74,9 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass virtual void printDebugInfo() override; virtual void disconnect () override; - int begin(ConnectionHandler & connection, bool const enableWatchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS, uint16_t brokerPort = DEFAULT_BROKER_PORT_AUTO, bool autoReconnect = true); + int begin(ConnectionHandler& connection, bool const enableWatchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS, uint16_t brokerPort = DEFAULT_BROKER_PORT_AUTO, bool autoReconnect = true); + int begin(Client& brokerClient, Client& otaClient, UDP& ntpClient, bool const enableWatchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS, uint16_t brokerPort = DEFAULT_BROKER_PORT_AUTO, bool autoReconnect = true); + int begin(Client& brokerClient, UDP& ntpClient, bool const enableWatchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS, uint16_t brokerPort = DEFAULT_BROKER_PORT_AUTO, bool autoReconnect = true); #if defined(BOARD_HAS_SECURE_ELEMENT) int updateCertificate(String authorityKeyIdentifier, String serialNumber, String notBefore, String notAfter, String signature); @@ -159,8 +161,11 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass bool _writeCertOnConnect; #endif + /* Base client from sketch or ConnectionHandler */ + Client * _brokerClient; TLSClientBroker _brokerTLSClient; MqttClient _mqttClient; + UDP * _ntpClient; String _messageTopicOut; String _messageTopicIn; @@ -168,6 +173,8 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass String _dataTopicIn; #if OTA_ENABLED + /* Base client from sketch or ConnectionHandler */ + Client * _otaClient; TLSClientOta _otaTLSClient; ArduinoCloudOTA _ota; onOTARequestCallbackFunc _get_ota_confirmation; diff --git a/src/tls/utility/TLSClientBroker.cpp b/src/tls/utility/TLSClientBroker.cpp index e9cd4515..80638e60 100644 --- a/src/tls/utility/TLSClientBroker.cpp +++ b/src/tls/utility/TLSClientBroker.cpp @@ -31,22 +31,28 @@ #endif -void TLSClientBroker::begin(ConnectionHandler & connection, ArduinoIoTAuthenticationMode authMode) { +void TLSClientBroker::begin(Client* client, ArduinoIoTAuthenticationMode authMode) { + +/* Client* is coming from a reference in ArduinoIoTCloud::begin( .. ) + * The Client must be instantiated in the user sketch, for example: + * WiFiClientSecure client; + */ #if defined(BOARD_HAS_OFFLOADED_ECCX08) /* Arduino Root CA is configured in nina-fw * https://github.com/arduino/nina-fw/blob/master/arduino/libraries/ArduinoBearSSL/src/BearSSLTrustAnchors.h */ (void)authMode; + (void)client; #elif defined(BOARD_HAS_ECCX08) (void)authMode; - setClient(connection.getClient()); + setClient(*client); setProfile(aiotc_client_profile_init); setTrustAnchors(ArduinoIoTCloudTrustAnchor, ArduinoIoTCloudTrustAnchor_NUM); ArduinoBearSSL.onGetTime(getTime); #elif defined(ARDUINO_PORTENTA_C33) (void)authMode; - setClient(connection.getClient()); + setClient(*client); setCACert(AIoTSSCert); #elif defined(ARDUINO_NICLA_VISION) (void)authMode; @@ -61,7 +67,7 @@ void TLSClientBroker::begin(ConnectionHandler & connection, ArduinoIoTAuthentica * also present in older firmware revisions * https://github.com/arduino/uno-r4-wifi-usb-bridge/blob/f09ca94fdcab845b8368d4435fdac9f6999d21d2/certificates/certificates.pem#L852 */ - (void)connection; + (void)client; /* Temporary force CACert to add new CA without rebuilding firmware */ if (authMode == ArduinoIoTAuthenticationMode::CERTIFICATE) { setCACert(AIoTSSCert); diff --git a/src/tls/utility/TLSClientBroker.h b/src/tls/utility/TLSClientBroker.h index 1024700a..579e00eb 100644 --- a/src/tls/utility/TLSClientBroker.h +++ b/src/tls/utility/TLSClientBroker.h @@ -73,6 +73,6 @@ enum class ArduinoIoTAuthenticationMode #endif public: - void begin(ConnectionHandler & connection, ArduinoIoTAuthenticationMode authMode = ArduinoIoTAuthenticationMode::CERTIFICATE); + void begin(Client* client, ArduinoIoTAuthenticationMode authMode = ArduinoIoTAuthenticationMode::CERTIFICATE); }; diff --git a/src/tls/utility/TLSClientOta.cpp b/src/tls/utility/TLSClientOta.cpp index 8d90e64b..02e017cb 100644 --- a/src/tls/utility/TLSClientOta.cpp +++ b/src/tls/utility/TLSClientOta.cpp @@ -30,18 +30,25 @@ } #endif -void TLSClientOta::begin(ConnectionHandler &connection) { +void TLSClientOta::begin(Client* client) { + +/* Client* is coming from a reference in ArduinoIoTCloud::begin( .. ) + * The Client must be instantiated in the user sketch, for example: + * WiFiClientSecure client; + */ + #if defined(BOARD_HAS_OFFLOADED_ECCX08) /* AWS Root CAs are configured in nina-fw * https://github.com/arduino/nina-fw/blob/master/data/roots.pem */ + (void)client; #elif defined(BOARD_HAS_ECCX08) - setClient(*getNewClient(connection.getInterface())); + setClient(*client); setProfile(aiotc_client_profile_init); setTrustAnchors(ArduinoIoTCloudTrustAnchor, ArduinoIoTCloudTrustAnchor_NUM); ArduinoBearSSL.onGetTime(getTime); #elif defined(ARDUINO_PORTENTA_C33) - setClient(*getNewClient(connection.getInterface())); + setClient(*client); setCACert(AIoTSSCert); #elif defined(ARDUINO_NICLA_VISION) appendCustomCACert(AIoTSSCert); @@ -51,7 +58,7 @@ void TLSClientOta::begin(ConnectionHandler &connection) { /* AWS Root CAs are configured in uno-r4-wifi-usb-bridge/libraries/Arduino_ESP32_OTA * https://github.com/arduino-libraries/Arduino_ESP32_OTA/blob/fc755e7d1d3946232107e2590662ee08d6ccdec4/src/tls/amazon_root_ca.h */ - (void)connection; + (void)client; #elif defined(ARDUINO_RASPBERRY_PI_PICO_W) setCACert(AIoTUPCert); #elif defined(ARDUINO_ARCH_ESP32) diff --git a/src/tls/utility/TLSClientOta.h b/src/tls/utility/TLSClientOta.h index 6859a96e..497bf3e8 100644 --- a/src/tls/utility/TLSClientOta.h +++ b/src/tls/utility/TLSClientOta.h @@ -67,37 +67,34 @@ #endif public: - void begin(ConnectionHandler & connection); + void begin(Client* client); -private: - inline Client* getNewClient(NetworkAdapter net) { + static inline Client& getNewClient(NetworkAdapter net) { switch(net) { #ifdef BOARD_HAS_WIFI case NetworkAdapter::WIFI: - return new WiFiClient(); + return * new WiFiClient(); #endif // BOARD_HAS_WIFI #ifdef BOARD_HAS_ETHERNET case NetworkAdapter::ETHERNET: - return new EthernetClient(); + return * new EthernetClient(); #endif // BOARD_HAS_ETHERNET #ifdef BOARD_HAS_NB case NetworkAdapter::NB: - return new NBClient(); + return * new NBClient(); #endif // BOARD_HAS_NB #ifdef BOARD_HAS_GSM case NetworkAdapter::GSM: - return new GSMClient(); + return * new GSMClient(); #endif // BOARD_HAS_GSM #ifdef BOARD_HAS_CATM1_NBIOT case NetworkAdapter::CATM1: - return new GSMClient(); + return * new GSMClient(); #endif // BOARD_HAS_CATM1_NBIOT #ifdef BOARD_HAS_CELLULAR case NetworkAdapter::CELL: - return new TinyGsmClient(modem, 1); + return * new TinyGsmClient(modem, 1); #endif // BOARD_HAS_CELLULAR - default: - return nullptr; } } }; diff --git a/src/utility/time/TimeService.h b/src/utility/time/TimeService.h index 71656d94..a830aa7a 100644 --- a/src/utility/time/TimeService.h +++ b/src/utility/time/TimeService.h @@ -59,7 +59,6 @@ class TimeServiceClass static bool isTimeValid(unsigned long const time); private: - ConnectionHandler * _con_hdl; bool _is_rtc_configured; bool _is_tz_configured; From 06e02975056a731f08feda89e9c4b1eb6d079317 Mon Sep 17 00:00:00 2001 From: pennam Date: Thu, 5 Jun 2025 22:22:18 +0200 Subject: [PATCH 7/9] add define to enable/disable connection handler --- extras/test/include/Arduino.h | 3 ++ src/AIoTC_Config.h | 12 ++++- src/ArduinoIoTCloud.cpp | 6 ++- src/ArduinoIoTCloud.h | 21 ++++++--- src/ArduinoIoTCloudDevice.cpp | 4 ++ src/ArduinoIoTCloudTCP.cpp | 37 +++++++++++---- src/ArduinoIoTCloudTCP.h | 2 + src/ArduinoIoTCloudThing.cpp | 4 ++ src/ota/implementation/OTANanoRP2040.cpp | 4 +- src/ota/implementation/OTASamd.cpp | 4 +- src/ota/implementation/OTAUnoR4.cpp | 4 +- src/ota/interface/OTAInterface.h | 4 +- src/tls/utility/TLSClientBroker.h | 4 +- src/tls/utility/TLSClientOta.h | 7 ++- src/utility/time/TimeService.cpp | 58 +++++++++++++++++++----- src/utility/time/TimeService.h | 16 +++++-- src/utility/watchdog/Watchdog.cpp | 8 +++- src/utility/watchdog/Watchdog.h | 7 ++- 18 files changed, 161 insertions(+), 44 deletions(-) diff --git a/extras/test/include/Arduino.h b/extras/test/include/Arduino.h index 9e743f96..010f80a9 100644 --- a/extras/test/include/Arduino.h +++ b/extras/test/include/Arduino.h @@ -23,6 +23,9 @@ ******************************************************************************/ typedef std::string String; +typedef struct { + void * udp; +} UDP; /****************************************************************************** FUNCTION PROTOTYPES diff --git a/src/AIoTC_Config.h b/src/AIoTC_Config.h index a25b8108..e0966e58 100644 --- a/src/AIoTC_Config.h +++ b/src/AIoTC_Config.h @@ -58,6 +58,12 @@ * AUTOMATICALLY CONFIGURED DEFINES ******************************************************************************/ +#if defined(DEBUG_ERROR) || defined(DEBUG_WARNING) || defined(DEBUG_INFO) || defined(DEBUG_DEBUG) || defined(DEBUG_VERBOSE) + #define DEBUG_ENABLED (1) +#else + #define DEBUG_ENABLED (0) +#endif + #if !defined(HAS_NOTECARD) #if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) @@ -148,8 +154,10 @@ #define BOARD_STM32H7 #endif -#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_NICLA_VISION) || defined(ARDUINO_OPTA) || defined(ARDUINO_GIGA) \ - || defined(ARDUINO_UNOR4_WIFI) || defined(ARDUINO_PORTENTA_C33) +#define CONNECTION_HANDLER_ENABLED (1) + +#if ((defined(BOARD_STM32H7) || defined(ARDUINO_UNOR4_WIFI) || defined(ARDUINO_PORTENTA_C33)) &&\ + (defined(CONNECTION_HANDLER_ENABLED) && (CONNECTION_HANDLER_ENABLED == 1))) #define NETWORK_CONFIGURATOR_ENABLED (1) #else #define NETWORK_CONFIGURATOR_ENABLED (0) diff --git a/src/ArduinoIoTCloud.cpp b/src/ArduinoIoTCloud.cpp index 74443ff5..e58201fb 100644 --- a/src/ArduinoIoTCloud.cpp +++ b/src/ArduinoIoTCloud.cpp @@ -26,11 +26,13 @@ ******************************************************************************/ ArduinoIoTCloudClass::ArduinoIoTCloudClass() -: _connection{nullptr} +: _time_service(TimeService) +#if CONNECTION_HANDLER_ENABLED +,_connection{nullptr} +#endif #if NETWORK_CONFIGURATOR_ENABLED , _configurator{nullptr} #endif -, _time_service(TimeService) , _thing_id{"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"} , _lib_version{AIOT_CONFIG_LIB_VERSION} , _device_id{"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"} diff --git a/src/ArduinoIoTCloud.h b/src/ArduinoIoTCloud.h index 19e94555..484038b1 100644 --- a/src/ArduinoIoTCloud.h +++ b/src/ArduinoIoTCloud.h @@ -24,12 +24,15 @@ #include -#include +#if CONNECTION_HANDLER_ENABLED + #include +#endif + #if NETWORK_CONFIGURATOR_ENABLED #include #endif -#if defined(DEBUG_ERROR) || defined(DEBUG_WARNING) || defined(DEBUG_INFO) || defined(DEBUG_DEBUG) || defined(DEBUG_VERBOSE) +#if DEBUG_ENABLED #include #endif @@ -99,14 +102,16 @@ class ArduinoIoTCloudClass inline void setDeviceId(String const device_id) { _device_id = device_id; }; inline String & getDeviceId() { return _device_id; }; +#if CONNECTION_HANDLER_ENABLED inline ConnectionHandler * getConnection() { return _connection; } +#endif inline unsigned long getInternalTime() { return _time_service.getTime(); } inline unsigned long getLocalTime() { return _time_service.getLocalTime(); } - #if NETWORK_CONFIGURATOR_ENABLED +#if NETWORK_CONFIGURATOR_ENABLED inline void setConfigurator(NetworkConfiguratorClass & configurator) { _configurator = &configurator; } - #endif +#endif void addCallback(ArduinoIoTCloudEvent const event, OnCloudEventCallback callback); #define addProperty( v, ...) addPropertyReal(v, #v, __VA_ARGS__) @@ -151,11 +156,13 @@ class ArduinoIoTCloudClass protected: + TimeServiceClass & _time_service; +#if CONNECTION_HANDLER_ENABLED ConnectionHandler * _connection; - #if NETWORK_CONFIGURATOR_ENABLED +#endif +#if NETWORK_CONFIGURATOR_ENABLED NetworkConfiguratorClass * _configurator; - #endif - TimeServiceClass & _time_service; +#endif String _thing_id; String _lib_version; diff --git a/src/ArduinoIoTCloudDevice.cpp b/src/ArduinoIoTCloudDevice.cpp index 58f00d20..a90026bf 100644 --- a/src/ArduinoIoTCloudDevice.cpp +++ b/src/ArduinoIoTCloudDevice.cpp @@ -19,6 +19,10 @@ #include "ArduinoIoTCloudDevice.h" #include "interfaces/CloudProcess.h" +#if DEBUG_ENABLED + #include +#endif + /****************************************************************************** CTOR/DTOR ******************************************************************************/ diff --git a/src/ArduinoIoTCloudTCP.cpp b/src/ArduinoIoTCloudTCP.cpp index 23bd9a95..238c93e3 100644 --- a/src/ArduinoIoTCloudTCP.cpp +++ b/src/ArduinoIoTCloudTCP.cpp @@ -80,6 +80,7 @@ ArduinoIoTCloudTCP::ArduinoIoTCloudTCP() * PUBLIC MEMBER FUNCTIONS ******************************************************************************/ +#if CONNECTION_HANDLER_ENABLED int ArduinoIoTCloudTCP::begin(ConnectionHandler& connection, bool const enableWatchdog, String brokerAddress, uint16_t brokerPort, bool autoReconnect) { _connection = &connection; @@ -89,6 +90,7 @@ int ArduinoIoTCloudTCP::begin(ConnectionHandler& connection, bool const enableWa return begin(_connection->getClient(), _connection->getUDP(), enableWatchdog, brokerAddress, brokerPort, autoReconnect); #endif } +#endif int ArduinoIoTCloudTCP::begin(Client& brokerClient, Client& otaClient, UDP& ntpClient, bool const enableWatchdog, String brokerAddress, uint16_t brokerPort, bool autoReconnect) { @@ -292,14 +294,15 @@ int ArduinoIoTCloudTCP::begin(bool const enableWatchdog, String brokerAddress, u _ota.setAuthentication(getDeviceId().c_str(), _password.c_str()); #endif // OTA_ENABLED && !defined(OFFLOADED_DOWNLOAD) && defined(OTA_BASIC_AUTH) -#ifdef BOARD_HAS_OFFLOADED_ECCX08 + +#if defined(BOARD_HAS_OFFLOADED_ECCX08) && defined(CONNECTION_HANDLER_ENABLED) && (CONNECTION_HANDLER_ENABLED == 1) if (String(WiFi.firmwareVersion()) < String("1.6.0")) { DEBUG_ERROR("ArduinoIoTCloudTCP::%s In order to connect to Arduino IoT Cloud, NINA firmware needs to be >= 1.6.0, current %s", __FUNCTION__, WiFi.firmwareVersion()); return 0; } #endif /* BOARD_HAS_OFFLOADED_ECCX08 */ -#if defined (ARDUINO_UNOWIFIR4) +#if defined(ARDUINO_UNOWIFIR4) && defined(CONNECTION_HANDLER_ENABLED) && (CONNECTION_HANDLER_ENABLED == 1) if (String(WiFi.firmwareVersion()) < String("0.2.0")) { DEBUG_ERROR("ArduinoIoTCloudTCP::%s In order to connect to Arduino IoT Cloud, WiFi firmware needs to be >= 0.2.0, current %s", __FUNCTION__, WiFi.firmwareVersion()); return 0; @@ -342,8 +345,13 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Init() _otaTLSClient.begin(_otaClient); #endif +#if CONNECTION_HANDLER_ENABLED /* Setup TimeService */ - _time_service.begin(_connection); + if (_connection != nullptr) { + _time_service.begin(_connection); + } else +#endif + _time_service.begin(_ntpClient); /* Since we do not control what code the user inserts * between ArduinoIoTCloudTCP::begin() and the first @@ -354,8 +362,12 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Init() if (_enableWatchdog) { /* Initialize watchdog hardware */ watchdog_enable(); - /* Setup callbacks to feed the watchdog during offloaded network operations (connection/download)*/ - watchdog_enable_network_feed(_connection->getInterface()); +#if CONNECTION_HANDLER_ENABLED + if (_connection != nullptr) { + /* Setup callbacks to feed the watchdog during offloaded network operations (connection/download)*/ + watchdog_enable_network_feed(_connection->getInterface()); + } +#endif } #endif @@ -364,13 +376,20 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Init() ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectPhy() { - if (_connection->check() == NetworkConnectionState::CONNECTED) - { - if (!_connection_attempt.isRetry() || (_connection_attempt.isRetry() && _connection_attempt.isExpired())) - return State::SyncTime; +#if CONNECTION_HANDLER_ENABLED + if (_connection == nullptr) { + return State::SyncTime; } + if (_connection->check() == NetworkConnectionState::CONNECTED) { + if (!_connection_attempt.isRetry() || (_connection_attempt.isRetry() && _connection_attempt.isExpired())) { + return State::SyncTime; + } + } return State::ConnectPhy; +#else + return State::SyncTime; +#endif } ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SyncTime() diff --git a/src/ArduinoIoTCloudTCP.h b/src/ArduinoIoTCloudTCP.h index 9f9670a2..0de85db0 100644 --- a/src/ArduinoIoTCloudTCP.h +++ b/src/ArduinoIoTCloudTCP.h @@ -74,7 +74,9 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass virtual void printDebugInfo() override; virtual void disconnect () override; +#if CONNECTION_HANDLER_ENABLED int begin(ConnectionHandler& connection, bool const enableWatchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS, uint16_t brokerPort = DEFAULT_BROKER_PORT_AUTO, bool autoReconnect = true); +#endif int begin(Client& brokerClient, Client& otaClient, UDP& ntpClient, bool const enableWatchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS, uint16_t brokerPort = DEFAULT_BROKER_PORT_AUTO, bool autoReconnect = true); int begin(Client& brokerClient, UDP& ntpClient, bool const enableWatchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS, uint16_t brokerPort = DEFAULT_BROKER_PORT_AUTO, bool autoReconnect = true); diff --git a/src/ArduinoIoTCloudThing.cpp b/src/ArduinoIoTCloudThing.cpp index a6bb7419..164527e9 100644 --- a/src/ArduinoIoTCloudThing.cpp +++ b/src/ArduinoIoTCloudThing.cpp @@ -22,6 +22,10 @@ #include "property/types/CloudWrapperInt.h" #include "property/types/CloudWrapperUnsignedInt.h" +#if DEBUG_ENABLED + #include +#endif + /****************************************************************************** * CTOR/DTOR ******************************************************************************/ diff --git a/src/ota/implementation/OTANanoRP2040.cpp b/src/ota/implementation/OTANanoRP2040.cpp index c24ebf9e..63eaa3ae 100644 --- a/src/ota/implementation/OTANanoRP2040.cpp +++ b/src/ota/implementation/OTANanoRP2040.cpp @@ -13,7 +13,9 @@ #if defined(ARDUINO_NANO_RP2040_CONNECT) && OTA_ENABLED #include #include "OTANanoRP2040.h" -#include +#if DEBUG_ENABLED + #include +#endif #include "mbed.h" #include "utility/watchdog/Watchdog.h" diff --git a/src/ota/implementation/OTASamd.cpp b/src/ota/implementation/OTASamd.cpp index bc919449..1369dbec 100644 --- a/src/ota/implementation/OTASamd.cpp +++ b/src/ota/implementation/OTASamd.cpp @@ -13,7 +13,9 @@ #if defined(ARDUINO_ARCH_SAMD) && OTA_ENABLED #include "OTASamd.h" -#include +#if DEBUG_ENABLED + #include +#endif #if OTA_STORAGE_SNU # include # include /* WiFiStorage */ diff --git a/src/ota/implementation/OTAUnoR4.cpp b/src/ota/implementation/OTAUnoR4.cpp index 5ba12afa..8fbba304 100644 --- a/src/ota/implementation/OTAUnoR4.cpp +++ b/src/ota/implementation/OTAUnoR4.cpp @@ -13,7 +13,9 @@ #if defined(ARDUINO_UNOR4_WIFI) && OTA_ENABLED #include "OTAUnoR4.h" -#include +#if DEBUG_ENABLED + #include +#endif #include "fsp_common_api.h" #include "r_flash_lp.h" #include "WiFi.h" diff --git a/src/ota/interface/OTAInterface.h b/src/ota/interface/OTAInterface.h index d3d21f0f..cf371fec 100644 --- a/src/ota/interface/OTAInterface.h +++ b/src/ota/interface/OTAInterface.h @@ -21,7 +21,9 @@ #include #include -#include +#if DEBUG_ENABLED + #include +#endif /****************************************************************************** * CLASS DECLARATION diff --git a/src/tls/utility/TLSClientBroker.h b/src/tls/utility/TLSClientBroker.h index 579e00eb..3bfd1261 100644 --- a/src/tls/utility/TLSClientBroker.h +++ b/src/tls/utility/TLSClientBroker.h @@ -10,8 +10,10 @@ #pragma once -#include #include +#if CONNECTION_HANDLER_ENABLED + #include +#endif enum class ArduinoIoTAuthenticationMode { diff --git a/src/tls/utility/TLSClientOta.h b/src/tls/utility/TLSClientOta.h index 497bf3e8..90cd8a07 100644 --- a/src/tls/utility/TLSClientOta.h +++ b/src/tls/utility/TLSClientOta.h @@ -10,8 +10,11 @@ #pragma once -#include #include +#if CONNECTION_HANDLER_ENABLED + #include +#endif + #if defined(BOARD_HAS_OFFLOADED_ECCX08) /* @@ -69,6 +72,7 @@ public: void begin(Client* client); +#if CONNECTION_HANDLER_ENABLED static inline Client& getNewClient(NetworkAdapter net) { switch(net) { #ifdef BOARD_HAS_WIFI @@ -97,4 +101,5 @@ #endif // BOARD_HAS_CELLULAR } } +#endif // CONNECTION_HANDLER_ENABLED }; diff --git a/src/utility/time/TimeService.cpp b/src/utility/time/TimeService.cpp index fb7272fc..1ff60437 100644 --- a/src/utility/time/TimeService.cpp +++ b/src/utility/time/TimeService.cpp @@ -37,6 +37,10 @@ #include "RTC.h" #endif +#if DEBUG_ENABLED + #include +#endif + /************************************************************************************** * GLOBAL VARIABLES **************************************************************************************/ @@ -116,14 +120,17 @@ static time_t const EPOCH = 0; **************************************************************************************/ TimeServiceClass::TimeServiceClass() -: _con_hdl(nullptr) -, _is_rtc_configured(false) +: _is_rtc_configured(false) , _is_tz_configured(false) , _timezone_offset(24 * 60 * 60) , _timezone_dst_until(0) , _last_sync_tick(0) , _sync_interval_ms(TIMESERVICE_NTP_SYNC_TIMEOUT_ms) , _sync_func(nullptr) +#if CONNECTION_HANDLER_ENABLED +, _con_hdl(nullptr) +#endif +, _ntp_client(nullptr) { } @@ -132,9 +139,26 @@ TimeServiceClass::TimeServiceClass() * PUBLIC MEMBER FUNCTIONS **************************************************************************************/ +#if CONNECTION_HANDLER_ENABLED void TimeServiceClass::begin(ConnectionHandler * con_hdl) { _con_hdl = con_hdl; +#ifdef HAS_TCP + begin(&_con_hdl->getUDP()); +#else + begin(); +#endif +} +#endif + +void TimeServiceClass::begin(UDP * ntp_client) +{ + _ntp_client = ntp_client; + begin(); +} + +void TimeServiceClass::begin() +{ initRTC(); #ifdef HAS_LORA setRTC(EPOCH_AT_COMPILE_TIME); @@ -293,11 +317,12 @@ unsigned long TimeServiceClass::getTimeFromString(const String& input) #if defined(HAS_NOTECARD) || defined(HAS_TCP) bool TimeServiceClass::connected() { - if(_con_hdl == nullptr) { - return false; - } else { +#if CONNECTION_HANDLER_ENABLED + if(_con_hdl != nullptr) { return _con_hdl->check() == NetworkConnectionState::CONNECTED; } +#endif + return true; // If no connection handler is used, assume we are connected } unsigned long TimeServiceClass::getRemoteTime() @@ -308,8 +333,14 @@ unsigned long TimeServiceClass::getRemoteTime() * This is the most reliable time source and it will * ensure a correct behaviour of the library. */ - if(_con_hdl->getInterface() != NetworkAdapter::CELL) { - unsigned long const ntp_time = NTPUtils::getTime(_con_hdl->getUDP()); + bool use_ntp = true; +#if CONNECTION_HANDLER_ENABLED + if((_con_hdl != nullptr) && (_con_hdl->getInterface() != NetworkAdapter::CELL)) { + use_ntp = false; + } +#endif + if (use_ntp && (_ntp_client != nullptr)) { + unsigned long const ntp_time = NTPUtils::getTime(*_ntp_client); if(isTimeValid(ntp_time)) { return ntp_time; } @@ -317,16 +348,19 @@ unsigned long TimeServiceClass::getRemoteTime() DEBUG_WARNING("TimeServiceClass::%s cannot get time from NTP, fallback on connection handler", __FUNCTION__); #endif /* HAS_TCP */ +#if CONNECTION_HANDLER_ENABLED /* As fallback if NTP request fails try to obtain the * network time using the connection handler. */ - unsigned long const connection_time = _con_hdl->getTime(); - if(isTimeValid(connection_time)) { - return connection_time; + if (_con_hdl != nullptr) { + unsigned long const connection_time = _con_hdl->getTime(); + if(isTimeValid(connection_time)) { + return connection_time; + } + DEBUG_WARNING("TimeServiceClass::%s cannot get time from connection handler", __FUNCTION__); } - DEBUG_WARNING("TimeServiceClass::%s cannot get time from connection handler", __FUNCTION__); +#endif } - /* Return known invalid value because we are not connected */ return EPOCH; } diff --git a/src/utility/time/TimeService.h b/src/utility/time/TimeService.h index a830aa7a..29efe686 100644 --- a/src/utility/time/TimeService.h +++ b/src/utility/time/TimeService.h @@ -23,7 +23,9 @@ **************************************************************************************/ #include -#include +#if CONNECTION_HANDLER_ENABLED + #include +#endif /****************************************************************************** * TYPEDEF @@ -42,7 +44,11 @@ class TimeServiceClass TimeServiceClass(); - void begin (ConnectionHandler * con_hdl); +#if CONNECTION_HANDLER_ENABLED + void begin(ConnectionHandler * con_hdl); +#endif + void begin(UDP * ntp_client); + void begin(); unsigned long getTime(); void setTime(unsigned long time); unsigned long getLocalTime(); @@ -59,7 +65,7 @@ class TimeServiceClass static bool isTimeValid(unsigned long const time); private: - ConnectionHandler * _con_hdl; + bool _is_rtc_configured; bool _is_tz_configured; long _timezone_offset; @@ -67,6 +73,10 @@ class TimeServiceClass unsigned long _last_sync_tick; unsigned long _sync_interval_ms; syncTimeFunctionPtr _sync_func; +#if CONNECTION_HANDLER_ENABLED + ConnectionHandler * _con_hdl; +#endif + UDP * _ntp_client; #if defined(HAS_NOTECARD) || defined(HAS_TCP) unsigned long getRemoteTime(); diff --git a/src/utility/watchdog/Watchdog.cpp b/src/utility/watchdog/Watchdog.cpp index d4723406..a9b8ffdc 100644 --- a/src/utility/watchdog/Watchdog.cpp +++ b/src/utility/watchdog/Watchdog.cpp @@ -23,8 +23,8 @@ #include -#if defined(DEBUG_ERROR) || defined(DEBUG_WARNING) || defined(DEBUG_INFO) || defined(DEBUG_DEBUG) || defined(DEBUG_VERBOSE) -# include +#if DEBUG_ENABLED + #include #endif #ifdef ARDUINO_ARCH_SAMD @@ -108,6 +108,7 @@ static void mbed_watchdog_reset() } } +#if CONNECTION_HANDLER_ENABLED static void mbed_watchdog_enable_network_feed(NetworkAdapter ni) { if (ni == NetworkAdapter::ETHERNET) { @@ -128,6 +129,7 @@ static void mbed_watchdog_enable_network_feed(NetworkAdapter ni) #endif } } +#endif /* (CONNECTION_HANDLER_ENABLED) */ #endif /* ARDUINO_ARCH_MBED */ #if defined (ARDUINO_ARCH_SAMD) || defined (ARDUINO_ARCH_MBED) @@ -149,6 +151,7 @@ void watchdog_reset() #endif } +#if CONNECTION_HANDLER_ENABLED void watchdog_enable_network_feed(NetworkAdapter ni) { /* Setup WiFi NINA watchdog feed callback function */ @@ -162,4 +165,5 @@ void watchdog_enable_network_feed(NetworkAdapter ni) mbed_watchdog_enable_network_feed(ni); #endif } +#endif /* (CONNECTION_HANDLER_ENABLED) */ #endif /* (ARDUINO_ARCH_SAMD) || (ARDUINO_ARCH_MBED) */ diff --git a/src/utility/watchdog/Watchdog.h b/src/utility/watchdog/Watchdog.h index 2ba43a87..aa4efd95 100644 --- a/src/utility/watchdog/Watchdog.h +++ b/src/utility/watchdog/Watchdog.h @@ -22,7 +22,10 @@ * INCLUDE ******************************************************************************/ -#include +#include +#if CONNECTION_HANDLER_ENABLED + #include +#endif /****************************************************************************** * FUNCTION DECLARATION @@ -31,7 +34,9 @@ #if defined (ARDUINO_ARCH_SAMD) || defined (ARDUINO_ARCH_MBED) void watchdog_enable(); void watchdog_reset(); +#if CONNECTION_HANDLER_ENABLED void watchdog_enable_network_feed(NetworkAdapter ni); +#endif #endif /* (ARDUINO_ARCH_SAMD) || (ARDUINO_ARCH_MBED) */ #endif /* ARDUINO_AIOTC_UTILITY_WATCHDOG_H_ */ From 4d6be98e02bdc0ad20e43ad84e40313b9b4a4da3 Mon Sep 17 00:00:00 2001 From: pennam Date: Thu, 5 Jun 2025 17:03:14 +0200 Subject: [PATCH 8/9] add example using WiFi Client --- .../ArduinoIoTCloud-WiFiClient.ino | 73 +++++++++++++++++++ .../arduino_secrets.h | 9 +++ .../thingProperties.h | 28 +++++++ 3 files changed, 110 insertions(+) create mode 100644 examples/ArduinoIoTCloud-WiFiClient/ArduinoIoTCloud-WiFiClient.ino create mode 100644 examples/ArduinoIoTCloud-WiFiClient/arduino_secrets.h create mode 100644 examples/ArduinoIoTCloud-WiFiClient/thingProperties.h diff --git a/examples/ArduinoIoTCloud-WiFiClient/ArduinoIoTCloud-WiFiClient.ino b/examples/ArduinoIoTCloud-WiFiClient/ArduinoIoTCloud-WiFiClient.ino new file mode 100644 index 00000000..4f7f7267 --- /dev/null +++ b/examples/ArduinoIoTCloud-WiFiClient/ArduinoIoTCloud-WiFiClient.ino @@ -0,0 +1,73 @@ +/* + This sketch demonstrates how to use Arduino IoT Cloud without ArduinoConnectionHandler + + * Connect a potentiometer (or other analog sensor) to A0. + * When the potentiometer (or sensor) value changes the data is sent to the Cloud. + * When you flip the switch in the Cloud dashboard the onboard LED lights gets turned ON or OFF. + + IMPORTANT: + This sketch works with only with WiFi. + + The full list of compatible boards can be found here: + - https://github.com/arduino-libraries/ArduinoIoTCloud#what +*/ + +#include +#include "thingProperties.h" + +#if !defined(LED_BUILTIN) && !defined(ARDUINO_NANO_ESP32) +static int const LED_BUILTIN = 2; +#endif + +WiFiClient brokerClient; +WiFiClient otaClient; +WiFiUDP ntpClient; + +void setup() { + /* Initialize serial and wait up to 5 seconds for port to open */ + Serial.begin(9600); + for(unsigned long const serialBeginTime = millis(); !Serial && (millis() - serialBeginTime <= 5000); ) { } + + int status = WL_IDLE_STATUS; + while (status != WL_CONNECTED) { + Serial.print("Attempting to connect to SSID: "); + Serial.println(SECRET_WIFI_SSID); + status = WiFi.begin(SECRET_WIFI_SSID, SECRET_WIFI_PASS); + delay(1000); + } + + /* Set the debug message level: + * - DBG_ERROR: Only show error messages + * - DBG_WARNING: Show warning and error messages + * - DBG_INFO: Show info, warning, and error messages + * - DBG_DEBUG: Show debug, info, warning, and error messages + * - DBG_VERBOSE: Show all messages + */ + setDebugMessageLevel(DBG_INFO); + + /* Configure LED pin as an output */ + pinMode(LED_BUILTIN, OUTPUT); + + /* This function takes care of connecting your sketch variables to the ArduinoIoTCloud object */ + initProperties(); + + /* Initialize Arduino IoT Cloud library */ + ArduinoCloud.begin(brokerClient, otaClient, ntpClient); + + ArduinoCloud.printDebugInfo(); +} + +void loop() { + ArduinoCloud.update(); + potentiometer = analogRead(A0); + seconds = millis() / 1000; +} + +/* + * 'onLedChange' is called when the "led" property of your Thing changes + */ +void onLedChange() { + Serial.print("LED set to "); + Serial.println(led); + digitalWrite(LED_BUILTIN, led); +} diff --git a/examples/ArduinoIoTCloud-WiFiClient/arduino_secrets.h b/examples/ArduinoIoTCloud-WiFiClient/arduino_secrets.h new file mode 100644 index 00000000..cc61fea2 --- /dev/null +++ b/examples/ArduinoIoTCloud-WiFiClient/arduino_secrets.h @@ -0,0 +1,9 @@ +/* A complete list of supported boards with WiFi is available here: + * https://github.com/arduino-libraries/ArduinoIoTCloud/#what + */ + +#define SECRET_WIFI_SSID "YOUR_WIFI_NETWORK_NAME" +#define SECRET_WIFI_PASS "YOUR_WIFI_PASSWORD" + +/* Configure this if you want to use username/password broker authentication */ +#define SECRET_DEVICE_KEY "my-device-password" diff --git a/examples/ArduinoIoTCloud-WiFiClient/thingProperties.h b/examples/ArduinoIoTCloud-WiFiClient/thingProperties.h new file mode 100644 index 00000000..adedc157 --- /dev/null +++ b/examples/ArduinoIoTCloud-WiFiClient/thingProperties.h @@ -0,0 +1,28 @@ +#include +#include "arduino_secrets.h" + +#if !defined(HAS_TCP) + #error "Please check Arduino IoT Cloud supported boards list: https://github.com/arduino-libraries/ArduinoIoTCloud/#what" +#endif + +#if !defined(BOARD_HAS_SECURE_ELEMENT) + #define BOARD_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" +#endif + +void onLedChange(); + +bool led; +int potentiometer; +int seconds; + +void initProperties() { + ArduinoCloud.addProperty(led, Permission::Write).onUpdate(onLedChange); + ArduinoCloud.addProperty(potentiometer, Permission::Read).publishOnChange(10); + ArduinoCloud.addProperty(seconds, Permission::Read).publishOnChange(1); + +#if !defined(BOARD_HAS_SECURE_ELEMENT) + ArduinoCloud.setBoardId(BOARD_ID); + ArduinoCloud.setSecretDeviceKey(SECRET_DEVICE_KEY); +#endif +} + From d487e8353ca01d4a884280b30c87bc19b1f2a74a Mon Sep 17 00:00:00 2001 From: pennam Date: Fri, 6 Jun 2025 10:58:16 +0200 Subject: [PATCH 9/9] temporary workaround to allow esp32 build --- src/tls/utility/TLSClientOta.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/tls/utility/TLSClientOta.h b/src/tls/utility/TLSClientOta.h index 90cd8a07..5d2c8599 100644 --- a/src/tls/utility/TLSClientOta.h +++ b/src/tls/utility/TLSClientOta.h @@ -73,6 +73,9 @@ void begin(Client* client); #if CONNECTION_HANDLER_ENABLED +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wswitch" +#pragma GCC diagnostic ignored "-Wreturn-type" static inline Client& getNewClient(NetworkAdapter net) { switch(net) { #ifdef BOARD_HAS_WIFI @@ -101,5 +104,6 @@ #endif // BOARD_HAS_CELLULAR } } +#pragma GCC diagnostic pop #endif // CONNECTION_HANDLER_ENABLED };