From 40fdb04d8dd04c4b2bf57f0a2298da9b34edbca0 Mon Sep 17 00:00:00 2001 From: JbLb Date: Wed, 9 Dec 2020 23:04:42 +0100 Subject: [PATCH] Initial code commit --- OTAsetup.cpp | 31 +++++++++++++++++ OTAsetup.h | 10 ++++++ Simple_Horloge.ino | 83 ++++++++++++++++++++++++++++++++++++++++++++++ d_helper.cpp | 31 +++++++++++++++++ d_helper.h | 21 ++++++++++++ 5 files changed, 176 insertions(+) create mode 100644 OTAsetup.cpp create mode 100644 OTAsetup.h create mode 100644 Simple_Horloge.ino create mode 100644 d_helper.cpp create mode 100644 d_helper.h diff --git a/OTAsetup.cpp b/OTAsetup.cpp new file mode 100644 index 0000000..54b7777 --- /dev/null +++ b/OTAsetup.cpp @@ -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(); +} diff --git a/OTAsetup.h b/OTAsetup.h new file mode 100644 index 0000000..6528904 --- /dev/null +++ b/OTAsetup.h @@ -0,0 +1,10 @@ + +#ifndef OTAsetup_h +#define OTAsetup_h + +#include +#include "d_helper.h" + +void initOTA(); + +#endif diff --git a/Simple_Horloge.ino b/Simple_Horloge.ino new file mode 100644 index 0000000..ad81392 --- /dev/null +++ b/Simple_Horloge.ino @@ -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 +#include +#include // https://github.com/tzapu/WiFiManager +#include +#include +#include // 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(); + +} diff --git a/d_helper.cpp b/d_helper.cpp new file mode 100644 index 0000000..1d14ca3 --- /dev/null +++ b/d_helper.cpp @@ -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 diff --git a/d_helper.h b/d_helper.h new file mode 100644 index 0000000..d2bcfa3 --- /dev/null +++ b/d_helper.h @@ -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