Skip to content

Commit

Permalink
Fixed some issues when the ethernet connection dropped, switched ntp …
Browse files Browse the repository at this point in the history
…server to pool.ntp.org
  • Loading branch information
prototypicalpro committed Jun 14, 2019
1 parent 5e4d4ec commit 5621f6d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 28 deletions.
5 changes: 2 additions & 3 deletions src/InternetPlats/Loom_InternetEthernet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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
Expand Down
47 changes: 24 additions & 23 deletions src/Loom_NTP_Sync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t>(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<uint8_t>(m_last_error), "\n");
else if (!(m_internet->is_connected()))
LPrint("Could not synchronize RTC due to lack of internet");
}
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/PublishPlats/Loom_PublishPlat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 5621f6d

Please sign in to comment.