arduino-libraries / NTPClient

Connect to a NTP server
542 stars 372 forks source link

Problem with timeClient.update() if you use also WiFi.config() in your sketch. #108

Closed CSpyridakis closed 4 years ago

CSpyridakis commented 4 years ago

If I try to give a static IP by using WiFi.config() timeClient could not be updated. Whenever I comment out this line everything works as expected.

Details

Sample code

#include "NTPClient.h"
#include <WiFiUdp.h>
#include <ESP8266WiFi.h>

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org");

const char *ssid     = "<SSID>";
const char *password = "<PASSWORD>";

void setup() {
  Serial.begin(115200);
  Serial.println("\nWelcome!");
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  IPAddress ip(192, 168, 1, 101);
  IPAddress gateway(192, 168, 1, 254);
  IPAddress subnet(255, 255, 255, 0);
  WiFi.config(ip, gateway, subnet);

  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }

  timeClient.begin();
  while (!timeClient.update()) {
    timeClient.forceUpdate();
    Serial.println("Time update Failed!");
    delay(1000);
  }

  Serial.println("Time updated!");
}

void loop() {
}
CSpyridakis commented 4 years ago

I had to also specify the DNS server.

Sample

    IPAddress ip(192, 168, 1, X);
    IPAddress dns( X, X, X, X );
    IPAddress gateway(192, 168, 1, X);
    IPAddress subnet(255, 255, 255, 0);
    WiFi.config(ip, dns, gateway, subnet);

I am really sorry for the unnecessary issue!