Browse Source

Updated NTPClientLib. Support for non full hour time offsets (#265)

i18n
Xose Pérez 6 years ago
parent
commit
3e4313fae3
7 changed files with 3268 additions and 3193 deletions
  1. BIN
      code/espurna/data/index.html.gz
  2. +59
    -24
      code/espurna/ntp.ino
  3. +1
    -0
      code/espurna/relay.ino
  4. +3172
    -3163
      code/espurna/static/index.html.gz.h
  5. +29
    -0
      code/html/custom.js
  6. +6
    -4
      code/html/index.html
  7. +1
    -2
      code/platformio.ini

BIN
code/espurna/data/index.html.gz View File


+ 59
- 24
code/espurna/ntp.ino View File

@ -13,7 +13,8 @@ Copyright (C) 2016-2018 by Xose Pérez <xose dot perez at gmail dot com>
#include <WiFiClient.h> #include <WiFiClient.h>
#include <Ticker.h> #include <Ticker.h>
Ticker _ntp_delay;
bool _ntp_update = false;
bool _ntp_configure = false;
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// NTP // NTP
@ -31,21 +32,63 @@ void _ntpWebSocketOnSend(JsonObject& root) {
} }
void _ntpUpdate() { void _ntpUpdate() {
_ntp_update = false;
#if WEB_SUPPORT #if WEB_SUPPORT
wsSend(_ntpWebSocketOnSend); wsSend(_ntpWebSocketOnSend);
#endif #endif
DEBUG_MSG_P(PSTR("[NTP] Time: %s\n"), (char *) ntpDateTime().c_str()); DEBUG_MSG_P(PSTR("[NTP] Time: %s\n"), (char *) ntpDateTime().c_str());
} }
void _ntpConfigure() { void _ntpConfigure() {
_ntp_configure = false;
int offset = getSetting("ntpOffset", NTP_TIME_OFFSET).toInt();
int sign = offset > 0 ? 1 : -1;
offset = abs(offset);
NTP.begin( NTP.begin(
getSetting("ntpServer1", NTP_SERVER),
getSetting("ntpOffset", NTP_TIME_OFFSET).toInt(),
getSetting("ntpDST", NTP_DAY_LIGHT).toInt() == 1
getSetting("ntpServer", 1, NTP_SERVER).c_str(),
sign * (offset / 60),
getSetting("ntpDST", NTP_DAY_LIGHT).toInt() == 1,
sign * (offset % 60)
); );
if (getSetting("ntpServer2")) NTP.setNtpServerName(getSetting("ntpServer2"), 1);
if (getSetting("ntpServer3")) NTP.setNtpServerName(getSetting("ntpServer3"), 2);
if (hasSetting("ntpServer", 2)) NTP.setNtpServerName(getSetting("ntpServer", 2).c_str(), 1);
if (hasSetting("ntpServer", 3)) NTP.setNtpServerName(getSetting("ntpServer", 3).c_str(), 2);
NTP.setInterval(NTP_UPDATE_INTERVAL); NTP.setInterval(NTP_UPDATE_INTERVAL);
_ntp_update = true;
}
void _ntpLoop() {
if (_ntp_configure) _ntpConfigure();
if (_ntp_update) _ntpUpdate();
now();
#if BROKER_SUPPORT
static unsigned char last_minute = 60;
if (ntpSynced() && (minute() != last_minute)) {
last_minute = minute();
brokerPublish(MQTT_TOPIC_DATETIME, ntpDateTime().c_str());
}
#endif
}
void _ntpBackwards() {
int offset = getSetting("ntpOffset", NTP_TIME_OFFSET).toInt();
if (-30 < offset && offset < 30) {
offset *= 60;
setSetting("ntpOffset", offset);
}
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
@ -65,8 +108,12 @@ String ntpDateTime() {
return String(buffer); return String(buffer);
} }
// -----------------------------------------------------------------------------
void ntpSetup() { void ntpSetup() {
_ntpBackwards();
NTP.onNTPSyncEvent([](NTPSyncEvent_t error) { NTP.onNTPSyncEvent([](NTPSyncEvent_t error) {
if (error) { if (error) {
#if WEB_SUPPORT #if WEB_SUPPORT
@ -78,33 +125,21 @@ void ntpSetup() {
DEBUG_MSG_P(PSTR("[NTP] Error: Invalid NTP server address\n")); DEBUG_MSG_P(PSTR("[NTP] Error: Invalid NTP server address\n"));
} }
} else { } else {
_ntp_delay.once_ms(100, _ntpUpdate);
_ntp_update = true;
} }
}); });
_ntpConfigure();
wifiRegister([](justwifi_messages_t code, char * parameter) {
if (code == MESSAGE_CONNECTED) _ntp_configure = true;
});
#if WEB_SUPPORT #if WEB_SUPPORT
wsOnSendRegister(_ntpWebSocketOnSend); wsOnSendRegister(_ntpWebSocketOnSend);
wsOnAfterParseRegister(_ntpConfigure);
wsOnAfterParseRegister([]() { _ntp_configure = true; });
#endif #endif
// Register loop // Register loop
espurnaRegisterLoop(ntpLoop);
}
void ntpLoop() {
now();
#if BROKER_SUPPORT
static unsigned char last_minute = 60;
if (ntpSynced() && (minute() != last_minute)) {
last_minute = minute();
brokerPublish(MQTT_TOPIC_DATETIME, ntpDateTime().c_str());
}
#endif
espurnaRegisterLoop(_ntpLoop);
} }


+ 1
- 0
code/espurna/relay.ino View File

@ -390,6 +390,7 @@ void _relayConfigure() {
#if WEB_SUPPORT #if WEB_SUPPORT
void _relayWebSocketUpdate(JsonObject& root) { void _relayWebSocketUpdate(JsonObject& root) {
Serial.println("_relayWebSocketUpdate");
JsonArray& relay = root.createNestedArray("relayStatus"); JsonArray& relay = root.createNestedArray("relayStatus");
for (unsigned char i=0; i<relayCount(); i++) { for (unsigned char i=0; i<relayCount(); i++) {
relay.add(_relays[i].target_status); relay.add(_relays[i].target_status);


+ 3172
- 3163
code/espurna/static/index.html.gz.h
File diff suppressed because it is too large
View File


+ 29
- 0
code/html/custom.js View File

@ -87,6 +87,34 @@ function zeroPad(number, positions) {
return (zeros + number).slice(-positions); return (zeros + number).slice(-positions);
} }
function loadTimeZones() {
var time_zones = [
-720, -660, -600, -570, -540,
-480, -420, -360, -300, -240,
-210, -180, -120, -60, 0,
60, 120, 180, 210, 240,
270, 300, 330, 345, 360,
390, 420, 480, 510, 525,
540, 570, 600, 630, 660,
720, 765, 780, 840
];
for (var i in time_zones) {
var value = parseInt(time_zones[i], 10);
var offset = value >= 0 ? value : -value;
var text = "GMT" + (value >= 0 ? "+" : "-") +
zeroPad(parseInt(offset / 60, 10), 2) + ":" +
zeroPad(offset % 60, 2);
$("select[name='ntpOffset']").append(
$("<option></option>").
attr("value",value).
text(text)
);
}
}
function validateForm(form) { function validateForm(form) {
// password // password
@ -1169,6 +1197,7 @@ function connect(host) {
$(function() { $(function() {
initMessages(); initMessages();
loadTimeZones();
$("#menuLink").on("click", toggleMenu); $("#menuLink").on("click", toggleMenu);
$(".pure-menu-link").on("click", showPanel); $(".pure-menu-link").on("click", showPanel);


+ 6
- 4
code/html/index.html View File

@ -724,6 +724,11 @@
<fieldset> <fieldset>
<div class="pure-g">
<label class="pure-u-1 pure-u-lg-1-4">Current Time</label>
<input class="pure-u-1 pure-u-lg-3-4" name="time" type="text" readonly />
</div>
<div class="pure-g"> <div class="pure-g">
<label class="pure-u-1 pure-u-lg-1-4">NTP Server #0</label> <label class="pure-u-1 pure-u-lg-1-4">NTP Server #0</label>
<input class="pure-u-1 pure-u-lg-3-4" name="ntpServer1" type="text" tabindex="41" /> <input class="pure-u-1 pure-u-lg-3-4" name="ntpServer1" type="text" tabindex="41" />
@ -741,10 +746,7 @@
<div class="pure-g"> <div class="pure-g">
<label class="pure-u-1 pure-u-lg-1-4">Time offset</label> <label class="pure-u-1 pure-u-lg-1-4">Time offset</label>
<div class="pure-u-1 pure-u-lg-1-4"><input class="pure-u-1 pure-u-lg-23-24" name="ntpOffset" type="number" min="-11" max="14" tabindex="44" data="0" /></div>
<div class="pure-u-0 pure-u-lg-1-2"></div>
<div class="pure-u-0 pure-u-lg-1-4"></div>
<div class="pure-u-1 pure-u-lg-1-2 hint">Set to 0 for UTC time</div>
<select class="pure-u-1 pure-u-lg-1-4" name="ntpOffset" tabindex="44"></select>
</div> </div>
<div class="pure-g"> <div class="pure-g">


+ 1
- 2
code/platformio.ini View File

@ -11,14 +11,13 @@ debug_flags = -DDEBUG_ESP_CORE -DDEBUG_ESP_SSL -DDEBUG_ESP_WIFI -DDEBUG_ESP_HTTP
build_flags_512k = ${common.build_flags} -Wl,-Tesp8266.flash.512k0.ld build_flags_512k = ${common.build_flags} -Wl,-Tesp8266.flash.512k0.ld
build_flags_1m = ${common.build_flags} -Wl,-Tesp8266.flash.1m0.ld build_flags_1m = ${common.build_flags} -Wl,-Tesp8266.flash.1m0.ld
lib_deps = lib_deps =
https://github.com/xoseperez/Time
ArduinoJson ArduinoJson
https://github.com/me-no-dev/ESPAsyncTCP#a57560d https://github.com/me-no-dev/ESPAsyncTCP#a57560d
https://github.com/me-no-dev/ESPAsyncWebServer#313f337 https://github.com/me-no-dev/ESPAsyncWebServer#313f337
https://github.com/marvinroger/async-mqtt-client#v0.8.1 https://github.com/marvinroger/async-mqtt-client#v0.8.1
PubSubClient PubSubClient
Embedis Embedis
NtpClientLib
https://github.com/gmag11/NtpClient.git#2.5.0
OneWire OneWire
Brzo I2C Brzo I2C
https://github.com/krosk93/espsoftwareserial#a770677 https://github.com/krosk93/espsoftwareserial#a770677


Loading…
Cancel
Save