diff --git a/ReadMe.md b/ReadMe.md index d810026..034e54d 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -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 menu = {"wifi", "info", "custom", "param", "sep", "restart", "exit"}; @@ -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 diff --git a/examples/tzupdate/tzupdate.ino b/examples/tzupdate/tzupdate.ino index e65313f..72cd6cd 100644 --- a/examples/tzupdate/tzupdate.ino +++ b/examples/tzupdate/tzupdate.ino @@ -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 @@ -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 menu = {"wifi", "info", "custom", "param", "sep", "restart", "exit"}; diff --git a/library.properties b/library.properties index fb9fcaa..898f2fb 100644 --- a/library.properties +++ b/library.properties @@ -1,12 +1,12 @@ name=WiFiManagerTz -version=1.3.2 +version=1.3.3 author=tobozo maintainer=tobozo 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 diff --git a/src/NTP.cpp b/src/NTP.cpp index cf9fe4d..5189398 100644 --- a/src/NTP.cpp +++ b/src/NTP.cpp @@ -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 @@ -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 ) { @@ -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; } @@ -114,11 +126,16 @@ namespace WiFiManagerNS void loadPrefs() { - prefs::getUChar( NVS_NTPZONE_KEY, ¤tServer, 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, ¤tServer, 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 } }; diff --git a/src/NTP.hpp b/src/NTP.hpp index 09dd3d9..7ebe028 100644 --- a/src/NTP.hpp +++ b/src/NTP.hpp @@ -42,8 +42,7 @@ namespace WiFiManagerNS }; - void setSyncDelay( unsigned int minutes ); - unsigned int getSyncDelay(); + void loadPrefServer(); void loadPrefs(); @@ -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 diff --git a/src/TZ.cpp b/src/TZ.cpp index 3e57fc4..9549f6f 100644 --- a/src/TZ.cpp +++ b/src/TZ.cpp @@ -24,7 +24,7 @@ * \*/ -#include "prefs.hpp" + #include @@ -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() @@ -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 +#include "prefs.hpp" namespace WiFiManagerNS { diff --git a/src/WiFiManagerTz.h b/src/WiFiManagerTz.h index 7f3c50d..42daff8 100644 --- a/src/WiFiManagerTz.h +++ b/src/WiFiManagerTz.h @@ -5,37 +5,17 @@ #include "NTP.hpp" #include "TZ.hpp" - - namespace WiFiManagerNS { - #include "strings_en.h" - bool NTPEnabled = false; // overriden by prefs bool DSTEnabled = true; String TimeConfHTML; char systime[64]; - constexpr const char* menuhtml = "

\n"; - - WiFiManager *_wifiManager; - + std::function _webserverPreCallback; void bindServerCallback(); - void init(WiFiManager *manager) - { - _wifiManager = manager; - _wifiManager->setWebServerCallback(bindServerCallback); - _wifiManager->setCustomMenuHTML( menuhtml ); - _wifiManager->setClass("invert"); - // _wifiManager->wm.setDarkMode(true); - TZ::loadPrefs(); - prefs::getBool("NTPEnabled", &NTPEnabled, false ); - if( NTPEnabled ) { - NTP::loadPrefs(); - } - } void configTime() { @@ -45,73 +25,6 @@ namespace WiFiManagerNS } - enum Element_t - { - HTML_HEAD_START, - HTML_STYLE, - HTML_SCRIPT, - HTML_HEAD_END, - HTML_PORTAL_OPTIONS, - HTML_ITEM, - HTML_FORM_START, - HTML_FORM_PARAM, - HTML_FORM_END, - HTML_SCAN_LINK, - HTML_SAVED, - HTML_END, - }; - - constexpr const size_t count = sizeof( Element_t ); - - struct TemplateSet_t - { - Element_t element; - const char* content; - }; - - TemplateSet_t templates[] = - { - { HTML_HEAD_START, HTTP_HEAD_START }, - { HTML_STYLE, HTTP_STYLE }, - { HTML_SCRIPT, HTTP_SCRIPT }, - { HTML_HEAD_END, HTTP_HEAD_END }, - { HTML_PORTAL_OPTIONS, HTTP_PORTAL_OPTIONS }, - { HTML_ITEM, HTTP_ITEM }, - { HTML_FORM_START, HTTP_FORM_START }, - { HTML_FORM_PARAM, HTTP_FORM_PARAM }, - { HTML_FORM_END, HTTP_FORM_END }, - { HTML_SCAN_LINK, HTTP_SCAN_LINK }, - { HTML_SAVED, HTTP_SAVED }, - { HTML_END, HTTP_END }, - }; - - - void setTemplate( Element_t element, const char* content ) - { - templates[element].content = content; - } - - - const char* getTemplate( Element_t element ) - { - return templates[element].content; - } - - // teeny-tiny 57 bytes GIF favicon - const char favicon[] = - { - 0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x09, 0x00, 0x08, 0x00, 0x80, 0x00, 0x00, 0xff, 0xff, 0xff, - 0x00, 0x00, 0x00, 0x21, 0xf9, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00, 0x00, - 0x09, 0x00, 0x08, 0x00, 0x00, 0x02, 0x10, 0x8c, 0x8f, 0x01, 0xbb, 0xc7, 0x60, 0xa0, 0x8a, 0x54, - 0xbd, 0x16, 0x1f, 0x82, 0x3c, 0xf9, 0x02, 0x00, 0x3b - }; - - void handleFavicon() - { - _wifiManager->server->send_P(200, "image/gif", favicon, sizeof(favicon) ); - } - - String getSystimeStr() { static String systimeStr; @@ -126,165 +39,289 @@ namespace WiFiManagerNS return systimeStr; } + #if ! defined WiFiManager_h - void handleRoute() - { - Serial.println("[HTTP] handle route Custom"); + void init() + { + TZ::loadPrefs(); + prefs::getBool("NTPEnabled", &NTPEnabled, false ); + prefs::getBool("DSTEnabled", &DSTEnabled, NTPEnabled ); + if( NTPEnabled ) { + NTP::loadPrefs(); + } + } + + #else // defined WiFiManager_h + + + constexpr const char* menuhtml = "

\n"; + + enum Element_t + { + HTML_HEAD_START, + HTML_STYLE, + HTML_SCRIPT, + HTML_HEAD_END, + HTML_PORTAL_OPTIONS, + HTML_ITEM, + HTML_FORM_START, + HTML_FORM_PARAM, + HTML_FORM_END, + HTML_SCAN_LINK, + HTML_SAVED, + HTML_END, + }; + + constexpr const size_t count = sizeof( Element_t ); + + struct TemplateSet_t + { + Element_t element; + const char* content; + }; + + TemplateSet_t templates[] = + { + { HTML_HEAD_START, HTTP_HEAD_START }, + { HTML_STYLE, HTTP_STYLE }, + { HTML_SCRIPT, HTTP_SCRIPT }, + { HTML_HEAD_END, HTTP_HEAD_END }, + { HTML_PORTAL_OPTIONS, HTTP_PORTAL_OPTIONS }, + { HTML_ITEM, HTTP_ITEM }, + { HTML_FORM_START, HTTP_FORM_START }, + { HTML_FORM_PARAM, HTTP_FORM_PARAM }, + { HTML_FORM_END, HTTP_FORM_END }, + { HTML_SCAN_LINK, HTTP_SCAN_LINK }, + { HTML_SAVED, HTTP_SAVED }, + { HTML_END, HTTP_END }, + }; + + + void setTemplate( Element_t element, const char* content ) + { + templates[element].content = content; + } + + + const char* getTemplate( Element_t element ) + { + return templates[element].content; + } - TimeConfHTML = ""; - TimeConfHTML += getTemplate(HTML_HEAD_START); - TimeConfHTML.replace(FPSTR(T_v), "Timezone setup"); - TimeConfHTML += getTemplate(HTML_SCRIPT); + // teeny-tiny 57 bytes GIF favicon + const char favicon[] = + { + 0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x09, 0x00, 0x08, 0x00, 0x80, 0x00, 0x00, 0xff, 0xff, 0xff, + 0x00, 0x00, 0x00, 0x21, 0xf9, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00, 0x00, + 0x09, 0x00, 0x08, 0x00, 0x00, 0x02, 0x10, 0x8c, 0x8f, 0x01, 0xbb, 0xc7, 0x60, 0xa0, 0x8a, 0x54, + 0xbd, 0x16, 0x1f, 0x82, 0x3c, 0xf9, 0x02, 0x00, 0x3b + }; - TimeConfHTML += ""; - TimeConfHTML += getTemplate(HTML_STYLE); - TimeConfHTML += ""; - TimeConfHTML += ""; - TimeConfHTML += getTemplate(HTML_HEAD_END); - TimeConfHTML.replace(FPSTR(T_c), "invert"); // add class str - TimeConfHTML += "

Time Settings

"; - String systimeStr = getSystimeStr(); + WiFiManager *_wifiManager; - TimeConfHTML += "
"; - TimeConfHTML += "
"; + void init(WiFiManager *manager, std::function webserverPreCallback) + { + _webserverPreCallback = webserverPreCallback; - //const char *currentTimeZone = "Europe/Paris"; - TimeConfHTML += ""; - TimeConfHTML += "
"; - TimeConfHTML += " "; - TimeConfHTML += " "; - TimeConfHTML += "
"; + void handleFavicon() + { + _wifiManager->server->send_P(200, "image/gif", favicon, sizeof(favicon) ); + } - TimeConfHTML += ""; + void handleRoute() + { + Serial.println("[HTTP] handle route Custom"); - TimeConfHTML += "
"; + TimeConfHTML = ""; + TimeConfHTML += getTemplate(HTML_HEAD_START); + TimeConfHTML.replace(FPSTR(T_v), "Timezone setup"); + TimeConfHTML += getTemplate(HTML_SCRIPT); - TimeConfHTML += "

NTP Client Setup

"; + TimeConfHTML += ""; - TimeConfHTML += " "; - TimeConfHTML += " "; - TimeConfHTML += "
"; + TimeConfHTML += getTemplate(HTML_STYLE); + TimeConfHTML += ""; + TimeConfHTML += ""; + TimeConfHTML += getTemplate(HTML_HEAD_END); + TimeConfHTML.replace(FPSTR(T_c), "invert"); // add class str + TimeConfHTML += "

Time Settings

"; - TimeConfHTML += ""; - //TimeConfHTML += ""; - TimeConfHTML += "
"; + String systimeStr = getSystimeStr(); - TimeConfHTML += ""; - TimeConfHTML += "
"; + TimeConfHTML += "
"; + TimeConfHTML += ""; + TimeConfHTML += "
"; - TimeConfHTML += ""; - TimeConfHTML += ""; + TimeConfHTML += "
"; - TimeConfHTML += getTemplate(HTML_END); + //const char *currentTimeZone = "Europe/Paris"; + TimeConfHTML += ""; + TimeConfHTML += "
"; - _wifiManager->server->send_P( 200, "text/html", TimeConfHTML.c_str(), TimeConfHTML.length() ); - TimeConfHTML = String(); - } + TimeConfHTML += " "; + TimeConfHTML += " "; + TimeConfHTML += "
"; + TimeConfHTML += ""; - void handleValues() - { - bool success = true; - bool _NTPEnabled = NTPEnabled; - Serial.println("[HTTP] handle route Values"); - if( _wifiManager->server->hasArg("use-ntp-server") ) { - String UseNtpServer = _wifiManager->server->arg("use-ntp-server"); - log_d("UseNtpServer: %s", UseNtpServer.c_str() ); - uint8_t useNtpServer = atoi( UseNtpServer.c_str() ); - NTPEnabled = useNtpServer==1; - } else { - NTPEnabled = false; - } - if( _NTPEnabled != NTPEnabled ) { - prefs::setBool("NTPEnabled", NTPEnabled ); - } - if( _wifiManager->server->hasArg("timezone") ) { - String timezoneStr = _wifiManager->server->arg("timezone"); - log_d("timezoneStr: %s", timezoneStr.c_str() ); - size_t tzidx = atoi( timezoneStr.c_str() ); - String timezone = TZ::defaultTzName; - if( tzidx < TZ::zones() ) { - timezone = String( TZ::timezones[tzidx] ); - log_d("timezone: %s", timezone.c_str() ); + TimeConfHTML += "
"; + + TimeConfHTML += "

NTP Client Setup

"; + + TimeConfHTML += " "; + TimeConfHTML += " "; + TimeConfHTML += "
"; + + TimeConfHTML += ""; + //TimeConfHTML += ""; + TimeConfHTML += "
"; + + #if defined ESP32 + TimeConfHTML += ""; + TimeConfHTML += "
"; + #endif + + TimeConfHTML += "
"; + + TimeConfHTML += ""; + TimeConfHTML += "
"; + + TimeConfHTML += getTemplate(HTML_END); - TZ::setTzName( timezone.c_str() ); - const char* tz = TZ::getTzByLocation( TZ::tzName ); - TZ::configTimeWithTz( tz, NTP::server() ); + _wifiManager->server->send_P( 200, "text/html", TimeConfHTML.c_str(), TimeConfHTML.length() ); + + TimeConfHTML = String(); } - if( NTPEnabled ) { - // also collect tz/server data - if( _wifiManager->server->hasArg("ntp-server") ) { - String NtpServer = _wifiManager->server->arg("ntp-server"); - log_d("NtpServer: %s", NtpServer.c_str() ); - uint8_t server_id = atoi( NtpServer.c_str() ); - if( !NTP::setServer( server_id ) ) success = false; + + void handleValues() + { + bool success = true; + bool _NTPEnabled = NTPEnabled; + Serial.println("[HTTP] handle route Values"); + if( _wifiManager->server->hasArg("use-ntp-server") ) { + String UseNtpServer = _wifiManager->server->arg("use-ntp-server"); + log_d("UseNtpServer: %s", UseNtpServer.c_str() ); + uint8_t useNtpServer = atoi( UseNtpServer.c_str() ); + NTPEnabled = useNtpServer==1; + } else { + NTPEnabled = false; + } + if( _NTPEnabled != NTPEnabled ) { + #if defined ESP32 + prefs::setBool("NTPEnabled", NTPEnabled ); + #else + prefs::setPref(prefs::NTP_ENABLED, NTPEnabled ); + #endif + } + + if( _wifiManager->server->hasArg("timezone") ) { + String timezoneStr = _wifiManager->server->arg("timezone"); + log_d("timezoneStr: %s", timezoneStr.c_str() ); + size_t tzidx = atoi( timezoneStr.c_str() ); + String timezone = TZ::defaultTzName; + if( tzidx < TZ::zones() ) { + timezone = String( TZ::timezones[tzidx] ); + log_d("timezone: %s", timezone.c_str() ); + } + + TZ::setTzName( timezone.c_str() ); + const char* tz = TZ::getTzByLocation( TZ::tzName ); + TZ::configTimeWithTz( tz, NTP::server() ); } - if( _wifiManager->server->hasArg("ntp-server-interval") ) { - String NtpServerInterval = _wifiManager->server->arg("ntp-server-interval"); - log_d("NtpServerInterval: %s", NtpServerInterval.c_str() ); - int serverInterval = atoi( NtpServerInterval.c_str() ); - switch( serverInterval ) { - case 60: - case 14400: - case 10080: - break; - default: - serverInterval = 14400; + if( NTPEnabled ) { + // also collect tz/server data + if( _wifiManager->server->hasArg("ntp-server") ) { + String NtpServer = _wifiManager->server->arg("ntp-server"); + log_d("NtpServer: %s", NtpServer.c_str() ); + uint8_t server_id = atoi( NtpServer.c_str() ); + if( !NTP::setServer( server_id ) ) success = false; + } + + if( _wifiManager->server->hasArg("ntp-server-interval") ) { + String NtpServerInterval = _wifiManager->server->arg("ntp-server-interval"); + log_d("NtpServerInterval: %s", NtpServerInterval.c_str() ); + int serverInterval = atoi( NtpServerInterval.c_str() ); + switch( serverInterval ) { + case 60: + case 14400: + case 10080: + break; + default: + serverInterval = 14400; + } + #if defined ESP32 + NTP::setSyncDelay( serverInterval ); + #endif } - NTP::setSyncDelay( serverInterval ); } + + const char* successResp = ""; + const char* failureResp = ""; + + _wifiManager->server->send(200, "text/html", success?successResp:failureResp); } - const char* successResp = ""; - const char* failureResp = ""; - _wifiManager->server->send(200, "text/html", success?successResp:failureResp); - } + void bindServerCallback() + { + if (_webserverPreCallback != NULL) { + _webserverPreCallback(); // callback to add/override server routes + } + _wifiManager->server->on("/custom", handleRoute); + _wifiManager->server->on("/save-tz", handleValues); + _wifiManager->server->on("/favicon.ico", handleFavicon); + } - void bindServerCallback() - { - _wifiManager->server->on("/custom", handleRoute); - _wifiManager->server->on("/save-tz", handleValues); - _wifiManager->server->on("/favicon.ico", handleFavicon); - } + #endif }; diff --git a/src/prefs.cpp b/src/prefs.cpp index 3c06f24..7621b12 100644 --- a/src/prefs.cpp +++ b/src/prefs.cpp @@ -25,155 +25,204 @@ \*/ #include "prefs.hpp" +#include "TZ.hpp" -#include namespace WiFiManagerNS { - Preferences _prefs; + #if defined ESP32 + Preferences _prefs; + #endif namespace prefs { - void reset() + + void setPref( pref_name_t pref_name, int val ) { - _prefs.begin(PREF_NAMESPACE, false ); - _prefs.clear(); - _prefs.end(); + switch( pref_name ) + { + #if defined ESP32 + default: break; + // case NTP_DELAYMIN: prefs::setUInt( NVS_NTP_DELAYMIN, val ); break; + // case NTP_ZONE_KEY: prefs::setUChar( NVS_NTPZONE_KEY, val ); break; + // case NTP_ENABLED : prefs::setBool( NTP_SYNC_ENABLED, val ); break; + // case TIMEZONE_ID : + // prefs::set( prefName, TZ::timezones[val], strlen(TZ::timezones[val]) ); + // prefs::setUInt( TZ_ID, val ); + // break; + #elif defined ESP8266 + case NTP_DELAYMIN: EEPROM.write( NTP_DELAYMIN, val ); EEPROM.commit(); break; + case NTP_ZONE_KEY: EEPROM.write( NTP_ZONE_KEY, val ); EEPROM.commit(); break; + case NTP_ENABLED : EEPROM.write( NTP_ENABLED, val ); EEPROM.commit(); break; + case TIMEZONE_ID : EEPROM.write( TIMEZONE_ID, val ); EEPROM.commit(); break; + #endif + } } - void set( const char *name, const char *value, size_t len ) + int getPref( pref_name_t pref_name ) { - _prefs.begin(PREF_NAMESPACE, false ); - char buf[len+2] = {0}; - snprintf( buf, len+1, "%s", value ); - if( _prefs.putString(name, buf) ) { - log_d("[Pref] '%s' Saved: char[%d]", name, len-1 ); - } else { - log_d("[Pref] '%s' Saving failed! (char[%d])", name, len-1 ); + switch( pref_name ) + { + #if defined ESP32 + default: return 0; + // case NTP_DELAYMIN: return prefs::getUInt( NVS_NTP_DELAYMIN, 60 ); + // case NTP_ZONE_KEY: prefs::getUChar( NVS_NTPZONE_KEY, ¤tServer, currentServer ); return 0; + // case NTP_ENABLED : return prefs::getBool( NTP_SYNC_ENABLED, false ); + // case TIMEZONE_ID : return prefs::getUInt( TZ_ID, val, 0 ); + #elif defined ESP8266 + case NTP_DELAYMIN: return EEPROM.read( NTP_DELAYMIN ); + case NTP_ZONE_KEY: return EEPROM.read( NTP_ZONE_KEY ); + case NTP_ENABLED : return EEPROM.read( NTP_ENABLED ); + case TIMEZONE_ID : return EEPROM.read( TIMEZONE_ID ); + default: return 0; + #endif } - _prefs.end(); } - void get( const char *name, char *dest, size_t max_len, const char *default_value ) - { - _prefs.begin(PREF_NAMESPACE, true ); - size_t len = _prefs.getString(name, dest, max_len ); - if( len > 0 ) { - log_d("[Pref] '%s' Thawed: char[%d]", name, len ); - } else { - len = strlen(default_value)+1; - snprintf( dest, max_len, "%s", default_value ); - log_d("[Pref] '%s' Defaulted to '%s')", name, default_value ); + #if defined ESP32 + + void reset() + { + _prefs.begin(PREF_NAMESPACE, false ); + _prefs.clear(); + _prefs.end(); } - _prefs.end(); - } - void setUChar( const char *name, uint8_t value ) - { - _prefs.begin(PREF_NAMESPACE, false ); - if( _prefs.putUChar(name, value) ) { - log_d("[Pref] Saved: '%s' => %d", name, value ); - } else { - log_d("[Pref] Saving failed! '%s' => %d", name, value ); + void set( const char *name, const char *value, size_t len ) + { + _prefs.begin(PREF_NAMESPACE, false ); + char buf[len+2] = {0}; + snprintf( buf, len+1, "%s", value ); + if( _prefs.putString(name, buf) ) { + log_d("[Pref] '%s' Saved: char[%d]", name, len-1 ); + } else { + log_d("[Pref] '%s' Saving failed! (char[%d])", name, len-1 ); + } + _prefs.end(); } - _prefs.end(); - } - void getUChar( const char *name, uint8_t *dest, uint8_t default_value ) - { - _prefs.begin(PREF_NAMESPACE, true ); - *dest = _prefs.getUChar(name, default_value ); - if( *dest != default_value ) { - log_d("[Pref] Thawed: '%s' => %d", name, *dest ); - } else { - log_d("[Pref] Defaulted: '%s' => %d", name, default_value ); + void get( const char *name, char *dest, size_t max_len, const char *default_value ) + { + _prefs.begin(PREF_NAMESPACE, true ); + size_t len = _prefs.getString(name, dest, max_len ); + if( len > 0 ) { + log_d("[Pref] '%s' Thawed: char[%d]", name, len ); + } else { + len = strlen(default_value)+1; + snprintf( dest, max_len, "%s", default_value ); + log_d("[Pref] '%s' Defaulted to '%s')", name, default_value ); + } + _prefs.end(); } - _prefs.end(); - } - void setFloat( const char *name, float value ) - { - _prefs.begin(PREF_NAMESPACE, false ); - if( _prefs.putFloat(name, value) ) { - log_d("[Pref] Saved: '%s' => %.2f", name, value ); - } else { - log_d("[Pref] Saving failed! '%s' => %.2f", name, value ); + void setUChar( const char *name, uint8_t value ) + { + _prefs.begin(PREF_NAMESPACE, false ); + if( _prefs.putUChar(name, value) ) { + log_d("[Pref] Saved: '%s' => %d", name, value ); + } else { + log_d("[Pref] Saving failed! '%s' => %d", name, value ); + } + _prefs.end(); } - _prefs.end(); - } - void getFloat( const char *name, float *dest, float default_value ) - { - _prefs.begin(PREF_NAMESPACE, true ); - *dest = _prefs.getFloat(name, default_value ); - if( *dest != default_value ) { - log_d("[Pref] Thawed: '%s' => %.2f", name, *dest ); - } else { - log_d("[Pref] Defaulted: '%s' => %.2f", name, default_value ); + void getUChar( const char *name, uint8_t *dest, uint8_t default_value ) + { + _prefs.begin(PREF_NAMESPACE, true ); + *dest = _prefs.getUChar(name, default_value ); + if( *dest != default_value ) { + log_d("[Pref] Thawed: '%s' => %d", name, *dest ); + } else { + log_d("[Pref] Defaulted: '%s' => %d", name, default_value ); + } + _prefs.end(); } - _prefs.end(); - } - void setBool(const char* name, bool value) - { - _prefs.begin(PREF_NAMESPACE, false ); - if( _prefs.putBool(name, value) ) { - log_d("[Pref] Saved: '%s' => %s", name, value?"true":"false" ); - } else { - log_d("[Pref] Saving failed! '%s' => %s", name, value?"true":"false" ); + void setFloat( const char *name, float value ) + { + _prefs.begin(PREF_NAMESPACE, false ); + if( _prefs.putFloat(name, value) ) { + log_d("[Pref] Saved: '%s' => %.2f", name, value ); + } else { + log_d("[Pref] Saving failed! '%s' => %.2f", name, value ); + } + _prefs.end(); } - _prefs.end(); - } - void getBool( const char *name, bool *dest, bool default_value ) - { - _prefs.begin(PREF_NAMESPACE, true ); - *dest = _prefs.getBool(name, default_value ); - if( *dest != default_value ) { - log_d("[Pref] Thawed: '%s' => %s", name, *dest?"true":"false" ); - } else { - log_d("[Pref] Defaulted: '%s' => %s", name, default_value?"true":"false" ); + void getFloat( const char *name, float *dest, float default_value ) + { + _prefs.begin(PREF_NAMESPACE, true ); + *dest = _prefs.getFloat(name, default_value ); + if( *dest != default_value ) { + log_d("[Pref] Thawed: '%s' => %.2f", name, *dest ); + } else { + log_d("[Pref] Defaulted: '%s' => %.2f", name, default_value ); + } + _prefs.end(); } - _prefs.end(); - } - void setUInt( const char* name, unsigned int value) - { - _prefs.begin(PREF_NAMESPACE, false ); - if( _prefs.putUInt(name, value) ) { - log_d("[Pref] Saved: '%s' => %d", name, value ); - } else { - log_d("[Pref] Saving failed! '%s' => %d", name, value ); + void setBool(const char* name, bool value) + { + _prefs.begin(PREF_NAMESPACE, false ); + if( _prefs.putBool(name, value) ) { + log_d("[Pref] Saved: '%s' => %s", name, value?"true":"false" ); + } else { + log_d("[Pref] Saving failed! '%s' => %s", name, value?"true":"false" ); + } + _prefs.end(); } - _prefs.end(); - } - void getUInt( const char *name, unsigned int *dest, unsigned int default_value ) - { - _prefs.begin(PREF_NAMESPACE, true ); - *dest = _prefs.getUInt(name, default_value ); - if( *dest != default_value ) { - log_d("[Pref] Thawed: '%s' => %d", name, *dest ); - } else { - log_d("[Pref] Defaulted: '%s' => %d", name, default_value ); + void getBool( const char *name, bool *dest, bool default_value ) + { + _prefs.begin(PREF_NAMESPACE, true ); + *dest = _prefs.getBool(name, default_value ); + if( *dest != default_value ) { + log_d("[Pref] Thawed: '%s' => %s", name, *dest?"true":"false" ); + } else { + log_d("[Pref] Defaulted: '%s' => %s", name, default_value?"true":"false" ); + } + _prefs.end(); } - _prefs.end(); - } + void setUInt( const char* name, unsigned int value) + { + _prefs.begin(PREF_NAMESPACE, false ); + if( _prefs.putUInt(name, value) ) { + log_d("[Pref] Saved: '%s' => %d", name, value ); + } else { + log_d("[Pref] Saving failed! '%s' => %d", name, value ); + } + _prefs.end(); + } + + + void getUInt( const char *name, unsigned int *dest, unsigned int default_value ) + { + _prefs.begin(PREF_NAMESPACE, true ); + *dest = _prefs.getUInt(name, default_value ); + if( *dest != default_value ) { + log_d("[Pref] Thawed: '%s' => %d", name, *dest ); + } else { + log_d("[Pref] Defaulted: '%s' => %d", name, default_value ); + } + _prefs.end(); + } + #endif + }; }; diff --git a/src/prefs.hpp b/src/prefs.hpp index 35a9697..f6e3a76 100644 --- a/src/prefs.hpp +++ b/src/prefs.hpp @@ -27,26 +27,77 @@ #pragma once #include +#if defined ESP32 + #include +#else + #include +#endif -#define PREF_NAMESPACE "wm" +#if !defined(log_d) + #define log_d printf +#endif +#define PREF_NAMESPACE "wm" namespace WiFiManagerNS { namespace prefs { + + enum pref_name_t + { + NTP_DELAYMIN, + NTP_ZONE_KEY, + NTP_ENABLED, + TIMEZONE_ID, + }; + + constexpr const char* NVS_DST_KEY = "DST"; + constexpr const char* NVS_NTPZONE_KEY = "NTPZONE"; + constexpr const char* NVS_NTP_DELAYMIN = "NTPDELAY"; + constexpr const char* prefName = "TZNAME"; + constexpr const char* TZ_ID = "TZID"; + constexpr const char* NTP_SYNC_ENABLED = "NTPEnabled"; + void reset(); - void set( const char *name, const char *value, size_t len ); - void get( const char *name, char *dest, size_t max_len, const char *default_value ); - void setUChar( const char *name, uint8_t value ); - void getUChar( const char *name, uint8_t *dest, uint8_t default_value ); - void setFloat( const char *name, float value ); - void getFloat( const char *name, float *dest, float default_value ); - void setBool( const char* key, bool value); - void getBool( const char *name, bool *dest, bool default_value ); - void setUInt( const char* name, unsigned int value); - void getUInt( const char *name, unsigned int *dest, unsigned int default_value ); + + #if defined ESP32 + void set( const char *name, const char *value, size_t len ); + void get( const char *name, char *dest, size_t max_len, const char *default_value ); + void setUChar( const char *name, uint8_t value ); + void getUChar( const char *name, uint8_t *dest, uint8_t default_value ); + void setFloat( const char *name, float value ); + void getFloat( const char *name, float *dest, float default_value ); + void setBool( const char* key, bool value); + void getBool( const char *name, bool *dest, bool default_value ); + void setUInt( const char* name, unsigned int value); + void getUInt( const char *name, unsigned int *dest, unsigned int default_value ); + #endif + + void setPref( pref_name_t pref_name, int val ); + int getPref( pref_name_t pref_name ); + + /* + { + switch( pref_name ) + { + #if defined ESP32 + case NTP_DELAYMIN: prefs::setUInt( NVS_NTP_DELAYMIN, val ); break; + case NTP_ZONE_KEY: prefs::setUChar( NVS_NTPZONE_KEY, val ); break; + case NTP_ENABLED : prefs::setBool("NTPEnabled", val ); break; + case TIMEZONE_ID : prefs::setUInt( "TZID", val ); break; + #elif defined ESP8266 + case NTP_DELAYMIN: EEPROM.write( NTP_DELAYMIN, val ); break; + case NTP_ZONE_KEY: EEPROM.write( NTP_ZONE_KEY, val ); break; + case NTP_ENABLED : EEPROM.write( NTP_ENABLED, val ); break; + case TIMEZONE_ID : EEPROM.write( TIMEZONE_ID, val ); break; + #endif + + } + }*/ + + };