arduino-libraries / NTPClient

Connect to a NTP server
533 stars 366 forks source link

on esp32 with fixed ip, it doesn't work #166

Open Albalate opened 2 years ago

Albalate commented 2 years ago

this code just changing the line WiFi.config(ip, gateway, subnet); stops working.

#include <NTPClient.h>
// change next line to use with another board/shield
//#include <ESP8266WiFi.h>
#include <WiFi.h> // for WiFi shield
//#include <WiFi101.h> // for WiFi 101 shield or MKR1000
#include <WiFiUdp.h>

IPAddress ip(192,168,0,30);     
IPAddress gateway(192,168,0,1);   
IPAddress subnet(255,255,255,0);

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

WiFiUDP ntpUDP;

// You can specify the time server pool and the offset (in seconds, can be
// changed later with setTimeOffset() ). Additionaly you can specify the
// update interval (in milliseconds, can be changed using setUpdateInterval() ).
NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", 3600, 60000);

void setup(){
  Serial.begin(115200);
  WiFi.config(ip, gateway, subnet);
  WiFi.begin(ssid, password);

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

  timeClient.begin();
}

void loop() {
  timeClient.update();

  Serial.println(timeClient.getFormattedTime());

  delay(1000);
}
JyunWei-Su commented 2 years ago

After I replace WiFi.config(ip, gateway, subnet); with WiFi.config(ip, gateway, subnet, dns);, it works. If it still not working, try to change your ntp server.