Skip to content

Commit

Permalink
Adding sunrise_sunset to NTP (#1001)
Browse files Browse the repository at this point in the history
  • Loading branch information
rickbronson authored Dec 19, 2023
1 parent b0bfc23 commit fe686b4
Show file tree
Hide file tree
Showing 6 changed files with 268 additions and 27 deletions.
19 changes: 19 additions & 0 deletions docs/autoexecs/sunrise_sunset.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
NOTE: Set Time offset, latitude, longitude accordingly

// autoexec for mini smart switch
PowerSave 1
addEventHandler OnHold 8 SafeMode
startDriver ntp
ntp_timeZoneOfs -8
ntp_setLatlong 44.002130 -123.091473
setPinRole 6 WifiLed_n
setPinRole 8 Btn_Tgl_All
setPinRole 14 TglChanOnTgl
setPinChannel 14 0
setPinRole 15 Rel
setPinChannel 15 0
removeClockEvent 12
removeClockEvent 13
waitFor NTPState 1
addClockEvent sunrise 0x7f 12 POWER OFF
addClockEvent sunset 0x7f 13 POWER ON
6 changes: 3 additions & 3 deletions docs/json/autoexecExamples.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@
{
"title": "[Custom countdown/timer system with HTTP GUI for TuyaMCU relay](https://www.elektroda.com/rtvforum/topic4009196.html)",
"file": "autoexecs/custom_timer_for_relay.bat"
},
}
{
"title": "[Choose command/color table example (with index wrap)](https://www.elektroda.com/rtvforum/viewtopic.php?p=20863886#20863886)",
"file": "autoexecs/choose_command.bat"
"title": "[Setup for EZB-WBZS1H16N-A V1.0 Tuya mini smart switch showing sunrise/sunset events](https://www.elektroda.com/rtvforum/topic3967141.html)",
"file": "autoexecs/sunrise_sunset.bat"
}
]
51 changes: 48 additions & 3 deletions src/driver/drv_ntp.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

#include "drv_ntp.h"

extern void NTP_Init_Events(void);
extern void NTP_RunEvents(unsigned int newTime, bool bTimeValid);

#define LOG_FEATURE LOG_FEATURE_NTP

typedef struct
Expand Down Expand Up @@ -59,7 +62,8 @@ static int adrLen;
static int g_ntp_delay = 0;
static bool g_synced;
// time offset (time zone?) in seconds
static int g_timeOffsetSeconds;
#define CFG_DEFAULT_TIMEOFFSETSECONDS (-8 * 60 * 60)
static int g_timeOffsetSeconds = CFG_DEFAULT_TIMEOFFSETSECONDS;
// current time
unsigned int g_ntpTime;

Expand Down Expand Up @@ -95,9 +99,43 @@ commandResult_t NTP_SetTimeZoneOfs(const void *context, const char *cmd, const c
}
g_ntpTime -= oldOfs;
g_ntpTime += g_timeOffsetSeconds;
addLogAdv(LOG_INFO, LOG_FEATURE_NTP,"NTP offset set");
addLogAdv(LOG_INFO, LOG_FEATURE_NTP,"NTP offset set");
return CMD_RES_OK;
}

#if ENABLE_NTP_SUNRISE_SUNSET

/* sunrise/sunset defaults */
#define CFG_DEFAULT_LATITUDE 43.994131
#define CFG_DEFAULT_LONGITUDE -123.095854

struct SUN_DATA sun_data =
{
.latitude = (int) (CFG_DEFAULT_LATITUDE * 1000000),
.longitude = (int) (CFG_DEFAULT_LONGITUDE * 1000000),
};

//Set Latitude and Longitude for sunrise/sunset calc
commandResult_t NTP_SetLatlong(const void *context, const char *cmd, const char *args, int cmdFlags) {
const char *newValue;

Tokenizer_TokenizeString(args,0);
// following check must be done after 'Tokenizer_TokenizeString',
// so we know arguments count in Tokenizer. 'cmd' argument is
// only for warning display
if (Tokenizer_CheckArgsCountAndPrintWarning(cmd, 2)) {
return CMD_RES_NOT_ENOUGH_ARGUMENTS;
}
newValue = Tokenizer_GetArg(0);
sun_data.latitude = (int) (atof(newValue) * 1000000);
addLogAdv(LOG_INFO, LOG_FEATURE_NTP, "NTP latitude set to %s", newValue);

newValue = Tokenizer_GetArg(1);
sun_data.longitude = (int) (atof(newValue) * 1000000);
addLogAdv(LOG_INFO, LOG_FEATURE_NTP, "NTP longitude set to %s", newValue);
return CMD_RES_OK;
}
#endif

//Set custom NTP server
commandResult_t NTP_SetServer(const void *context, const char *cmd, const char *args, int cmdFlags) {
Expand Down Expand Up @@ -229,6 +267,13 @@ void NTP_Init() {
//cmddetail:"fn":"NTP_SetServer","file":"driver/drv_ntp.c","requires":"",
//cmddetail:"examples":""}
CMD_RegisterCommand("ntp_setServer", NTP_SetServer, NULL);
#if ENABLE_NTP_SUNRISE_SUNSET
//cmddetail:{"name":"ntp_setLatlong","args":"[Latlong]",
//cmddetail:"descr":"Sets the NTP latitude and longitude",
//cmddetail:"fn":"NTP_SetLatlong","file":"driver/drv_ntp.c","requires":"",
//cmddetail:"examples":"NTP_SetLatlong -34.911498 138.809488"}
CMD_RegisterCommand("ntp_setLatLong",NTP_SetLatlong, NULL);
#endif
//cmddetail:{"name":"ntp_info","args":"",
//cmddetail:"descr":"Display NTP related settings",
//cmddetail:"fn":"NTP_Info","file":"driver/drv_ntp.c","requires":"",
Expand Down Expand Up @@ -357,7 +402,7 @@ void NTP_CheckForReceive() {
#endif

if(recv_len < 0){
addLogAdv(LOG_INFO, LOG_FEATURE_NTP,"NTP_CheckForReceive: Error while receiving server's msg");
addLogAdv(LOG_INFO, LOG_FEATURE_NTP,"NTP_CheckForReceive: Error while receiving server's msg");
return;
}
highWord = MAKE_WORD(ptr[40], ptr[41]);
Expand Down
7 changes: 4 additions & 3 deletions src/driver/drv_ntp.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ void NTP_SetSimulatedTime(unsigned int timeNow);
int NTP_PrintEventList();
int NTP_RemoveClockEvent(int id);
int NTP_ClearEvents();
void NTP_Init_Events();
int NTP_ClearEvents();
void NTP_RunEvents(unsigned int newTime, bool bTimeValid);

extern unsigned int g_ntpTime;
extern struct SUN_DATA { /* sunrise / sunset globals */
int latitude; /* latitude * 1000000 */
int longitude; /* longitude * 1000000 */
} sun_data;

#endif /* __DRV_NTP_H__ */

Loading

0 comments on commit fe686b4

Please sign in to comment.