From 2c138a3225c84817f174f1f175bf16f4b1cf01b5 Mon Sep 17 00:00:00 2001 From: Clement Date: Fri, 18 Aug 2023 21:34:05 +0200 Subject: [PATCH 1/2] feat: test ntp --- include/Program.h | 4 ++++ platformio.ini | 1 + src/Program.cpp | 27 ++++++++++++++++++++++++--- src/main.cpp | 2 ++ 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/include/Program.h b/include/Program.h index 6449382..d2c92e8 100644 --- a/include/Program.h +++ b/include/Program.h @@ -3,6 +3,8 @@ #include #include +#include +#include #include "DiscordAPI.h" @@ -21,6 +23,8 @@ public: private: DiscordAPI* discord; + WiFiUDP* ntpUDP; + NTPClient* timeClient; }; #endif diff --git a/platformio.ini b/platformio.ini index 329b7c7..4de31e9 100644 --- a/platformio.ini +++ b/platformio.ini @@ -52,6 +52,7 @@ upload_speed = 921600 lib_deps = ; example: ; erropix/ESP32 AnalogWrite@0.2 + arduino-libraries/NTPClient@^3.2.1 ; Checker settings check_tool = clangtidy, cppcheck diff --git a/src/Program.cpp b/src/Program.cpp index 9dbdbea..129ac98 100644 --- a/src/Program.cpp +++ b/src/Program.cpp @@ -21,12 +21,33 @@ Program::Program() { Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer - + //init obj this->discord = new DiscordAPI(DISCORD_HOOK); - delay(1000); - Serial.println(this->discord->sendHeure("10h", "18h")); + this->ntpUDP = new WiFiUDP(); + this->timeClient = new NTPClient(*this->ntpUDP, "pool.ntp.org", 3600*2);//*2 = gnt+2 + + //Serial.println(this->discord->sendHeure("10h", "18h")); } void Program::loop() { // Loop + delay(1000); + + this->timeClient->update(); + + int currentHour = this->timeClient->getHours(); + Serial.print("Hour: "); + Serial.println(currentHour); + + int currentMinute = this->timeClient->getMinutes(); + Serial.print("Minutes: "); + Serial.println(currentMinute); + + int currentSecond = this->timeClient->getSeconds(); + Serial.print("Seconds: "); + Serial.println(currentSecond); + + int currentDayOfWeek = this->timeClient->getDay(); + Serial.print("Day of week: "); + Serial.println(currentDayOfWeek); } diff --git a/src/main.cpp b/src/main.cpp index 74bf5a7..6828cdb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,3 +9,5 @@ void setup() { void loop() { program->loop(); } + + -- 2.47.1 From 2a61d59edc687d5676e1d1c63b3be6156e3a7c5a Mon Sep 17 00:00:00 2001 From: Clement Date: Fri, 1 Sep 2023 21:09:08 +0200 Subject: [PATCH 2/2] add sendTime --- include/Program.h | 7 +++++++ src/Program.cpp | 12 +++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/include/Program.h b/include/Program.h index d2c92e8..31aab13 100644 --- a/include/Program.h +++ b/include/Program.h @@ -25,6 +25,13 @@ private: DiscordAPI* discord; WiFiUDP* ntpUDP; NTPClient* timeClient; + + /** + * @brief Send time to discord + * the start time is automaticly set to the current time + * @param[in] timeEnd LabOuest closing time + */ + void sendTime(String timeEnd); }; #endif diff --git a/src/Program.cpp b/src/Program.cpp index 129ac98..7a43a7e 100644 --- a/src/Program.cpp +++ b/src/Program.cpp @@ -26,7 +26,17 @@ Program::Program() { this->ntpUDP = new WiFiUDP(); this->timeClient = new NTPClient(*this->ntpUDP, "pool.ntp.org", 3600*2);//*2 = gnt+2 - //Serial.println(this->discord->sendHeure("10h", "18h")); + this->timeClient->update(); + this->sendTime("18h12"); +} + +void Program::sendTime(String timeEnd){ + String start = (String)this->timeClient->getHours() + "h"; + int startQuater = this->timeClient->getMinutes()/15; + if (startQuater != 0){ + start += (String)(startQuater * 15); + } + this->discord->sendHeure(start, timeEnd); } void Program::loop() { -- 2.47.1