diff --git a/README.md b/README.md
index e4c3e044..117198a0 100644
--- a/README.md
+++ b/README.md
@@ -4,9 +4,9 @@ ESPurna ("spark" in Catalan) is a custom firmware for ESP8285/ESP8266 based smar
It uses the Arduino Core for ESP8266 framework and a number of 3rd party libraries.
[![version](https://img.shields.io/badge/version-1.13.1-brightgreen.svg)](CHANGELOG.md)
-[![branch](https://img.shields.io/badge/branch-dev-orange.svg)](https://github.com/xoseperez/espurna/tree/dev/)
-[![travis](https://travis-ci.org/xoseperez/espurna.svg?branch=dev)](https://travis-ci.org/xoseperez/espurna)
-[![codacy](https://img.shields.io/codacy/grade/c9496e25cf07434cba786b462cb15f49/dev.svg)](https://www.codacy.com/app/xoseperez/espurna/dashboard)
+[![branch](https://img.shields.io/badge/branch-rtc-orange.svg)](https://github.com/xoseperez/espurna/tree/rtc/)
+[![travis](https://travis-ci.org/xoseperez/espurna.svg?branch=rtc)](https://travis-ci.org/xoseperez/espurna)
+[![codacy](https://img.shields.io/codacy/grade/c9496e25cf07434cba786b462cb15f49/rtc.svg)](https://www.codacy.com/app/xoseperez/espurna/dashboard)
[![license](https://img.shields.io/github/license/xoseperez/espurna.svg)](LICENSE)
[![donate](https://img.shields.io/badge/donate-PayPal-blue.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=xose%2eperez%40gmail%2ecom&lc=US&no_note=0¤cy_code=EUR&bn=PP%2dDonationsBF%3abtn_donate_LG%2egif%3aNonHostedGuest)
diff --git a/code/espurna/config/all.h b/code/espurna/config/all.h
index fe5124d6..d21eb007 100644
--- a/code/espurna/config/all.h
+++ b/code/espurna/config/all.h
@@ -31,7 +31,6 @@
#include "general.h"
#include "prototypes.h"
#include "sensors.h"
-#include "rtc.h"
#include "dependencies.h"
#include "progmem.h"
#include "debug.h"
diff --git a/code/espurna/config/arduino.h b/code/espurna/config/arduino.h
index e69cf669..86b774cc 100644
--- a/code/espurna/config/arduino.h
+++ b/code/espurna/config/arduino.h
@@ -89,7 +89,8 @@
//#define BLITZWOLF_BWSHP2
//#define BH_ONOFRE
//#define ITEAD_SONOFF_IFAN02
-//#define SHAPEsp
+//#define POULCH74_SHAPESP
+
//--------------------------------------------------------------------------------
// Features (values below are non-default values)
//--------------------------------------------------------------------------------
diff --git a/code/espurna/config/general.h b/code/espurna/config/general.h
index 950d33d7..f6abe9fb 100644
--- a/code/espurna/config/general.h
+++ b/code/espurna/config/general.h
@@ -1286,3 +1286,24 @@
#ifndef RFM69_IS_RFM69HW
#define RFM69_IS_RFM69HW 0
#endif
+
+//--------------------------------------------------------------------------------
+// Real Time Clock support
+// Enable support by passing RTC_SUPPORT=1 build flag
+//--------------------------------------------------------------------------------
+
+#ifndef RTC_SUPPORT
+#define RTC_SUPPORT 0 // Enable battery backed RTC for ntp
+#endif
+
+#ifndef RTC_PROVIDER
+#define RTC_PROVIDER RTC_PROVIDER_DS1307 // RTC chip (only if RTC_SUPPORT == 1)
+#endif
+
+#ifndef RTC_RECOVERY_CNT
+#define RTC_RECOVERY_CNT 10 // 0 no recovery
+#endif
+
+#ifndef RTC_NTP_SYNC_ENA
+#define RTC_NTP_SYNC_ENA 1 // enable sync RTC on NTP sync success
+#endif
diff --git a/code/espurna/config/hardware.h b/code/espurna/config/hardware.h
index ba4d79a2..5a2ab6b4 100644
--- a/code/espurna/config/hardware.h
+++ b/code/espurna/config/hardware.h
@@ -112,20 +112,19 @@
#define I2C_SDA_PIN 12 // D6
#define I2C_SCL_PIN 14 // D5
- // -----------------------------------------------------------------------------
- // RTC_SUPPORT example
- // -----------------------------------------------------------------------------
- //#define RTC_SUPPORT 1 // for test
- //#define RTC_PROVIDER RTC_PROVIDER_DS1307 // for test
- //#define RTC_RECOVERY_CNT 3 // 0 no recovery default 10
- //#define RTC_NTP_SYNC_ENA 1 // 0 rtc not synced with ntp default 1
-
#elif defined(WEMOS_D1_TARPUNA_SHIELD)
// Info
#define MANUFACTURER "WEMOS"
#define DEVICE "D1_TARPUNA_SHIELD"
+ // RTC support
+ #define I2C_SUPPORT 1
+ #define RTC_SUPPORT 1
+ #define RTC_PROVIDER RTC_PROVIDER_DS1307
+ #define RTC_RECOVERY_CNT 3 // 0 no recovery default 10
+ #define RTC_NTP_SYNC_ENA 1 // 0 rtc not synced with ntp default 1
+
// -----------------------------------------------------------------------------
// ESPurna
// -----------------------------------------------------------------------------
@@ -2431,6 +2430,24 @@
#define HLW8012_POWER_RATIO 3414290
#define HLW8012_INTERRUPT_ON FALLING
+#elif defined(POULCH74_SHAPESP)
+
+ // Info
+ #define MANUFACTURER "POULCH74"
+ #define DEVICE "SHAPESP"
+
+ // LEDs
+ #define LED1_PIN 2 //15 to free pin 2 which can be used for dallas support or water sensor for example
+ #define LED1_PIN_INVERSE 1
+
+ #define I2C_SDA_PIN 4
+ #define I2C_SCL_PIN 5
+
+ // RTC support
+ #define RTC_SUPPORT 1 // for test
+ #define RTC_PROVIDER RTC_PROVIDER_DS3231 // for test
+ #define RTC_RECOVERY_CNT 3 // 0 no recovery
+ #define RTC_NTP_SYNC_ENA 1 // 0 rtc not synced with ntp
// -----------------------------------------------------------------------------
// TEST boards (do not use!!)
@@ -2439,8 +2456,8 @@
#elif defined(TRAVIS01)
// Info
- #define MANUFACTURER "TravisCI"
- #define DEVICE "Virtual board 01"
+ #define MANUFACTURER "TravisCI"
+ #define DEVICE "Virtual board 01"
// Some buttons - pin 0
#define BUTTON1_PIN 0
@@ -2603,56 +2620,6 @@
#define NETBIOS_SUPPORT 1
#define SSDP_SUPPORT 1
-#elif defined(SHAPEsp)
-
- // Info
- #define MANUFACTURER "POULCH74"
- #define DEVICE "SHAPEsp"
-
- // Buttons
-
- //#define BUTTON1_PIN 2
- //#define BUTTON1_MODE BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH | BUTTON_ONCE
- //#define BUTTON1_PRESS BUTTON_MODE_TOGGLE
- //#define BUTTON1_CLICK BUTTON_MODE_TOGGLE
- //#define BUTTON1_DBLCLICK BUTTON_MODE_TOGGLE
- //#define BUTTON1_LNGCLICK BUTTON_MODE_TOGGLE
- //#define BUTTON1_LNGLNGCLICK BUTTON_MODE_TOGGLE
-
- //#define BUTTON1_RELAY 1
-
- // Relays
- //#define RELAY1_PIN 14
- //#define RELAY1_PIN2 12
- //#define RELAY1_PINP 13 // GPIO_NONE to leave it unused, so no pulse on start valve (autostop not used)
- //#define RELAY1_TYPE RELAY_TYPE_HBRIDGE
- // pulse duration for pinP pulse to start valve
- //#define RELAY1_HBPULSE 100
-
- // Relays
- //#define RELAY2_PIN 98 // fake pin for relay
- //#define RELAY2_TYPE RELAY_TYPE_BTNRESET
- //#define RELAY2_BUTTON 1
-
- // LEDs
- #define LED1_PIN 2 //15 to free pin 2 which can be used for dallas support or water sensor for example
- #define LED1_PIN_INVERSE 1
-
- #define I2C_SDA_PIN 4
- #define I2C_SCL_PIN 5
-
- //#define ADC_MODE_VALUE ADC_VCC_CUSTOM
- //#define ADC_VCC_CUSTOM_MUL (15.63)
- //#define ADC_VCC_CUSTOM_ADD (0)
-
- // -----------------------------------------------------------------------------
- // RTC_SUPPORT
- // -----------------------------------------------------------------------------
- #define RTC_SUPPORT 1 // for test
- #define RTC_PROVIDER RTC_PROVIDER_DS3231 // for test
- #define RTC_RECOVERY_CNT 3 // 0 no recovery
- #define RTC_NTP_SYNC_ENA 1 // 0 rtc not synced with ntp
-
#endif
// -----------------------------------------------------------------------------
diff --git a/code/espurna/config/prototypes.h b/code/espurna/config/prototypes.h
index 5f4436c9..9af039df 100644
--- a/code/espurna/config/prototypes.h
+++ b/code/espurna/config/prototypes.h
@@ -78,6 +78,13 @@ int16_t i2c_read_int16(uint8_t address, uint8_t reg);
int16_t i2c_read_int16_le(uint8_t address, uint8_t reg);
void i2c_read_buffer(uint8_t address, uint8_t * buffer, size_t len);
+// -----------------------------------------------------------------------------
+// RTC
+// -----------------------------------------------------------------------------
+#include
+time_t rtcGetTime();
+uint8_t rtcSetTime(time_t nt);
+
// -----------------------------------------------------------------------------
// MQTT
// -----------------------------------------------------------------------------
diff --git a/code/espurna/config/rtc.h b/code/espurna/config/rtc.h
deleted file mode 100644
index 7d6e95a9..00000000
--- a/code/espurna/config/rtc.h
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
-
-RTC support module
-
-Copyright (C) 2018 by Pavel Chauzov
-
-*/
-
-// -----------------------------------------------------------------------------
-// RTC3231_SUPPORT
-// -----------------------------------------------------------------------------
-
-#ifndef RTC_SUPPORT
-#define RTC_SUPPORT 0 // enable battery backed RTC for ntp
-#define RTC_PROVIDER RTC_DUMMY
-#endif
-
-#ifndef RTC_RECOVERY_CNT
-#define RTC_RECOVERY_CNT 10 // 0 no recovery
-#endif
-
-#ifndef RTC_NTP_SYNC_ENA
-#define RTC_NTP_SYNC_ENA 1 // enable sync RTC on NTP sync success
-#endif
-
-
-#if RTC_SUPPORT
-
-#if RTC_PROVIDER == RTC_PROVIDER_DS3231
- #include "../rtc/ds3231.h"
-#elif RTC_PROVIDER == RTC_PROVIDER_DS1307
- #include "../rtc/ds1307.h"
-#else
- #include "../rtc/dummy.h"
-#endif
-
-#endif
diff --git a/code/espurna/config/types.h b/code/espurna/config/types.h
index c2729482..0ad349b7 100644
--- a/code/espurna/config/types.h
+++ b/code/espurna/config/types.h
@@ -305,6 +305,6 @@
//-----------------------------------------------------------------------------
// RTC providers
//-----------------------------------------------------------------------------
-#define RTC_PROVIDER_DUMMY 0
+
#define RTC_PROVIDER_DS3231 1
#define RTC_PROVIDER_DS1307 2
diff --git a/code/espurna/espurna.ino b/code/espurna/espurna.ino
index adc77fcf..86233401 100644
--- a/code/espurna/espurna.ino
+++ b/code/espurna/espurna.ino
@@ -123,11 +123,14 @@ void setup() {
#if SSDP_SUPPORT
ssdpSetup();
#endif
+ #if I2C_SUPPORT
+ i2cSetup();
+ #endif
#if NTP_SUPPORT
ntpSetup();
#endif
- #if I2C_SUPPORT
- i2cSetup();
+ #if RTC_SUPPORT
+ rtcSetup(); // Always after NTP and I2C
#endif
#ifdef ITEAD_SONOFF_RFBRIDGE
rfbSetup();
diff --git a/code/espurna/mqtt.ino b/code/espurna/mqtt.ino
index fa02afde..bf71da05 100644
--- a/code/espurna/mqtt.ino
+++ b/code/espurna/mqtt.ino
@@ -594,8 +594,8 @@ void mqttFlush() {
_mqttBuildTree(root, 255);
// Add extra propeties
- #if NTP_SUPPORT && MQTT_ENQUEUE_DATETIME
- if (ntpSynced()) root[MQTT_TOPIC_TIME] = ntpDateTime();
+ #if MQTT_ENQUEUE_DATETIME
+ if (rtcReady()) root[MQTT_TOPIC_TIME] = rtcDateTime();
#endif
#if MQTT_ENQUEUE_MAC
root[MQTT_TOPIC_MAC] = WiFi.macAddress();
@@ -751,7 +751,7 @@ void mqttReset() {
void mqttSetup() {
_mqttBackwards();
-
+
DEBUG_MSG_P(PSTR("[MQTT] Async %s, SSL %s, Autoconnect %s\n"),
MQTT_USE_ASYNC ? "ENABLED" : "DISABLED",
ASYNC_TCP_SSL_ENABLED ? "ENABLED" : "DISABLED",
diff --git a/code/espurna/ntp.ino b/code/espurna/ntp.ino
index fc785697..4b237ad4 100644
--- a/code/espurna/ntp.ino
+++ b/code/espurna/ntp.ino
@@ -17,11 +17,6 @@ unsigned long _ntp_start = 0;
bool _ntp_update = false;
bool _ntp_configure = false;
-#if RTC_SUPPORT && RTC_NTP_SYNC_ENA
-bool _rtc_update = false;
-#endif
-
-
// -----------------------------------------------------------------------------
// NTP
// -----------------------------------------------------------------------------
@@ -39,26 +34,17 @@ void _ntpWebSocketOnSend(JsonObject& root) {
root["ntpOffset"] = getSetting("ntpOffset", NTP_TIME_OFFSET).toInt();
root["ntpDST"] = getSetting("ntpDST", NTP_DAY_LIGHT).toInt() == 1;
root["ntpRegion"] = getSetting("ntpRegion", NTP_DST_REGION).toInt();
- if (ntpSynced()) root["now"] = now();
+ if (rtcReady()) root["now"] = now();
}
#endif
void _ntpStart() {
-
_ntp_start = 0;
-
NTP.begin(getSetting("ntpServer", NTP_SERVER));
-
-#if RTC_SUPPORT
- // set alter sync provider. two attempts for NTP synchro at start occure...
- setSyncProvider(ntp_getTime);
-#endif
-
NTP.setInterval(NTP_SYNC_INTERVAL, NTP_UPDATE_INTERVAL);
NTP.setNTPTimeout(NTP_TIMEOUT);
_ntpConfigure();
-
}
void _ntpConfigure() {
@@ -99,15 +85,17 @@ void _ntpUpdate() {
wsSend(_ntpWebSocketOnSend);
#endif
- if (ntpSynced()) {
+ if (rtcReady()) {
+
time_t t = now();
+
#if RTC_SUPPORT && RTC_NTP_SYNC_ENA
- // sync/update rtc here!!!!!!!!!!!!
- if(_rtc_update) setTime_rtc(t);
+ rtcSetTime(t);
#endif
- DEBUG_MSG_P(PSTR("[NTP] UTC Time : %s\n"), (char *) ntpDateTime(ntpLocal2UTC(t)).c_str());
- DEBUG_MSG_P(PSTR("[NTP] Local Time: %s\n"), (char *) ntpDateTime(t).c_str());
+ DEBUG_MSG_P(PSTR("[NTP] UTC Time : %s\n"), (char *) rtcDateTime(ntpLocal2UTC(t)).c_str());
+ DEBUG_MSG_P(PSTR("[NTP] Local Time: %s\n"), (char *) rtcDateTime(t).c_str());
+
}
}
@@ -122,9 +110,9 @@ void _ntpLoop() {
#if BROKER_SUPPORT
static unsigned char last_minute = 60;
- if (ntpSynced() && (minute() != last_minute)) {
+ if (rtcReady() && (minute() != last_minute)) {
last_minute = minute();
- brokerPublish(MQTT_TOPIC_DATETIME, ntpDateTime().c_str());
+ brokerPublish(MQTT_TOPIC_DATETIME, rtcDateTime(now()).c_str());
}
#endif
@@ -143,24 +131,6 @@ void _ntpBackwards() {
// -----------------------------------------------------------------------------
-bool ntpSynced() {
- return (year() > 2017);
-}
-
-String ntpDateTime(time_t t) {
- char buffer[20];
- snprintf_P(buffer, sizeof(buffer),
- PSTR("%04d-%02d-%02d %02d:%02d:%02d"),
- year(t), month(t), day(t), hour(t), minute(t), second(t)
- );
- return String(buffer);
-}
-
-String ntpDateTime() {
- if (ntpSynced()) return ntpDateTime(now());
- return String();
-}
-
time_t ntpLocal2UTC(time_t local) {
int offset = getSetting("ntpOffset", NTP_TIME_OFFSET).toInt();
if (NTP.isSummerTime()) offset += 60;
@@ -186,20 +156,16 @@ void ntpSetup() {
_ntp_update = false;
} else {
_ntp_update = true;
- #if RTC_SUPPORT && RTC_NTP_SYNC_ENA
- _rtc_update = true;
- #endif
-
}
});
wifiRegister([](justwifi_messages_t code, char * parameter) {
if (code == MESSAGE_CONNECTED) _ntp_start = millis() + NTP_START_DELAY;
#if RTC_SUPPORT
- // system time from local RTC, but still try recovery if enabled (without success)
- else
- if(code == MESSAGE_ACCESSPOINT_CREATED) _ntp_start = millis() + NTP_START_DELAY;
- #endif
+ // system time from local RTC, but still try recovery if enabled (without success)
+ else
+ if (code == MESSAGE_ACCESSPOINT_CREATED) _ntp_start = millis() + NTP_START_DELAY;
+ #endif
});
@@ -211,12 +177,6 @@ void ntpSetup() {
// Register loop
espurnaRegisterLoop(_ntpLoop);
-
- #if RTC_SUPPORT
- #if TERMINAL_SUPPORT
- _rtcInitCommands();
- #endif
- #endif
}
diff --git a/code/espurna/rtc.ino b/code/espurna/rtc.ino
index b67490db..419866a2 100644
--- a/code/espurna/rtc.ino
+++ b/code/espurna/rtc.ino
@@ -6,30 +6,19 @@ Copyright (C) 2018 by Pavel Chauzov
in debug terminal - RTC return current rtc time
- RTC YYYY-MM-DD DoW HH:MM:SS setup rtc
-
+
*/
#if RTC_SUPPORT
-static int _rtc_recovery = 0;
+#if RTC_PROVIDER == RTC_PROVIDER_DS3231
+ #include "rtc/ds3231.h"
+#elif RTC_PROVIDER == RTC_PROVIDER_DS1307
+ #include "rtc/ds1307.h"
+#endif
-static time_t ntp_getTime() {
- time_t tm = 0;
- if(_rtc_recovery == 0) { tm = NTP.getTime(); }
- if((_rtc_recovery !=0) || (tm == 0)) {
- _rtc_recovery++;
- tm = getTime_rtc();
- // signal ntp loop to update clock but not rtc...
- _ntp_update = true;
- #if RTC_SUPPORT && RTC_NTP_SYNC_ENA
- _rtc_update = false;
- #endif
- }
- if(_rtc_recovery > RTC_RECOVERY_CNT) { _rtc_recovery = RTC_RECOVERY_CNT ? 0:1; }
- DEBUG_MSG_P(PSTR("[NTP] RTC Time : %s\n"), (char *) ntpDateTime(tm).c_str());
- return tm;
-}
+static int _rtc_recovery = 0;
//------------------------------------------------------------------------------
// Settings
@@ -55,15 +44,18 @@ String _rtc_getValue(String data, char sep, int idx) {
void _rtcInitCommands() {
settingsRegisterCommand(F("RTC"), [](Embedis* e) {
+
String rtc;
time_t t;
tmElements_t tm;
+
if (e->argc == 1) {
- t = getTime_rtc();
- DEBUG_MSG_P(PSTR("[NTP] GET RTC Local Time: %s\n"), (char *) ntpDateTime(t).c_str());
+ t = rtcGetTime();
+ DEBUG_MSG_P(PSTR("[RTC] Local Time: %s\n"), (char *) rtcDateTime(t).c_str());
}
if (e->argc > 3) {
+
String sdate = String(e->argv[1]);
String sdow = String(e->argv[2]);
String stime = String(e->argv[3]);
@@ -79,17 +71,42 @@ void _rtcInitCommands() {
tm.Year = y2kYearToTm(_rtc_getValue(sdate,'-',0).toInt()-2000);
t = makeTime(tm);
- setTime_rtc(t);
+ rtcSetTime(t);
setTime(t);
- DEBUG_MSG_P(PSTR("[NTP] SET RTC Local Time: %s\n"), (char *) ntpDateTime(t).c_str());
+ DEBUG_MSG_P(PSTR("[RTC] Local Time: %s\n"), (char *) rtcDateTime(t).c_str());
+
}
+
DEBUG_MSG_P(PSTR("+OK\n"));
+
});
}
#endif // TERMINAL_SUPPORT
+static time_t _rtcSync() {
+ return rtcGetTime(); // defined in the driver
+}
+
+// -----------------------------------------------------------------------------
+// Setup
+// -----------------------------------------------------------------------------
+
+void rtcSetup() {
+
+ #if TERMINAL_SUPPORT
+ _rtcInitCommands();
+ #endif // TERMINAL_SUPPORT
+
+ // overwrite sync provider,
+ // there might be two attempts for NTP synchro on boot
+ setSyncProvider(_rtcSync);
+
+ // Dump current time from local RTC
+ DEBUG_MSG_P(PSTR("[RTC] Local Time: %s\n"), (char *) rtcDateTime(rtcGetTime()).c_str());
+
+}
-#endif
\ No newline at end of file
+#endif // RTC_SUPPORT
diff --git a/code/espurna/rtc/README.md b/code/espurna/rtc/README.md
index a09e7d90..04911b03 100644
--- a/code/espurna/rtc/README.md
+++ b/code/espurna/rtc/README.md
@@ -12,6 +12,6 @@
1 rtc synced with ntp on ntp success
## to add new rtc ic support
- see ds3231.h as example, create your_ic.h file and write two
- functions time_t getTime_rtc() and uint8_t setTime_rtc(time_t nt)
+ functions time_t rtcGetTime() and uint8_t rtcSetTime(time_t nt)
- define your provider constant in config/types.h
- add your provider section in config/rtc.h
diff --git a/code/espurna/rtc/ds1307.h b/code/espurna/rtc/ds1307.h
index 660e2a8c..be905f8a 100644
--- a/code/espurna/rtc/ds1307.h
+++ b/code/espurna/rtc/ds1307.h
@@ -10,9 +10,6 @@ Copyright (C) 2018 by Pavel Chauzov
#pragma once
-#undef I2C_SUPPORT
-#define I2C_SUPPORT 1 // Explicitly request I2C support.
-
#include
#define DS1307ADDR 0x68
@@ -20,7 +17,7 @@ Copyright (C) 2018 by Pavel Chauzov
#define _bcdToDec(val) ((uint8_t) ((val / 16 * 10) + (val % 16)))
#define _decToBcd(val) ((uint8_t) ((val / 10 * 16) + (val % 10)))
-time_t getTime_rtc() {
+time_t rtcGetTime() {
uint8_t data[7];
tmElements_t tm;
@@ -37,7 +34,7 @@ time_t getTime_rtc() {
return makeTime(tm);
}
-uint8_t setTime_rtc(time_t nt) {
+uint8_t rtcSetTime(time_t nt) {
uint8_t data[8];
tmElements_t ct;
breakTime(nt, ct);
diff --git a/code/espurna/rtc/ds3231.h b/code/espurna/rtc/ds3231.h
index c1d93e30..6cde9f7e 100644
--- a/code/espurna/rtc/ds3231.h
+++ b/code/espurna/rtc/ds3231.h
@@ -4,20 +4,17 @@ ds3231 support module
Copyright (C) 2018 by Pavel Chauzov
*/
-#pragma once
-#undef I2C_SUPPORT
-#define I2C_SUPPORT 1 // Explicitly request I2C support.
+#pragma once
#include
-
#define DS3231ADDR 0x68
#define _bcdToDec(val) ((uint8_t) ((val / 16 * 10) + (val % 16)))
#define _decToBcd(val) ((uint8_t) ((val / 10 * 16) + (val % 10)))
-time_t getTime_rtc() {
+time_t rtcGetTime() {
uint8_t data[7];
tmElements_t tm;
@@ -34,7 +31,7 @@ time_t getTime_rtc() {
return makeTime(tm);
}
-uint8_t setTime_rtc(time_t nt) {
+uint8_t rtcSetTime(time_t nt) {
uint8_t data[8];
tmElements_t ct;
breakTime(nt, ct);
diff --git a/code/espurna/rtc/dummy.h b/code/espurna/rtc/dummy.h
deleted file mode 100644
index fce7421d..00000000
--- a/code/espurna/rtc/dummy.h
+++ /dev/null
@@ -1,30 +0,0 @@
-/*
-
-dummyRTC support module
-
-Copyright (C) 2018 by Pavel Chauzov
-
-*/
-#include
-
-#define _bcdToDec(val) ((uint8_t) ((val / 16 * 10) + (val % 16)))
-#define _decToBcd(val) ((uint8_t) ((val / 10 * 16) + (val % 10)))
-
-time_t getTime_rtc() {
-
- tmElements_t tm;
-
- tm.Second = _bcdToDec(1);
- tm.Minute = _bcdToDec(0);
- tm.Hour = _bcdToDec(0);
- tm.Wday = _bcdToDec(0);
- tm.Day = _bcdToDec(1);
- tm.Month = _bcdToDec(1);
- tm.Year = y2kYearToTm(16);
-
- return makeTime(tm);
-}
-
-uint8_t setTime_rtc(time_t nt) {
- return 0;
-}
diff --git a/code/espurna/scheduler.ino b/code/espurna/scheduler.ino
index 003a6f69..450428dd 100644
--- a/code/espurna/scheduler.ino
+++ b/code/espurna/scheduler.ino
@@ -194,7 +194,7 @@ void _schCheck() {
void _schLoop() {
// Check time has been sync'ed
- if (!ntpSynced()) return;
+ if (!rtcReady()) return;
// Check schedules every minute at hh:mm:00
static unsigned long last_minute = 60;
diff --git a/code/espurna/sensor.ino b/code/espurna/sensor.ino
index 06e40e7a..404313c3 100644
--- a/code/espurna/sensor.ino
+++ b/code/espurna/sensor.ino
@@ -287,11 +287,9 @@ void _sensorPost() {
}
void _sensorReset() {
- #if NTP_SUPPORT
- if (ntpSynced()) {
- _sensor_energy_reset_ts = String(" (since ") + ntpDateTime() + String(")");
- }
- #endif
+ if (rtcReady()) {
+ _sensor_energy_reset_ts = String(" (since ") + rtcDateTime() + String(")");
+ }
}
// -----------------------------------------------------------------------------
diff --git a/code/espurna/utils.ino b/code/espurna/utils.ino
index 2cc34d08..b47da0c7 100644
--- a/code/espurna/utils.ino
+++ b/code/espurna/utils.ino
@@ -103,6 +103,23 @@ String buildTime() {
}
+bool rtcReady() {
+ return (year() > 2017);
+}
+
+String rtcDateTime(time_t t) {
+ char buffer[20];
+ snprintf_P(buffer, sizeof(buffer),
+ PSTR("%04d-%02d-%02d %02d:%02d:%02d"),
+ year(t), month(t), day(t), hour(t), minute(t), second(t)
+ );
+ return String(buffer);
+}
+
+String rtcDateTime() {
+ if (rtcReady()) return rtcDateTime(now());
+ return String();
+}
unsigned long getUptime() {
@@ -141,7 +158,7 @@ void heartbeat() {
DEBUG_MSG_P(PSTR("[MAIN] Power: %lu mV\n"), ESP.getVcc());
#endif
#if NTP_SUPPORT
- if (ntpSynced()) DEBUG_MSG_P(PSTR("[MAIN] Time: %s\n"), (char *) ntpDateTime().c_str());
+ if (rtcReady()) DEBUG_MSG_P(PSTR("[MAIN] Time: %s\n"), (char *) rtcDateTime().c_str());
#endif
}
@@ -179,7 +196,7 @@ void heartbeat() {
mqttSend(MQTT_TOPIC_UPTIME, String(uptime_seconds).c_str());
#endif
#if (HEARTBEAT_REPORT_DATETIME) && (NTP_SUPPORT)
- if (ntpSynced()) mqttSend(MQTT_TOPIC_DATETIME, ntpDateTime().c_str());
+ if (rtcReady()) mqttSend(MQTT_TOPIC_DATETIME, rtcDateTime().c_str());
#endif
#if (HEARTBEAT_REPORT_FREEHEAP)
mqttSend(MQTT_TOPIC_FREEHEAP, String(free_heap).c_str());
diff --git a/code/espurna/web.ino b/code/espurna/web.ino
index 7086bffd..e002044c 100644
--- a/code/espurna/web.ino
+++ b/code/espurna/web.ino
@@ -91,7 +91,7 @@ void _onGetConfig(AsyncWebServerRequest *request) {
response->printf("{\n\"app\": \"%s\"", APP_NAME);
response->printf(",\n\"version\": \"%s\"", APP_VERSION);
response->printf(",\n\"backup\": \"1\"");
- response->printf(",\n\"timestamp\": \"%s\"", ntpDateTime().c_str());
+ response->printf(",\n\"timestamp\": \"%s\"", rtcDateTime().c_str());
// Write the keys line by line (not sorted)
unsigned long count = settingsKeyCount();
diff --git a/code/espurna/ws.ino b/code/espurna/ws.ino
index aab9be2e..6caa690e 100644
--- a/code/espurna/ws.ino
+++ b/code/espurna/ws.ino
@@ -288,7 +288,7 @@ void _wsUpdate(JsonObject& root) {
root["vcc"] = ESP.getVcc();
#endif
#if NTP_SUPPORT
- if (ntpSynced()) root["now"] = now();
+ if (rtcReady()) root["now"] = now();
#endif
}