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.
#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