From 4d968ecc793cb0454fe3e50a8924a9b0b72e54ec Mon Sep 17 00:00:00 2001 From: paulvha Date: Fri, 16 Feb 2024 17:19:16 +0100 Subject: [PATCH 1/2] getTime --- UNOR4USBBridge/cmds_esp_generic.h | 49 ++++++++++++++++++++++++++++++- UNOR4USBBridge/commands.h | 1 + 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/UNOR4USBBridge/cmds_esp_generic.h b/UNOR4USBBridge/cmds_esp_generic.h index cd0e2d9..18d1778 100644 --- a/UNOR4USBBridge/cmds_esp_generic.h +++ b/UNOR4USBBridge/cmds_esp_generic.h @@ -2,6 +2,10 @@ #define CMDS_ESP_GENERIC_H #include "at_handler.h" + +char SecTime[80]; // gettime +struct tm timeinfo; + extern "C" { #include "esp32-hal-tinyusb.h" } @@ -332,6 +336,49 @@ void CAtHandler::add_cmds_esp_generic() { return chAT::CommandStatus::ERROR; } }; + +/* ....................................................................... */ + command_table[_GETTIME] = [this](auto & srv, auto & parser) { +/* ....................................................................... */ + + switch (parser.cmd_mode) { + case chAT::CommandMode::Write: { + + if (parser.args.size() != 3) { + return chAT::CommandStatus::ERROR; + } + + // get TimeZone info + auto &tz = parser.args[1]; + if (tz.empty()) { + return chAT::CommandStatus::ERROR; + } + + // get format + auto &format = parser.args[2]; + if (format.empty()) { + return chAT::CommandStatus::ERROR; + } + + configTime(0, 0, "pool.ntp.org"); // 0, 0 because we will use TZ from tzset() + setenv("TZ", tz.c_str(), 1); // Set environment variable with your time zone + tzset(); + + while (! getLocalTime(&timeinfo) ) { + delay(100); + } + + //See http://www.cplusplus.com/reference/ctime/strftime/ + strftime(SecTime, 80, format.c_str(), &timeinfo); + srv.write_response_prompt(); + srv.write_cstr((const char *) SecTime); + srv.write_line_end(); + return chAT::CommandStatus::OK; + } + default: + return chAT::CommandStatus::ERROR; + } + }; } -#endif \ No newline at end of file +#endif diff --git a/UNOR4USBBridge/commands.h b/UNOR4USBBridge/commands.h index ffa9555..52cba99 100644 --- a/UNOR4USBBridge/commands.h +++ b/UNOR4USBBridge/commands.h @@ -20,6 +20,7 @@ enum file_op { #define _ENDL "\r\n" #define _WIFISCAN "+WIFISCAN" +#define _GETTIME "+GETTIME" #define _RESET "+RESET" #define _RESTART_BOOTLOADER "+RESTARTBOOTLOADER" #define _GMR "+GMR" From 2e99966902f505c81ab91069854e601d38ecb2fa Mon Sep 17 00:00:00 2001 From: paulvha Date: Wed, 21 Feb 2024 13:12:28 +0100 Subject: [PATCH 2/2] getTime --- UNOR4USBBridge/cmds_esp_generic.h | 58 +++++++++++-------------------- 1 file changed, 21 insertions(+), 37 deletions(-) diff --git a/UNOR4USBBridge/cmds_esp_generic.h b/UNOR4USBBridge/cmds_esp_generic.h index 18d1778..ec3643a 100644 --- a/UNOR4USBBridge/cmds_esp_generic.h +++ b/UNOR4USBBridge/cmds_esp_generic.h @@ -3,8 +3,8 @@ #include "at_handler.h" -char SecTime[80]; // gettime -struct tm timeinfo; +char epoch[12]; // gettime +#define SECS_YR_2000 (946684800UL) // the time at the start of y2k extern "C" { #include "esp32-hal-tinyusb.h" @@ -343,42 +343,26 @@ void CAtHandler::add_cmds_esp_generic() { switch (parser.cmd_mode) { case chAT::CommandMode::Write: { - - if (parser.args.size() != 3) { - return chAT::CommandStatus::ERROR; - } - - // get TimeZone info - auto &tz = parser.args[1]; - if (tz.empty()) { - return chAT::CommandStatus::ERROR; - } - - // get format - auto &format = parser.args[2]; - if (format.empty()) { - return chAT::CommandStatus::ERROR; - } - - configTime(0, 0, "pool.ntp.org"); // 0, 0 because we will use TZ from tzset() - setenv("TZ", tz.c_str(), 1); // Set environment variable with your time zone - tzset(); - - while (! getLocalTime(&timeinfo) ) { - delay(100); - } - - //See http://www.cplusplus.com/reference/ctime/strftime/ - strftime(SecTime, 80, format.c_str(), &timeinfo); - srv.write_response_prompt(); - srv.write_cstr((const char *) SecTime); - srv.write_line_end(); - return chAT::CommandStatus::OK; - } - default: - return chAT::CommandStatus::ERROR; + + configTime(0, 0, "pool.ntp.org"); + time_t now = time(nullptr); + + while (now < SECS_YR_2000) { + delay(100); + now = time(nullptr); + } + + srv.write_response_prompt(); + sprintf(epoch,"%d", (unsigned long) now); + srv.write_cstr((const char *) epoch); + srv.write_line_end(); + + return chAT::CommandStatus::OK; } - }; + default: + return chAT::CommandStatus::ERROR; + } + }; } #endif