Cannot connect to NTP server with fixed IP

lesept
Posts: 31
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: 141
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: 559
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: 31
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: 141
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: 31
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!

SlemiELK
Posts: 1
Joined: Mon Jul 01, 2019 4:54 pm

Re: Cannot connect to NTP server with fixed IP

Postby SlemiELK » Mon Jul 01, 2019 4:57 pm

Hi!

I also had problems with this. The following code solved it:
IPAddress ip(*,*,*,*);
IPAddress gateway(*,*,*,*,);
IPAddress subnet(*,*,*,*);
IPAddress dns1(*,*,*,*);
IPAddress dns2(*,*,*,*);
WiFi.config(ip, gateway, subnet, dns1, dns2); //This is important! Include dns1 and dns2 when configuring!
enter your configuration where there are *
Regards!

Viafx24
Posts: 1
Joined: Sat Jul 09, 2022 10:34 pm

Re: Cannot connect to NTP server with fixed IP

Postby Viafx24 » Sat Jul 09, 2022 10:50 pm

Agree with the last comment but i did not need to add two dns (dns1 and dns2). Only one dns. (Dns appears to be a thing needed to allow the esp to go on internet and looks to be often the same than the gateway; at least in my case it is the same i.e 192.168.1.1). THUS, the MAIN POINT was to put dns at the end of the parameters of Wifi.config. It looks like the order matters. I dont know why but when dns was placed just after staticIP, it failed to work for me. Cheers.

Working code for me:

Code: Select all

IPAddress local_IP(192, 168, 1, 24);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress dns(192, 168, 1, 1); //needed to go on internet

WiFi.config(staticIP,  gateway, subnet, dns);

Who is online

Users browsing this forum: No registered users and 45 guests