Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checking received data for correctness and verbose debugging #165

Open
aly-fly opened this issue Feb 16, 2022 · 0 comments
Open

Checking received data for correctness and verbose debugging #165

aly-fly opened this issue Feb 16, 2022 · 0 comments
Labels
topic: code Related to content of the project itself type: enhancement Proposed improvement

Comments

@aly-fly
Copy link

aly-fly commented Feb 16, 2022

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]);
@per1234 per1234 added topic: code Related to content of the project itself type: enhancement Proposed improvement labels Feb 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: code Related to content of the project itself type: enhancement Proposed improvement
Projects
None yet
Development

No branches or pull requests

2 participants