parent
c25d9a37b1
commit
40fdb04d8d
@ -0,0 +1,31 @@
|
|||||||
|
|
||||||
|
#include "OTAsetup.h"
|
||||||
|
|
||||||
|
void initOTA(){
|
||||||
|
// Set hostname and start OTA
|
||||||
|
ArduinoOTA.setHostname("Horloge");
|
||||||
|
// Start of OTA
|
||||||
|
ArduinoOTA.onStart([]() {
|
||||||
|
DEBUG_PRINTLN("OTA Beginning!");
|
||||||
|
});
|
||||||
|
// progress of OTA
|
||||||
|
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
|
||||||
|
DEBUG_PRINTF("progress: %u%%\r", (progress/(total/100)));
|
||||||
|
});
|
||||||
|
// OTA onEnd
|
||||||
|
ArduinoOTA.onEnd([](){
|
||||||
|
DEBUG_PRINTLN("\nOTA ended, let\'s restart");
|
||||||
|
});
|
||||||
|
// OTA error handle
|
||||||
|
ArduinoOTA.onError([](ota_error_t error) {
|
||||||
|
DEBUG_PRINT("ArduinoOTA Error[");
|
||||||
|
DEBUG_PRINT(error);
|
||||||
|
DEBUG_PRINT("]: ");
|
||||||
|
if (error == OTA_AUTH_ERROR) DEBUG_PRINTLN("Auth Failed");
|
||||||
|
else if (error == OTA_BEGIN_ERROR) DEBUG_PRINTLN("Begin Failed");
|
||||||
|
else if (error == OTA_CONNECT_ERROR) DEBUG_PRINTLN("Connect Failed");
|
||||||
|
else if (error == OTA_RECEIVE_ERROR) DEBUG_PRINTLN("Receive Failed");
|
||||||
|
else if (error == OTA_END_ERROR) DEBUG_PRINTLN("End Failed");
|
||||||
|
});
|
||||||
|
ArduinoOTA.begin();
|
||||||
|
}
|
||||||
@ -0,0 +1,10 @@
|
|||||||
|
|
||||||
|
#ifndef OTAsetup_h
|
||||||
|
#define OTAsetup_h
|
||||||
|
|
||||||
|
#include <ArduinoOTA.h>
|
||||||
|
#include "d_helper.h"
|
||||||
|
|
||||||
|
void initOTA();
|
||||||
|
|
||||||
|
#endif
|
||||||
@ -0,0 +1,83 @@
|
|||||||
|
/* 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();
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,31 @@
|
|||||||
|
// debug helper
|
||||||
|
|
||||||
|
#include "d_helper.h"
|
||||||
|
|
||||||
|
// in a terminal: telnet esp IP
|
||||||
|
#ifdef DEBUG_TELNET
|
||||||
|
WiFiServer telnetServer(23);
|
||||||
|
WiFiClient telnetClient;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////
|
||||||
|
// TELNET
|
||||||
|
///////////////////////////////////////////////////////////////////////////
|
||||||
|
/*
|
||||||
|
Function called to handle Telnet clients
|
||||||
|
https://www.youtube.com/watch?v=j9yW10OcahI
|
||||||
|
*/
|
||||||
|
#ifdef DEBUG_TELNET
|
||||||
|
void handleTelnet(void) {
|
||||||
|
if (telnetServer.hasClient()) {
|
||||||
|
if (!telnetClient || !telnetClient.connected()) {
|
||||||
|
if (telnetClient) {
|
||||||
|
telnetClient.stop();
|
||||||
|
}
|
||||||
|
telnetClient = telnetServer.available();
|
||||||
|
} else {
|
||||||
|
telnetServer.available().stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
@ -0,0 +1,21 @@
|
|||||||
|
#ifndef D_helper_h
|
||||||
|
#define D_helper_h
|
||||||
|
|
||||||
|
void handleTelnet(void);
|
||||||
|
|
||||||
|
// ################# Macros for debugging ################
|
||||||
|
#ifdef DEBUG_TELNET
|
||||||
|
#define DEBUG_PRINT(x) telnetClient.print(x)
|
||||||
|
#define DEBUG_PRINTF(x,y) telnetClient.printf(x,y)
|
||||||
|
#define DEBUG_PRINT_WITH_FMT(x, fmt) telnetClient.print(x, fmt)
|
||||||
|
#define DEBUG_PRINTLN(x) telnetClient.println(x)
|
||||||
|
#define DEBUG_PRINTLN_WITH_FMT(x, fmt) telnetClient.println(x, fmt)
|
||||||
|
#else
|
||||||
|
#define DEBUG_PRINT(x) Serial.print(x)
|
||||||
|
#define DEBUG_PRINTF(x,y) Serial.printf(x,y)
|
||||||
|
#define DEBUG_PRINT_WITH_FMT(x, fmt) Serial.print(x, fmt)
|
||||||
|
#define DEBUG_PRINTLN(x) Serial.println(x)
|
||||||
|
#define DEBUG_PRINTLN_WITH_FMT(x, fmt) Serial.println(x, fmt)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
Loading…
Reference in new issue