Cannot connect to NTP server with fixed IP

lesept
Posts: 25
Joined: Wed Jun 27, 2018 10:30 pm

Cannot connect to NTP server with fixed IP

Postby lesept » Mon May 13, 2019 1:31 pm

Hi
I usually use the sketch from here https://lastminuteengineers.com/esp32-n ... -tutorial/ to get the time via NTP. But when I use a fixed IP for my ESP32, the connexion to the NTP server doesn't work anymore.

Here is the modified sketch: the fixed IP part works in any other conditions.

Code: Select all

#include <WiFi.h>
#include "time.h"

const char* ssid       = "YOUR_SSID";
const char* password   = "YOUR_PASS";

// **** These lines added :
//Static IP address configuration
IPAddress staticIP(192, 168, 0, 51); //ESP32 static ip
IPAddress gateway(192, 168, 0, 254); //IP Address of WiFi Router
IPAddress subnet(255, 255, 255, 0);  //Subnet mask
IPAddress dns(192, 168, 0, 254);     //DNS
// *** up to here

const char* ntpServer = "pool.ntp.org";
const long  gmtOffset_sec = 3600;
const int   daylightOffset_sec = 3600;

void printLocalTime()
{
  struct tm timeinfo;
  if (!getLocalTime(&timeinfo)) {
    Serial.println("Failed to obtain time");
    return;
  }
  Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
}

void setup()
{
  Serial.begin(115200);

  //connect to WiFi
  Serial.printf("Connecting to %s ", ssid);

// **** These lines added :
  WiFi.mode(WIFI_OFF);
  WiFi.config(staticIP, dns, gateway, subnet);
  WiFi.mode(WIFI_STA); //WiFi mode station (connect to wifi router only)
  delay(100);
// *** up to here

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println(" CONNECTED");
  Serial.print("WiFi connected - IP address: ");
  Serial.println(WiFi.localIP());

  //init and get the time
  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
  printLocalTime();

  //disconnect WiFi as it's no longer needed
  WiFi.disconnect(true);
  WiFi.mode(WIFI_OFF);
}

void loop()
{
  delay(1000);
  printLocalTime();
}
I get :
Connecting to ***.. CONNECTED
WiFi connected - IP address: 192.168.0.51
Failed to obtain time
What is wrong? Thanks for your help.

User avatar
martinayotte
Posts: 123
Joined: Fri Nov 13, 2015 4:27 pm

Re: Cannot connect to NTP server with fixed IP

Postby martinayotte » Mon May 13, 2019 8:16 pm

lesept wrote:
Mon May 13, 2019 1:31 pm

Code: Select all

IPAddress dns(192, 168, 0, 254);     //DNS
Is this DNS really able to resolve it ?
Is this address of your router ?
Doesn't have external DNS enabled ?
Did you tried using 8.8.8.8 as the DNS ?

boarchuz
Posts: 80
Joined: Tue Aug 21, 2018 5:28 am

Re: Cannot connect to NTP server with fixed IP

Postby boarchuz » Mon May 13, 2019 9:57 pm

Reorder the parameters in config:

Code: Select all

bool WiFiSTAClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1, IPAddress dns2)

lesept
Posts: 25
Joined: Wed Jun 27, 2018 10:30 pm

Re: Cannot connect to NTP server with fixed IP

Postby lesept » Tue May 14, 2019 8:09 am

@martinayotte : Actually, I don't know. How can I find this information?
@boarchuz : I'll try to change the order. Thanks

User avatar
martinayotte
Posts: 123
Joined: Fri Nov 13, 2015 4:27 pm

Re: Cannot connect to NTP server with fixed IP

Postby martinayotte » Tue May 14, 2019 3:15 pm

lesept wrote:
Tue May 14, 2019 8:09 am
@martinayotte : Actually, I don't know. How can I find this information?
If on your PC, the DNS is the same 192.168.0.254, then try to ping "pool.ntp.org" from the PC.
If it is not working there too, then your router is misconfigured.
As I said, try using 8.8.8.8 as DNS, it should work ...

lesept
Posts: 25
Joined: Wed Jun 27, 2018 10:30 pm

Re: Cannot connect to NTP server with fixed IP

Postby lesept » Thu May 16, 2019 12:43 pm

martinayotte wrote:
Tue May 14, 2019 3:15 pm
As I said, try using 8.8.8.8 as DNS, it should work ...
Changed the order of the parameters in the config. Changed the DNS to 8.8.8.8.
It works now, thanks a lot!

Who is online

Users browsing this forum: stefkpl and 9 guests