You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.7 KiB

/* Horloge simple sur ESP8266
*
* Mise a l'heure automatique via NTP
* passage automatique heure d'hiver heure d'été
*
*/
//################# LIBRARIES ##########################
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
#include <DNSServer.h>
#include <time.h>
#include <Timezone.h> // https://github.com/JChristensen/Timezone
#include "OTAsetup.h" // OTA helpper
#include "d_helper.h" // debug helper
const char* ntpServer = "fr.pool.ntp.org"; // adresse du serveur NTP
//################## Time Zone & dst config #############
// regle de changement pour l'heure d'hiver
TimeChangeRule CET = {"CET", Last, Sun, Oct, 2, 60}; //UTC + 1 hour
// regle de changement pour l'heure d'été
TimeChangeRule CEST = {"CEST", Last, Sun, Mar, 2, 120}; //UTC + 2 hours
// objet de gestion de l'ajustement d'heure
Timezone CentralEuropean(CET, CEST);
int prev_seconde = 0; // sauvegarde pour affichage 1 fois par seconde
//################## Wifi manager config callback ###################
void configModeCallback (WiFiManager *myWiFiManager) { // AP config mode
DEBUG_PRINTLN("Entered config mode");
DEBUG_PRINTLN(WiFi.softAPIP());
DEBUG_PRINTLN(myWiFiManager->getConfigPortalSSID());
}
void setup() {
Serial.begin(115200); // initialize serial communications
DEBUG_PRINT(F("\r\n"));
initOTA(); // initialize OTA
//############### gestion et connection au reseau Wifi ###############
WiFiManager wifiManager;
// reset saved settings
// wifiManager.resetSettings();
wifiManager.setTimeout(180);
wifiManager.setAPCallback(configModeCallback);
if(!wifiManager.autoConnect("LibreMetric")) {
DEBUG_PRINTLN(F("failed to connect and timeout occurred"));
delay(6000);
ESP.reset(); //reset and try again
delay(180000);
}
// At this stage the WiFi manager will have successfully connected to a network, or if not will try again in 180-seconds
DEBUG_PRINTLN(F("WiFi connected..."));
DEBUG_PRINTLN(WiFi.localIP());
configTime(0, 0, ntpServer); // start sync of time with (S)NTP UTC time
//l'heure UTC est retournée par la fonction now()
}
void loop() {
time_t now = time(nullptr); // heure UTC via lib time
now = CentralEuropean.toLocal(now); // ajustement heure local avec gestion de l'ajustement d'heure
String time = String(ctime(&now)); // transformation en string
int seconds = time.substring(17,19).toInt(); // on recupere les secondes
if (seconds != prev_seconde) { // si nouvelle seconde
Serial.println(time); // on affiche
prev_seconde = seconds; // et on sauve
}
// OTA
yield();
ArduinoOTA.handle();
yield();
}