From 5621f6dbe56288d530f1211fe053d1c845de99bf Mon Sep 17 00:00:00 2001 From: Noah Laptop Date: Fri, 14 Jun 2019 11:43:06 -0700 Subject: [PATCH] Fixed some issues when the ethernet connection dropped, switched ntp server to pool.ntp.org --- src/InternetPlats/Loom_InternetEthernet.cpp | 5 +-- src/Loom_NTP_Sync.cpp | 47 +++++++++++---------- src/PublishPlats/Loom_PublishPlat.cpp | 3 +- 3 files changed, 27 insertions(+), 28 deletions(-) diff --git a/src/InternetPlats/Loom_InternetEthernet.cpp b/src/InternetPlats/Loom_InternetEthernet.cpp index ec65b3284..0a0ba1211 100644 --- a/src/InternetPlats/Loom_InternetEthernet.cpp +++ b/src/InternetPlats/Loom_InternetEthernet.cpp @@ -122,9 +122,8 @@ Client& Loom_Ethernet_I::http_request(const char* domain, const char* url, const ///////////////////////////////////////////////////////////////////// constexpr unsigned int localPort = 8888; // Local port to listen for UDP packets on -constexpr char time_server[] = "time.nist.gov"; // time.nist.gov NTP server +constexpr char time_server[] = "pool.ntp.org"; // pool.ntp.org NTP server constexpr int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message -// byte packet_buffer[NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets void print_unix_time(unsigned long epoch) { @@ -165,7 +164,7 @@ uint32_t Loom_Ethernet_I::get_time() unsigned long epoch = 0; // wait to see if a reply is available - delay(2000); + delay(1000); print_module_label(); if (m_UDP.parsePacket()) { // We've received a packet, read the data from it diff --git a/src/Loom_NTP_Sync.cpp b/src/Loom_NTP_Sync.cpp index f19cfcccd..1f907e46b 100644 --- a/src/Loom_NTP_Sync.cpp +++ b/src/Loom_NTP_Sync.cpp @@ -68,31 +68,32 @@ void LoomNTPSync::print_state() { ///////////////////////////////////////////////////////////////////// void LoomNTPSync::measure() { - // if periodic sync is requested and the sync time has passed - if (m_last_error == Error::OK - && m_internet->is_connected() - && m_next_sync.unixtime() != 0 - && (m_rtc->now() - m_next_sync).totalseconds() >= 0) { - // synchronize the RTC - DateTime timeNow; - // repeat synchronize if this is the first power on - do { - timeNow = m_sync_rtc(); - if (timeNow.unixtime() != 0) m_next_sync = DateTime(0); - else delay(100); - } while (m_next_sync.unixtime() == 1); - // set the next sync time - if (m_sync_interval != 0) { - // to n hours from now - m_next_sync = DateTime(timeNow) + TimeSpan(0, m_sync_interval, 0, 0); + // if a sync is requested + if (m_next_sync.unixtime() != 0 && (m_rtc->now() - m_next_sync).totalseconds() >= 0) { + // if the engine is operating correctly + if (m_last_error == Error::OK && m_internet->is_connected()) { + // synchronize the RTC + DateTime timeNow; + // repeat synchronize if this is the first power on + do { + timeNow = m_sync_rtc(); + if (timeNow.unixtime() != 0) m_next_sync = DateTime(0); + else delay(100); + } while (m_next_sync.unixtime() == 1); + // set the next sync time + if (m_sync_interval != 0) { + // to n hours from now + m_next_sync = DateTime(timeNow) + TimeSpan(0, m_sync_interval, 0, 0); + } } - } - else { + // else log errors + else { print_module_label(); - if (m_last_error != Error::OK) - LPrint("Could not synchronize RTC due to error ", static_cast(m_last_error), "\n"); - else if (!(m_internet->is_connected())) - LPrint("Could not synchronize RTC due to lack of internet"); + if (m_last_error != Error::OK) + LPrint("Could not synchronize RTC due to error ", static_cast(m_last_error), "\n"); + else if (!(m_internet->is_connected())) + LPrint("Could not synchronize RTC due to lack of internet"); + } } } diff --git a/src/PublishPlats/Loom_PublishPlat.cpp b/src/PublishPlats/Loom_PublishPlat.cpp index e6f0c3de1..e931776f5 100644 --- a/src/PublishPlats/Loom_PublishPlat.cpp +++ b/src/PublishPlats/Loom_PublishPlat.cpp @@ -33,11 +33,10 @@ void LoomPublishPlat::second_stage_ctor() { ///////////////////////////////////////////////////////////////////// bool LoomPublishPlat::publish(const JsonObject json) { // check validity - if(m_internet == nullptr || !(m_internet->is_connected()) || json.isNull()){ + if(m_internet == nullptr || json.isNull()){ print_module_label(); LPrint("Could not publish without "); if(m_internet == nullptr) LPrint("internet module\n"); - else if (!(m_internet->is_connected())) LPrint("internet connectivity.\n"); else LPrint("valid data.\n"); return false; }