-->
Page 1 of 1

TimeLib do not work with fixe IP address

PostPosted: Sun Apr 21, 2019 4:24 pm
by azimut2000
Hello,

I have a problem with TimeLib : when i use fixe IP address, i can't access to good time.
Here is en exemple that work on ESP8266 and ESP32 : it do the same bug, i allways stay in 1970 when i use fixe IP address.

Code: Select all
#include <ESP8266WiFi.h>            // if ESP8266 plateform
//#include <WiFi.h>                     // if ESP32 plateform
#include <TimeLib.h>     
#include <NtpClientLib.h>

const char* ssid = "xxxxxxxxxx;                 // Entrez le SSID du réseau WiFi
const char* password = "xxxxxxxxxxxxxxxxxx";    // Entrez le password du réseau WiFI
bool cnxMode = false;                           // Mode de reception @IP : false -> DHCP / true -> assignée

IPAddress MyIP(192,168,1,90);     
IPAddress subnet(255, 255, 0, 0);
IPAddress gateway(192, 168, 1, 1);
IPAddress myDNS(8, 8, 8, 8);   

void setup() {

  Serial.begin(115200);
  delay(100);

  WiFi.begin(ssid, password);

  // On attend d'être connecté
  unsigned long MyTime = millis();
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    if (millis() >= ((20*1000) + MyTime) ) {  // si au bout de 20 secondes on est toujours dans la boucle...
      Serial.println("");
      Serial.println("On a dépassé le temps imparti pour la connexion : reboot !");
      ESP.restart();                          // ... on reboot !
    }
  }
  // On affecte une adresse par DHCP ou par IP Fixe
  if (cnxMode) {
    // Mode IP Fixe
    WiFi.config(MyIP, gateway, subnet, myDNS);
  } else {
    // Mode DHCP
  }
 
  // On confirme la connexion à l'utilisateur en allumant la LED
  digitalWrite(LED_BUILTIN, LOW);

  NTP.begin (); // Only statement needed to start NTP sync.

  Serial.println("Nous sommes le " + String(day()) + " " + String(month()) + " " + String(year()));
}

void loop() {

}



What do you think of this ? Does someone have found a solution to work fine ?

Thank's for your ideas and answer.

Thierry

Re: TimeLib do not work with fixe IP address

PostPosted: Sun Apr 28, 2019 9:15 am
by pipi61
Hi!
Try: set esp to DHCP mode...
Find in your router the set DHCP address reservation menu,
then set ESP mac address and requested ipaddress...

Re: TimeLib do not work with fixe IP address

PostPosted: Sun Apr 28, 2019 3:40 pm
by schufti
just don't use
#include <TimeLib.h>
#include <NtpClientLib.h>

see \libraries\esp8266\examples\NTP-TZ-DST for example how2 use sntp in newclib

Re: TimeLib do not work with fixe IP address

PostPosted: Wed May 01, 2019 2:15 pm
by azimut2000
i've found the problem : simply put WiFi.config before WiFi.begin and it works !!!
Very strange...

Thank's again guys for answer,
Thierry