Skip to content

Commit

Permalink
Merge pull request #9 from tobozo/1.3.3
Browse files Browse the repository at this point in the history
1.3.3
  • Loading branch information
tobozo authored Jan 9, 2024
2 parents ed8d39a + 6c23539 commit 184ca5c
Show file tree
Hide file tree
Showing 10 changed files with 562 additions and 382 deletions.
3 changes: 2 additions & 1 deletion ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ void setup()
WiFiManager wifiManager;

WiFiManagerNS::init( &wifiManager );
// WiFiManagerNS::init( &wifiManager, webserverPreCallback ); // using a custom callback add/override server routes

// /!\ make sure "custom" is listed there as it's required to pull the "Setup Clock" button
std::vector<const char *> menu = {"wifi", "info", "custom", "param", "sep", "restart", "exit"};
Expand All @@ -40,7 +41,7 @@ I made this library in order to learn how to properly use the WiFiManager, and I
- Add more examples
- Implement minimal logic for external RTC modules coupling
- Make the Time Setup page skinnable
- ESP8266 support
~~- ESP8266 support~~


## Dependencies
Expand Down
30 changes: 17 additions & 13 deletions examples/tzupdate/tzupdate.ino
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,18 @@ WiFiManager wifiManager;
bool configSaved = false;


// Optional callback function, fired when NTP gets updated.
// Used to print the updated time or adjust an external RTC module.
void on_time_available(struct timeval *t)
{
Serial.println("Received time adjustment from NTP");
struct tm timeInfo;
getLocalTime(&timeInfo, 1000);
Serial.println(&timeInfo, "%A, %B %d %Y %H:%M:%S zone %Z %z ");
// RTC.adjust( &timeInfo );
}
#if defined ESP32
// Optional callback function, fired when NTP gets updated.
// Used to print the updated time or adjust an external RTC module.
void on_time_available(struct timeval *t)
{
Serial.println("Received time adjustment from NTP");
struct tm timeInfo;
getLocalTime(&timeInfo, 1000);
Serial.println(&timeInfo, "%A, %B %d %Y %H:%M:%S zone %Z %z ");
// RTC.adjust( &timeInfo );
}
#endif



Expand All @@ -57,11 +59,13 @@ void setup()

// wifiManager.resetSettings();

// optionally attach external RTC update callback
WiFiManagerNS::NTP::onTimeAvailable( &on_time_available );
#if defined ESP32
// optionally attach external RTC update callback
WiFiManagerNS::NTP::onTimeAvailable( &on_time_available );
#endif

// attach NTP/TZ/Clock-setup page to the WiFi Manager
WiFiManagerNS::init( &wifiManager );
WiFiManagerNS::init( &wifiManager, nullptr );

// /!\ make sure "custom" is listed there as it's required to pull the "Setup Clock" button
std::vector<const char *> menu = {"wifi", "info", "custom", "param", "sep", "restart", "exit"};
Expand Down
4 changes: 2 additions & 2 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
name=WiFiManagerTz
version=1.3.2
version=1.3.3
author=tobozo <tobozo@noreply.github.com>
maintainer=tobozo <tobozo@noreply.github.com>
sentence=A NTP/Timezone extension to @tzapu's WiFiManager
paragraph=WiFiManagerTz is a plugin for @tzapu's WiFiManager, adds timezone update/preselection, DST, NTP sync and settings persistence
category=Communication
url=https://github.com/tobozo/WiFiManagerTz
architectures=esp32
architectures=esp32,esp8266
includes=WiFiManagerTz.h
depends=WiFiManager,Time
license=MIT
Expand Down
91 changes: 54 additions & 37 deletions src/NTP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@

#include "prefs.hpp"
#include "NTP.hpp"
#include "sntp.h"
#if __has_include("esp_sntp.h")
#include "esp_sntp.h"
#elif __has_include("sntp.h")
#include "sntp.h"
#else
#error "This library needs either esp_sntp.h or ntp.h from esp core"
#endif


namespace WiFiManagerNS
Expand All @@ -35,47 +41,48 @@ namespace WiFiManagerNS
namespace NTP
{

const char* NVS_DST_KEY = "DST";
const char* NVS_NTPZONE_KEY = "NTPZONE";
const char* NVS_NTP_DELAYMIN = "NTPDELAY";
using namespace WiFiManagerNS::prefs;

const char* defaultServer = "pool.ntp.org";
uint8_t currentServer = 0;
unsigned int sync_delay = 60; // minutes

unsigned int getSyncDelay()
{
return sync_delay;
}
#if defined ESP32

void setSyncDelay( unsigned int minutes )
{
if( sync_delay != minutes ) {
log_d("Setting NTP sync delay to #%d minutes", minutes );
prefs::setUInt( NVS_NTP_DELAYMIN, minutes );
unsigned int sync_delay = 60; // minutes

unsigned int getSyncDelay()
{
return sync_delay;
}
sync_delay = minutes;
sntp_set_sync_interval(sync_delay*60*1000);
}

// Callback function (get's called when time adjusts via NTP)
void timeavailable_default(struct timeval *t)
{
Serial.println("Got time adjustment from NTP!");
struct tm timeInfo;
getLocalTime(&timeInfo, 1000);
Serial.println(&timeInfo, "%A, %B %d %Y %H:%M:%S zone %Z %z ");
}
void setSyncDelay( unsigned int minutes )
{
if( sync_delay != minutes ) {
sync_delay = minutes;
log_d("Setting NTP sync delay to #%d minutes", minutes );
prefs::setUInt( NVS_NTP_DELAYMIN, minutes );
}
sntp_set_sync_interval(sync_delay*60*1000);
}

// Callback function (get's called when time adjusts via NTP)
void timeavailable_default(struct timeval *t)
{
Serial.println("Got time adjustment from NTP!");
struct tm timeInfo;
getLocalTime(&timeInfo, 1000);
Serial.println(&timeInfo, "%A, %B %d %Y %H:%M:%S zone %Z %z ");
}

onTimeAvailable_fn timeavailable = &timeavailable_default;

void onTimeAvailable( onTimeAvailable_fn fn )
{
log_d("Settting custom time notifier");
timeavailable = fn;
}
static onTimeAvailable_fn timeavailable = &timeavailable_default;

void onTimeAvailable( onTimeAvailable_fn fn )
{
log_d("Settting custom time notifier");
timeavailable = fn;
}
#endif

bool setServer( uint8_t id )
{
Expand All @@ -85,7 +92,12 @@ namespace WiFiManagerNS
if( id != currentServer ) {
currentServer = id;
log_d("Setting NTP server to #%d ( %s / %s )", currentServer, Servers[currentServer].name, Servers[currentServer].addr );
prefs::setUChar( NVS_NTPZONE_KEY, currentServer );
#if defined ESP32
prefs::setUChar( NVS_NTPZONE_KEY, currentServer );
#else
prefs::setPref( NTP_ZONE_KEY, currentServer );
#endif

}
return true;
}
Expand Down Expand Up @@ -114,11 +126,16 @@ namespace WiFiManagerNS

void loadPrefs()
{
prefs::getUChar( NVS_NTPZONE_KEY, &currentServer, currentServer );
prefs::getUInt( NVS_NTP_DELAYMIN, &sync_delay, sync_delay );
if( timeavailable )
sntp_set_time_sync_notification_cb( timeavailable );
sntp_set_sync_interval(sync_delay*60*1000);
#if defined ESP32
prefs::getUChar( NVS_NTPZONE_KEY, &currentServer, currentServer );
prefs::getUInt( NVS_NTP_DELAYMIN, &sync_delay, sync_delay );
if( timeavailable )
sntp_set_time_sync_notification_cb( timeavailable );
sntp_set_sync_interval(sync_delay*60*1000);
#else
currentServer = prefs::getPref( NTP_ZONE_KEY );
//sync_delay = (unsigned int)prefs::getPref( NTP_DELAYMIN );
#endif
}

};
Expand Down
11 changes: 7 additions & 4 deletions src/NTP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ namespace WiFiManagerNS
};


void setSyncDelay( unsigned int minutes );
unsigned int getSyncDelay();


void loadPrefServer();
void loadPrefs();
Expand All @@ -52,10 +51,14 @@ namespace WiFiManagerNS
uint8_t getServerId();
const char* server();

#if defined ESP32
void setSyncDelay( unsigned int minutes );
unsigned int getSyncDelay(); // minutes

typedef void(*onTimeAvailable_fn)(struct timeval *t);
typedef void(*onTimeAvailable_fn)(struct timeval *t);

void onTimeAvailable( onTimeAvailable_fn fn );
void onTimeAvailable( onTimeAvailable_fn fn );
#endif



Expand Down
23 changes: 20 additions & 3 deletions src/TZ.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*
\*/

#include "prefs.hpp"

#include <TZ.hpp>


Expand All @@ -34,9 +34,11 @@ namespace WiFiManagerNS
namespace TZ
{

constexpr const char* prefName = "TZNAME";
using namespace WiFiManagerNS::prefs;

const char* defaultTzName = "UTC0";
char tzName[255];
int tzId;


size_t zones()
Expand All @@ -47,14 +49,29 @@ namespace WiFiManagerNS

void loadPrefs()
{
prefs::get( prefName, tzName, 255, defaultTzName );
#if defined ESP32
prefs::get( prefName, tzName, 255, defaultTzName );
#else
tzId = prefs::getPref(TIMEZONE_ID);
sprintf(tzName, "%s", timezones[tzId] );
#endif
}


void setTzName( const char* name )
{
if( strcmp( tzName, name ) != 0 ) {
#if defined ESP32
prefs::set( prefName, name, strlen(name) );
#else
for(size_t i=0;i<count;i++) {
if( strcmp(name, timezones[0])==0 ) {
tzId = i;
prefs::setPref(TIMEZONE_ID, tzId );
}
}
#endif

}
snprintf( tzName, 254, "%s", name );
}
Expand Down
1 change: 1 addition & 0 deletions src/TZ.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
\*/

#include <Arduino.h>
#include "prefs.hpp"

namespace WiFiManagerNS
{
Expand Down
Loading

0 comments on commit 184ca5c

Please sign in to comment.