Skip to content

Commit e80ed99

Browse files
authored
Merge pull request #64 from pennam/gettime-fix
Add getTime
2 parents ed57ad8 + f33879a commit e80ed99

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

UNOR4USBBridge/UNOR4USBBridge.ino

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ void setup() {
162162
/* Set up wifi event */
163163
WiFi.onEvent(CAtHandler::onWiFiEvent);
164164

165+
/* Configure ntp */
166+
configTime(0, 0, "pool.ntp.org");
167+
165168
Debug.setDebugOutputStream(&USBSerial);
166169
Debug.setDebugLevel(DBG_ERROR);
167170

UNOR4USBBridge/cmds_esp_generic.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define CMDS_ESP_GENERIC_H
33

44
#include "at_handler.h"
5+
56
extern "C" {
67
#include "esp32-hal-tinyusb.h"
78
}
@@ -359,6 +360,32 @@ void CAtHandler::add_cmds_esp_generic() {
359360
return chAT::CommandStatus::ERROR;
360361
}
361362
};
363+
364+
/* ....................................................................... */
365+
command_table[_GETTIME] = [this](auto & srv, auto & parser) {
366+
/* ....................................................................... */
367+
368+
switch (parser.cmd_mode) {
369+
case chAT::CommandMode::Write: {
370+
char epoch[12]; // gettime
371+
constexpr uint32_t SECS_YR_2000 = 946684800UL; // the time at the start of y2k
372+
time_t now = time(nullptr);
373+
374+
if (now < SECS_YR_2000) {
375+
now = 0;
376+
}
377+
378+
srv.write_response_prompt();
379+
sprintf(epoch,"%d", (unsigned long) now);
380+
srv.write_str((const char *) epoch);
381+
srv.write_line_end();
382+
383+
return chAT::CommandStatus::OK;
384+
}
385+
default:
386+
return chAT::CommandStatus::ERROR;
387+
}
388+
};
362389
}
363390

364391
#endif

UNOR4USBBridge/commands.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ enum file_op {
2020
#define _ENDL "\r\n"
2121
#define _WIFISCAN "+WIFISCAN"
2222

23+
#define _GETTIME "+GETTIME"
2324
#define _RESET "+RESET"
2425
#define _RESTART_BOOTLOADER "+RESTARTBOOTLOADER"
2526
#define _GMR "+GMR"

0 commit comments

Comments
 (0)