Completely removed NTP implementation

This commit is contained in:
cschwinne 2017-02-07 14:24:42 +01:00
parent 369c00bbff
commit 70dfdfc814
2 changed files with 8 additions and 94 deletions

View File

@ -82,10 +82,10 @@ boolean nightlightFade = true;
uint16_t udpPort = 21324;
uint8_t effectDefault = 0;
uint8_t effectSpeedDefault = 75;
//NTP stuff
boolean ntpEnabled = false;
const char* ntpServerName = "time.nist.gov";
long ntpRetryMs = 9600;
long ntpResyncMs = 72000000L;
//overlay stuff
int overlayMin = 0, overlayMax = 9;
int analogClock12pixel = 25;
boolean analogClockSecondsTrail = false;
@ -122,12 +122,9 @@ uint8_t effectCurrent = 0;
uint8_t effectSpeed = 75;
boolean udpConnected = false;
byte udpIn[LEDCOUNT*4+2];
IPAddress ntpIp;
byte ntpBuffer[48];
boolean ntpConnected = false;
boolean ntpSyncNeeded = true;
boolean ntpPacketSent = false;
long ntpPacketSentTime, ntpSyncTime;
//NTP stuff
//overlay stuff
uint8_t overlayCurrent = 0;
long overlayRefreshMs = 200;
long overlayRefreshedTime;
@ -229,7 +226,7 @@ void loop() {
}
lastWifiState = WiFi.status();
DEBUG_PRINT("Wifi state: "); DEBUG_PRINTLN(wifiStateChangedTime);
DEBUG_PRINT("NTP sync needed: "); DEBUG_PRINTLN(ntpSyncNeeded);
//DEBUG_PRINT("NTP sync needed: "); DEBUG_PRINTLN(ntpSyncNeeded);
DEBUG_PRINT("Client IP: "); DEBUG_PRINTLN(WiFi.localIP());
}
#endif

View File

@ -4,90 +4,7 @@
void handleNetworkTime()
{
if (ntpEnabled && udpConnected)
{
if (ntpSyncNeeded)
{
if (ntpPacketSent)
{
if (getNtpTime())
{
ntpSyncNeeded = false;
ntpPacketSent = false;
ntpSyncTime = millis();
DEBUG_PRINT("Time: ");
DEBUG_PRINTLN(now());
} else
{
if (millis() - ntpPacketSentTime > ntpRetryMs)
{
ntpPacketSent = false; //try new packet
}
}
} else
{
WiFi.hostByName(ntpServerName, ntpIp);
sendNTPpacket();
ntpPacketSent = true;
ntpPacketSentTime = millis();
}
} else if (millis() - ntpSyncTime > ntpResyncMs)
{
ntpSyncNeeded = true;
}
}
}
bool getNtpTime()
{
if (notifierUdp.parsePacket()) {
notifierUdp.read(ntpBuffer, 48); // read packet into the buffer
#ifdef DEBUG
int i= 0;
while (i < 48)
{
Serial.print(ntpBuffer[i], HEX);
Serial.print(".");
i++;
if ((i % 4) ==0) Serial.println();
}
#endif
if (ntpBuffer[40] == 0 && ntpBuffer[41] == 0 && ntpBuffer[42] == 0 && ntpBuffer[43] == 0)
{
DEBUG_PRINTLN("Bad NTP response!");
return false;
}
unsigned long secsSince1900;
// convert four bytes starting at location 40 to a long integer
secsSince1900 = (unsigned long)ntpBuffer[40] << 24;
secsSince1900 |= (unsigned long)ntpBuffer[41] << 16;
secsSince1900 |= (unsigned long)ntpBuffer[42] << 8;
secsSince1900 |= (unsigned long)ntpBuffer[43];
setTime(secsSince1900 - 2208988800UL + (millis() - ntpPacketSentTime)/2000); //naive approach to improve accuracy, utc
return true;
}
return false; //unable to get the time
}
void sendNTPpacket()
{
while (notifierUdp.parsePacket()>0);
notifierUdp.flush(); //discard old packets
DEBUG_PRINTLN("Sending NTP packet");
memset(ntpBuffer, 0, 48);
ntpBuffer[0] = 0b11100011; // LI, Version, Mode
ntpBuffer[1] = 0; // Stratum, or type of clock
ntpBuffer[2] = 6; // Polling Interval
ntpBuffer[3] = 0xEC; // Peer Clock Precision
ntpBuffer[12] = 49;
ntpBuffer[13] = 0x4E;
ntpBuffer[14] = 49;
ntpBuffer[15] = 52;
notifierUdp.beginPacket(ntpIp, 123); //NTP requests are to port 123
notifierUdp.write(ntpBuffer, 48);
notifierUdp.endPacket();
}
String getTimeString()