Skip to content

Commit 4238229

Browse files
committed
Commands
* configTime * dateTime
1 parent 7985347 commit 4238229

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

src/Commander-API-Commands.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,61 @@ void commander_wifiScan_func( char *args, Stream *response ){
489489

490490
#endif
491491

492+
#ifdef ESP32
493+
494+
void commander_configTime_func( char *args, Stream *response ){
495+
496+
int gmtOffset_sec;
497+
int daylightOffset_sec;
498+
char ntpServer[32] = "";
499+
int argResult;
500+
501+
argResult = sscanf( args, "%d %d %s", &gmtOffset_sec, &daylightOffset_sec, ntpServer );
502+
503+
if( argResult == 3 ){
504+
505+
configTime( gmtOffset_sec, daylightOffset_sec, ntpServer );
506+
response -> print( (const char*)"Time configured." );
507+
return;
508+
509+
}
510+
511+
argResult = sscanf( args, "%d %d", &gmtOffset_sec, &daylightOffset_sec );
512+
513+
if( argResult == 2 ){
514+
515+
configTime( gmtOffset_sec, daylightOffset_sec, (const char*)"pool.ntp.org" );
516+
response -> print( (const char*)"Time configured with default NTP server: pool.ntp.org" );
517+
return;
518+
519+
}
520+
521+
else{
522+
523+
response -> print( (const char*)"Argument error!" );
524+
return;
525+
526+
}
527+
528+
}
529+
530+
void commander_dateTime_func( char *args, Stream *response ){
531+
532+
struct tm timeInfo;
533+
534+
if( !getLocalTime( &timeInfo ) ){
535+
536+
response -> print( "Failed to obtain time!" );
537+
return;
538+
539+
}
540+
541+
response -> print( &timeInfo, "%A, %B %d %Y %H:%M:%S" );
542+
543+
}
544+
545+
#endif
546+
492547
#ifdef __AVR__
493548

494549
void commander_neofetch_func( char *args, Stream *response ){
@@ -721,6 +776,12 @@ void commander_reboot_func( char *args, Stream *response ){
721776

722777
}
723778

779+
#ifdef ARDUINO
780+
781+
782+
783+
#endif
784+
724785

725786
void commander_sin_func( char *args, Stream *response ){
726787

src/Commander-API-Commands.hpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ SOFTWARE.
3434
/// Arduino detection
3535
#ifdef ARDUINO
3636
#include "Arduino.h"
37+
#include "Wire.h"
3738
#endif
3839

3940
#include <math.h>
@@ -42,6 +43,12 @@ SOFTWARE.
4243
#include <avr/wdt.h>
4344
#endif
4445

46+
#ifdef ESP32
47+
48+
#include "time.h"
49+
50+
#endif
51+
4552
#include "Commander-API.hpp"
4653

4754
//-------- System functions --------//
@@ -161,6 +168,37 @@ void commander_wifiScan_func( char *args, Stream *response );
161168

162169
#endif
163170

171+
#ifdef ESP32
172+
173+
#define API_ELEMENT_CONFIGTIME apiElement( "configTime", "Configure NTP time settings.\r\n\tExample: configTime [ GMT Offset Sec ] [ DL Offset Sec ] [ Server ]\r\n\t[ GMT Offset Sec ] - UTC offset for your timezone in seconds.\r\n\t[ GMT Offset Sec ] - Daylight offset in sec.\r\n\t[ Server ] - NTP Server Address( optional, default: pool.ntp.org )", commander_configTime_func )
174+
/// Premade function for wifiScan command.
175+
/// @param args Pointer to the argument string.
176+
/// @param response Response channel for messages.
177+
void commander_configTime_func( char *args, Stream *response );
178+
179+
#define API_ELEMENT_DATETIME apiElement( "dateTime", "Returns the NTP synchronised date and time.", commander_dateTime_func )
180+
/// Premade function for wifiScan command.
181+
/// @param args Pointer to the argument string.
182+
/// @param response Response channel for messages.
183+
void commander_dateTime_func( char *args, Stream *response );
184+
185+
#endif
186+
187+
#ifdef ARDUINO
188+
189+
#ifdef __AVR__
190+
191+
#define API_ELEMENT_I2C_BEGIN apiElement( "i2cBegin", "Initialize I2C peripheral as bus master.", commander_i2cBegin_func )
192+
#ifdef __AVR__
193+
#define API_ELEMENT_P_I2C_BEGIN( element ) apiElement_P( element, "i2cBegin", "Initialize I2C peripheral as bus master.", commander_i2cBegin_func )
194+
#endif
195+
/// Premade function for sin command.
196+
/// @param args Pointer to the argument string.
197+
/// @param response Response channel for messages.
198+
void commander_i2cBegin_func( char *args, Stream *response );
199+
200+
#endif
201+
164202
//-------- Math functions --------//
165203

166204
#define API_ELEMENT_SIN apiElement( "sin", "Sine function. The input is in radians.", commander_sin_func )

0 commit comments

Comments
 (0)