From 49d58aefb51598560f0f32ffd812d057096f4caa Mon Sep 17 00:00:00 2001 From: pennam Date: Thu, 11 Jan 2024 09:14:16 +0100 Subject: [PATCH 1/5] Make NTPUtils work with an UDP pointer instead of reference --- src/utility/time/NTPUtils.cpp | 22 +++++++++++----------- src/utility/time/NTPUtils.h | 4 ++-- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/utility/time/NTPUtils.cpp b/src/utility/time/NTPUtils.cpp index 24c626a7..2a5f231d 100644 --- a/src/utility/time/NTPUtils.cpp +++ b/src/utility/time/NTPUtils.cpp @@ -33,12 +33,12 @@ * PUBLIC MEMBER FUNCTIONS **************************************************************************************/ -unsigned long NTPUtils::getTime(UDP & udp) +unsigned long NTPUtils::getTime(UDP * udp) { #ifdef NTP_USE_RANDOM_PORT - udp.begin(NTPUtils::getRandomPort(MIN_NTP_PORT, MAX_NTP_PORT)); + udp->begin(NTPUtils::getRandomPort(MIN_NTP_PORT, MAX_NTP_PORT)); #else - udp.begin(NTP_LOCAL_PORT); + udp->begin(NTP_LOCAL_PORT); #endif sendNTPpacket(udp); @@ -48,16 +48,16 @@ unsigned long NTPUtils::getTime(UDP & udp) do { is_timeout = (millis() - start) >= NTP_TIMEOUT_MS; - } while(!is_timeout && !udp.parsePacket()); + } while(!is_timeout && !udp->parsePacket()); if(is_timeout) { - udp.stop(); + udp->stop(); return 0; } uint8_t ntp_packet_buf[NTP_PACKET_SIZE]; - udp.read(ntp_packet_buf, NTP_PACKET_SIZE); - udp.stop(); + udp->read(ntp_packet_buf, NTP_PACKET_SIZE); + udp->stop(); unsigned long const highWord = word(ntp_packet_buf[40], ntp_packet_buf[41]); unsigned long const lowWord = word(ntp_packet_buf[42], ntp_packet_buf[43]); @@ -72,7 +72,7 @@ unsigned long NTPUtils::getTime(UDP & udp) * PRIVATE MEMBER FUNCTIONS **************************************************************************************/ -void NTPUtils::sendNTPpacket(UDP & udp) +void NTPUtils::sendNTPpacket(UDP * udp) { uint8_t ntp_packet_buf[NTP_PACKET_SIZE] = {0}; @@ -85,9 +85,9 @@ void NTPUtils::sendNTPpacket(UDP & udp) ntp_packet_buf[14] = 49; ntp_packet_buf[15] = 52; - udp.beginPacket(NTP_TIME_SERVER, NTP_TIME_SERVER_PORT); - udp.write(ntp_packet_buf, NTP_PACKET_SIZE); - udp.endPacket(); + udp->beginPacket(NTP_TIME_SERVER, NTP_TIME_SERVER_PORT); + udp->write(ntp_packet_buf, NTP_PACKET_SIZE); + udp->endPacket(); } int NTPUtils::getRandomPort(int const min_port, int const max_port) diff --git a/src/utility/time/NTPUtils.h b/src/utility/time/NTPUtils.h index 6337015a..8c3dec09 100644 --- a/src/utility/time/NTPUtils.h +++ b/src/utility/time/NTPUtils.h @@ -41,7 +41,7 @@ class NTPUtils { public: - static unsigned long getTime(UDP & udp); + static unsigned long getTime(UDP * udp); static int getRandomPort(int const min_port, int const max_port); private: @@ -56,7 +56,7 @@ class NTPUtils static unsigned long const NTP_TIMEOUT_MS = 1000; static constexpr const char * NTP_TIME_SERVER = "time.arduino.cc"; - static void sendNTPpacket(UDP & udp); + static void sendNTPpacket(UDP * udp); }; #endif /* #ifndef HAS_LORA */ From 2ea6f62743bc7b4ad03491f92d5bad1272c7d228 Mon Sep 17 00:00:00 2001 From: pennam Date: Thu, 11 Jan 2024 09:15:54 +0100 Subject: [PATCH 2/5] Make TimeService work without ConnectionHandler - Removing fallback for getTime it only needs an UDP reference to run NTP requests --- src/utility/time/TimeService.cpp | 48 +++++++++++++------------------- src/utility/time/TimeService.h | 6 ++-- 2 files changed, 22 insertions(+), 32 deletions(-) diff --git a/src/utility/time/TimeService.cpp b/src/utility/time/TimeService.cpp index cc387215..9b3a19b2 100644 --- a/src/utility/time/TimeService.cpp +++ b/src/utility/time/TimeService.cpp @@ -109,7 +109,7 @@ static time_t const EPOCH = 0; **************************************************************************************/ TimeServiceClass::TimeServiceClass() -: _con_hdl(nullptr) +: _udp(nullptr) , _is_rtc_configured(false) , _is_tz_configured(false) , _timezone_offset(24 * 60 * 60) @@ -125,13 +125,14 @@ TimeServiceClass::TimeServiceClass() * PUBLIC MEMBER FUNCTIONS **************************************************************************************/ -void TimeServiceClass::begin(ConnectionHandler * con_hdl) +void TimeServiceClass::begin(UDP & udp) { - _con_hdl = con_hdl; initRTC(); #ifdef HAS_LORA setRTC(EPOCH_AT_COMPILE_TIME); #endif + + _udp = &udp; } unsigned long TimeServiceClass::getTime() @@ -179,6 +180,14 @@ bool TimeServiceClass::sync() return _is_rtc_configured; } +bool TimeServiceClass::isTimeValid() +{ + if (_is_rtc_configured) { + return isTimeValid(getRTC()); + } + return false; +} + void TimeServiceClass::setSyncInterval(unsigned long seconds) { _sync_interval_ms = seconds * 1000; @@ -276,34 +285,15 @@ unsigned long TimeServiceClass::getTimeFromString(const String& input) **************************************************************************************/ #ifdef HAS_TCP -bool TimeServiceClass::connected() -{ - if(_con_hdl == nullptr) { - return false; - } else { - return _con_hdl->getStatus() == NetworkConnectionState::CONNECTED; - } -} - unsigned long TimeServiceClass::getRemoteTime() { - if(connected()) { - /* At first try to obtain a valid time via NTP. - * This is the most reliable time source and it will - * ensure a correct behaviour of the library. - */ - unsigned long const ntp_time = NTPUtils::getTime(_con_hdl->getUDP()); - if(isTimeValid(ntp_time)) { - return ntp_time; - } - - /* 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; - } + /* At first try to obtain a valid time via NTP. + * This is the most reliable time source and it will + * ensure a correct behaviour of the library. + */ + unsigned long const ntp_time = NTPUtils::getTime(_udp); + if(isTimeValid(ntp_time)) { + return ntp_time; } /* Return the epoch timestamp at compile time as a last diff --git a/src/utility/time/TimeService.h b/src/utility/time/TimeService.h index 6e6af6bb..8961768b 100644 --- a/src/utility/time/TimeService.h +++ b/src/utility/time/TimeService.h @@ -42,12 +42,13 @@ class TimeServiceClass TimeServiceClass(); - void begin (ConnectionHandler * con_hdl); + void begin (UDP & udp); unsigned long getTime(); void setTime(unsigned long time); unsigned long getLocalTime(); void setTimeZoneData(long offset, unsigned long valid_until); bool sync(); + bool isTimeValid(); void setSyncInterval(unsigned long seconds); void setSyncFunction(syncTimeFunctionPtr sync_func); @@ -58,7 +59,7 @@ class TimeServiceClass private: - ConnectionHandler * _con_hdl; + UDP * _udp; bool _is_rtc_configured; bool _is_tz_configured; long _timezone_offset; @@ -69,7 +70,6 @@ class TimeServiceClass #ifdef HAS_TCP unsigned long getRemoteTime(); - bool connected(); #endif void initRTC(); void setRTC(unsigned long time); From 1ff31708c06fd77fc2b57c9331adc35ac7aa3adb Mon Sep 17 00:00:00 2001 From: pennam Date: Thu, 11 Jan 2024 10:31:14 +0100 Subject: [PATCH 3/5] Add constructor and code to make ArduinoCloud work without ConnectionHandler --- src/ArduinoIoTCloud.cpp | 2 ++ src/ArduinoIoTCloud.h | 4 ++++ src/ArduinoIoTCloudTCP.cpp | 48 ++++++++++++++++++++++++++------------ src/ArduinoIoTCloudTCP.h | 6 ++++- 4 files changed, 44 insertions(+), 16 deletions(-) diff --git a/src/ArduinoIoTCloud.cpp b/src/ArduinoIoTCloud.cpp index 4997ec9b..287a159a 100644 --- a/src/ArduinoIoTCloud.cpp +++ b/src/ArduinoIoTCloud.cpp @@ -27,6 +27,8 @@ ArduinoIoTCloudClass::ArduinoIoTCloudClass() : _connection{nullptr} +, _client{nullptr} +, _adapter{NetworkAdapter::WIFI} , _last_checked_property_index{0} , _time_service(TimeService) , _tz_offset{0} diff --git a/src/ArduinoIoTCloud.h b/src/ArduinoIoTCloud.h index bce662eb..3b571e3a 100644 --- a/src/ArduinoIoTCloud.h +++ b/src/ArduinoIoTCloud.h @@ -103,6 +103,8 @@ class ArduinoIoTCloudClass inline bool deviceNotAttached() { return _thing_id == ""; } inline ConnectionHandler * getConnection() { return _connection; } + inline Client * getClient() { return _client; } + inline unsigned long getInternalTime() { return _time_service.getTime(); } inline unsigned long getLocalTime() { return _time_service.getLocalTime(); } @@ -153,6 +155,8 @@ class ArduinoIoTCloudClass protected: ConnectionHandler * _connection; + Client * _client; + NetworkAdapter _adapter; PropertyContainer _device_property_container; PropertyContainer _thing_property_container; unsigned int _last_checked_property_index; diff --git a/src/ArduinoIoTCloudTCP.cpp b/src/ArduinoIoTCloudTCP.cpp index 9e018d60..b783ace3 100644 --- a/src/ArduinoIoTCloudTCP.cpp +++ b/src/ArduinoIoTCloudTCP.cpp @@ -121,14 +121,22 @@ ArduinoIoTCloudTCP::ArduinoIoTCloudTCP() int ArduinoIoTCloudTCP::begin(ConnectionHandler & connection, bool const enable_watchdog, String brokerAddress, uint16_t brokerPort) { _connection = &connection; - _brokerAddress = brokerAddress; - _brokerPort = brokerPort; - _time_service.begin(&connection); - return begin(enable_watchdog, _brokerAddress, _brokerPort); + + return begin(_connection->getClient(), _connection->getUDP(), _connection->getInterface(), enable_watchdog, brokerAddress, brokerPort); +} + +int ArduinoIoTCloudTCP::begin(Client & client, UDP & udp, NetworkAdapter adapter, bool const enable_watchdog, String brokerAddress, uint16_t brokerPort) +{ + _adapter = adapter; + _time_service.begin(udp); + + return begin(client, enable_watchdog, brokerAddress, brokerPort); } -int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress, uint16_t brokerPort) +int ArduinoIoTCloudTCP::begin(Client & client, bool const enable_watchdog, String brokerAddress, uint16_t brokerPort) { + _client = &client; + _brokerAddress = brokerAddress; _brokerPort = brokerPort; @@ -160,9 +168,9 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress, #endif #if defined(BOARD_HAS_ECCX08) - _sslClient.setClient(_connection->getClient()); + _sslClient.setClient(client); #elif defined(ARDUINO_PORTENTA_C33) - _sslClient.setClient(_connection->getClient()); + _sslClient.setClient(client); _sslClient.setCACert(AIoTSSCert); #elif defined(BOARD_HAS_SE050) _sslClient.appendCustomCACert(AIoTSSCert); @@ -234,7 +242,7 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress, #if defined (ARDUINO_ARCH_SAMD) || defined (ARDUINO_ARCH_MBED) if (enable_watchdog) { watchdog_enable(); - bool const use_ethernet = _connection->getInterface() == NetworkAdapter::ETHERNET ? true : false; + bool const use_ethernet = _adapter == NetworkAdapter::ETHERNET ? true : false; watchdog_enable_network_feed(use_ethernet); } #endif @@ -301,6 +309,11 @@ void ArduinoIoTCloudTCP::printDebugInfo() ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectPhy() { + if (_connection == nullptr) + { + return State::SyncTime; + } + if (_connection->check() == NetworkConnectionState::CONNECTED) { bool const is_retry_attempt = (_last_connection_attempt_cnt > 0); @@ -313,12 +326,17 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectPhy() ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SyncTime() { -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wunused-variable" - unsigned long const internal_posix_time = _time_service.getTime(); -#pragma GCC diagnostic pop - DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s internal clock configured to posix timestamp %d", __FUNCTION__, internal_posix_time); - return State::ConnectMqttBroker; + _time_service.sync(); + Serial.println("_time_service.sync()"); + + if (_time_service.isTimeValid()) + { + DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s internal clock configured to posix timestamp %d", __FUNCTION__, _time_service.getTime()); + return State::ConnectMqttBroker; + } + + // TODO: handle retry delay + return State::SyncTime; } ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectMqttBroker() @@ -585,7 +603,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected() /* Transmit the cleared request flags to the cloud. */ sendDevicePropertyToCloud("OTA_REQ"); /* Call member function to handle OTA request. */ - _ota_error = OTA::onRequest(_ota_url, _connection->getInterface()); + _ota_error = OTA::onRequest(_ota_url, _adapter); /* If something fails send the OTA error to the cloud */ sendDevicePropertyToCloud("OTA_ERROR"); } diff --git a/src/ArduinoIoTCloudTCP.h b/src/ArduinoIoTCloudTCP.h index e50f9a07..95b49df6 100644 --- a/src/ArduinoIoTCloudTCP.h +++ b/src/ArduinoIoTCloudTCP.h @@ -81,10 +81,12 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass #if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08) || defined(BOARD_HAS_SE050) int begin(ConnectionHandler & connection, bool const enable_watchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS_SECURE_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_SECURE_AUTH); + int begin(Client & client, UDP & udp, NetworkAdapter adapter = NetworkAdapter::WIFI, bool const enable_watchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS_SECURE_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_SECURE_AUTH); #else int begin(ConnectionHandler & connection, bool const enable_watchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS_USER_PASS_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_USER_PASS_AUTH); + int begin(Client & client, UDP & udp, NetworkAdapter adapter = NetworkAdapter::WIFI, bool const enable_watchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS_USER_PASS_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_USER_PASS_AUTH); #endif - int begin(bool const enable_watchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS_SECURE_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_SECURE_AUTH); + #ifdef BOARD_HAS_SECRET_KEY inline void setBoardId (String const device_id) { setDeviceId(device_id); } @@ -189,6 +191,8 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass onOTARequestCallbackFunc _get_ota_confirmation; #endif /* OTA_ENABLED */ + int begin(Client & client, bool const enable_watchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS_SECURE_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_SECURE_AUTH); + inline String getTopic_deviceout() { return String("/a/d/" + getDeviceId() + "/e/o");} inline String getTopic_devicein () { return String("/a/d/" + getDeviceId() + "/e/i");} inline String getTopic_shadowout() { return ( getThingId().length() == 0) ? String("") : String("/a/t/" + getThingId() + "/shadow/o"); } From 1158d66885ef66a0ff679e6447b772b53088d6dc Mon Sep 17 00:00:00 2001 From: pennam Date: Thu, 11 Jan 2024 11:42:19 +0100 Subject: [PATCH 4/5] Add example --- .../ArduinoIoTCloud-Client.ino | 65 +++++++++++++++++++ .../ArduinoIoTCloud-Client/arduino_secrets.h | 45 +++++++++++++ .../ArduinoIoTCloud-Client/thingProperties.h | 36 ++++++++++ 3 files changed, 146 insertions(+) create mode 100644 examples/ArduinoIoTCloud-Client/ArduinoIoTCloud-Client.ino create mode 100644 examples/ArduinoIoTCloud-Client/arduino_secrets.h create mode 100644 examples/ArduinoIoTCloud-Client/thingProperties.h diff --git a/examples/ArduinoIoTCloud-Client/ArduinoIoTCloud-Client.ino b/examples/ArduinoIoTCloud-Client/ArduinoIoTCloud-Client.ino new file mode 100644 index 00000000..c395f770 --- /dev/null +++ b/examples/ArduinoIoTCloud-Client/ArduinoIoTCloud-Client.ino @@ -0,0 +1,65 @@ +/* + This sketch demonstrates how to exchange data between your board and the Arduino IoT Cloud. + + * 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 WiFi, GSM, NB, Ethernet and Lora enabled boards supported by Arduino IoT Cloud. + On a LoRa board, if it is configured as a class A device (default and preferred option), values from Cloud dashboard are received + only after a value is sent to Cloud. + + The full list of compatible boards can be found here: + - https://github.com/arduino-libraries/ArduinoIoTCloud#what +*/ + +#include "arduino_secrets.h" +#include "thingProperties.h" + +#if !defined(LED_BUILTIN) && !defined(ARDUINO_NANO_ESP32) +static int const LED_BUILTIN = 2; +#endif + +void setup() { + /* Initialize serial and wait up to 5 seconds for port to open */ + Serial.begin(9600); + while(!Serial) {} + + /* Configure LED pin as an output */ + pinMode(LED_BUILTIN, OUTPUT); + + /* This function takes care of connecting your sketch variables to the ArduinoIoTCloud object */ + initProperties(); + + Serial.print("Attempting to connect to SSID: "); + Serial.println(SECRET_SSID); + unsigned long start = millis(); + WiFi.begin(SECRET_SSID, SECRET_PASS); + while ((WiFi.status() != WL_CONNECTED) && ((millis() - start) < 3000)) { + delay(100); + } + Serial.println("WiFi connected"); + + /* Initialize Arduino IoT Cloud library */ + ArduinoCloud.begin(client, udp); + + setDebugMessageLevel(DBG_VERBOSE); + ArduinoCloud.printDebugInfo(); +} + +void loop() { + ArduinoCloud.update(); + potentiometer = analogRead(A0); + seconds = millis() / 1000; + delay(100); +} + +/* + * '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-Client/arduino_secrets.h b/examples/ArduinoIoTCloud-Client/arduino_secrets.h new file mode 100644 index 00000000..85d1da0b --- /dev/null +++ b/examples/ArduinoIoTCloud-Client/arduino_secrets.h @@ -0,0 +1,45 @@ +#include +#include + +/* A complete list of supported boards with WiFi is available here: + * https://github.com/arduino-libraries/ArduinoIoTCloud/#what + */ +#if defined(BOARD_HAS_WIFI) + #define SECRET_SSID "" + #define SECRET_PASS "" +#endif + +/* ESP8266 ESP32*/ +#if defined(BOARD_HAS_SECRET_KEY) + #define SECRET_DEVICE_KEY "" +#endif + +/* MKR GSM 1400 */ +#if defined(BOARD_HAS_GSM) + #define SECRET_PIN "" + #define SECRET_APN "" + #define SECRET_LOGIN "" + #define SECRET_PASS "" +#endif + +/* MKR WAN 1300/1310 */ +#if defined(BOARD_HAS_LORA) + #define SECRET_APP_EUI "" + #define SECRET_APP_KEY "" +#endif + +/* MKR NB 1500 */ +#if defined(BOARD_HAS_NB) + #define SECRET_PIN "" + #define SECRET_APN "" + #define SECRET_LOGIN "" + #define SECRET_PASS "" +#endif + +/* Portenta H7 + Ethernet shield */ +#if defined(BOARD_HAS_ETHERNET) + #define SECRET_OPTIONAL_IP "" + #define SECRET_OPTIONAL_DNS "" + #define SECRET_OPTIONAL_GATEWAY "" + #define SECRET_OPTIONAL_NETMASK "" +#endif diff --git a/examples/ArduinoIoTCloud-Client/thingProperties.h b/examples/ArduinoIoTCloud-Client/thingProperties.h new file mode 100644 index 00000000..3921a1a6 --- /dev/null +++ b/examples/ArduinoIoTCloud-Client/thingProperties.h @@ -0,0 +1,36 @@ +#if defined(BOARD_HAS_WIFI) + WiFiClient client; + WiFiUDP udp; +#elif defined(BOARD_HAS_GSM) +#elif defined(BOARD_HAS_LORA) +#elif defined(BOARD_HAS_NB) +#elif defined(BOARD_HAS_ETHERNET) +#else + #error "Please check Arduino IoT Cloud supported boards list: https://github.com/arduino-libraries/ArduinoIoTCloud/#what" +#endif + +#if defined(BOARD_HAS_SECRET_KEY) + #define BOARD_ID "" +#endif + +void onLedChange(); + +bool led; +int potentiometer; +int seconds; + +void initProperties() { +#if defined(BOARD_HAS_SECRET_KEY) + ArduinoCloud.setBoardId(BOARD_ID); + ArduinoCloud.setSecretDeviceKey(SECRET_DEVICE_KEY); +#endif +#if defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_NB) || defined(BOARD_HAS_ETHERNET) + ArduinoCloud.addProperty(led, Permission::Write).onUpdate(onLedChange); + ArduinoCloud.addProperty(potentiometer, Permission::Read).publishOnChange(10); + ArduinoCloud.addProperty(seconds, Permission::Read).publishOnChange(1); +#elif defined(BOARD_HAS_LORA) + ArduinoCloud.addProperty(led, 1, READWRITE, ON_CHANGE, onLedChange); + ArduinoCloud.addProperty(potentiometer, 2, READ, ON_CHANGE); + ArduinoCloud.addProperty(seconds, 3, READ, 5 * MINUTES); +#endif +} From 6abf46bd57713f44a6edb0a6448fcebd3dd9f5d3 Mon Sep 17 00:00:00 2001 From: pennam Date: Thu, 11 Jan 2024 14:34:24 +0100 Subject: [PATCH 5/5] Fix LPWAN build --- src/ArduinoIoTCloud.cpp | 2 ++ src/ArduinoIoTCloud.h | 4 ++++ src/ArduinoIoTCloudLPWAN.cpp | 2 +- src/utility/time/TimeService.cpp | 9 +++++++-- src/utility/time/TimeService.h | 3 ++- 5 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/ArduinoIoTCloud.cpp b/src/ArduinoIoTCloud.cpp index 287a159a..6b605fdf 100644 --- a/src/ArduinoIoTCloud.cpp +++ b/src/ArduinoIoTCloud.cpp @@ -27,7 +27,9 @@ ArduinoIoTCloudClass::ArduinoIoTCloudClass() : _connection{nullptr} +#ifdef HAS_TCP , _client{nullptr} +#endif , _adapter{NetworkAdapter::WIFI} , _last_checked_property_index{0} , _time_service(TimeService) diff --git a/src/ArduinoIoTCloud.h b/src/ArduinoIoTCloud.h index 3b571e3a..9983e664 100644 --- a/src/ArduinoIoTCloud.h +++ b/src/ArduinoIoTCloud.h @@ -103,7 +103,9 @@ class ArduinoIoTCloudClass inline bool deviceNotAttached() { return _thing_id == ""; } inline ConnectionHandler * getConnection() { return _connection; } +#ifdef HAS_TCP inline Client * getClient() { return _client; } +#endif inline unsigned long getInternalTime() { return _time_service.getTime(); } @@ -155,7 +157,9 @@ class ArduinoIoTCloudClass protected: ConnectionHandler * _connection; +#ifdef HAS_TCP Client * _client; +#endif NetworkAdapter _adapter; PropertyContainer _device_property_container; PropertyContainer _thing_property_container; diff --git a/src/ArduinoIoTCloudLPWAN.cpp b/src/ArduinoIoTCloudLPWAN.cpp index 0f96d36d..ed5a9a3d 100644 --- a/src/ArduinoIoTCloudLPWAN.cpp +++ b/src/ArduinoIoTCloudLPWAN.cpp @@ -68,7 +68,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/utility/time/TimeService.cpp b/src/utility/time/TimeService.cpp index 9b3a19b2..907bfcfb 100644 --- a/src/utility/time/TimeService.cpp +++ b/src/utility/time/TimeService.cpp @@ -126,13 +126,18 @@ TimeServiceClass::TimeServiceClass() **************************************************************************************/ void TimeServiceClass::begin(UDP & udp) +{ + _udp = &udp; + + begin(); +} + +void TimeServiceClass::begin() { initRTC(); #ifdef HAS_LORA setRTC(EPOCH_AT_COMPILE_TIME); #endif - - _udp = &udp; } unsigned long TimeServiceClass::getTime() diff --git a/src/utility/time/TimeService.h b/src/utility/time/TimeService.h index 8961768b..3c5584fd 100644 --- a/src/utility/time/TimeService.h +++ b/src/utility/time/TimeService.h @@ -42,7 +42,8 @@ class TimeServiceClass TimeServiceClass(); - void begin (UDP & udp); + void begin(UDP & udp); + void begin(); unsigned long getTime(); void setTime(unsigned long time); unsigned long getLocalTime();