Skip to content

Checking received data for correctness and verbose debugging #165

Open
@aly-fly

Description

@aly-fly

While using this function I came up with a few improvements.
Recommended additions:

- more debug outputs for easier trackingwhat is going on in the background if problems appear

if (timeout > 100) {
  #ifdef DEBUG_NTPClient
    Serial.println("NTP Timeout!");
  #endif
  return false; // timeout after 1000 ms
   .........
this->_udp->read(this->_packetBuffer, NTP_PACKET_SIZE);
#ifdef DEBUG_NTPClient
  Serial.print("NTP Data:");
  char s1[4];
  for (int i = 0; i < NTP_PACKET_SIZE; i++) {
    sprintf(s1, " %02X", _packetBuffer[i]);
    Serial.print(s1);
    }
  Serial.println(".");
#endif

- clearing data buffer before contacting server to have a clean start and easier track errors

 this->sendNTPPacket();
 // clear  buffer before receiving data from server
 memset(this->_packetBuffer, 0, sizeof(_packetBuffer));

- checking NTP protocol version, if incorrect data is received, this should catch it

 unsigned char version = this->_packetBuffer[0];
 version = (version >> 3) & 0x07;
 if (version != 4) {
   #ifdef DEBUG_NTPClient
     Serial.println("Incorrect NTP version!");
   #endif
   return false;
   }
   unsigned long highWord = word(this->_packetBuffer[40], this->_packetBuffer[41]);

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: codeRelated to content of the project itselftype: enhancementProposed improvement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions