Add support for I2C RTC chips DS3231 and DS1307 through Makuna/RTC lib

pull/142/head
Jeff Lehman 1 year ago
parent e48d1cd2ee
commit 478396507a

@ -45,14 +45,23 @@
#define FDRS_DEBUG // Enable USB-Serial debugging
// I2C - OLED or RTC
#define USE_I2C
#define I2C_SDA 21
#define I2C_SCL 22
// OLED -- Displays console debugging messages on an SSD1306 I²C OLED
///#define USE_OLED
#define OLED_HEADER "FDRS"
#define OLED_PAGE_SECS 30
#define OLED_SDA 4
#define OLED_SCL 15
#define OLED_RST 16
// RTC - I2C
// #define USE_RTC_DS3231
// #define RTC_ADDR 0x57
// #define USE_RTC_DS1307
// #define RTC_ADDR 0x68
// UART data interface pins (if available)
#define RXD2 14
#define TXD2 15

@ -45,14 +45,23 @@
#define FDRS_DEBUG // Enable USB-Serial debugging
// I2C - OLED or RTC
#define USE_I2C
#define I2C_SDA 21
#define I2C_SCL 22
// OLED -- Displays console debugging messages on an SSD1306 I²C OLED
///#define USE_OLED
#define OLED_HEADER "FDRS"
#define OLED_PAGE_SECS 30
#define OLED_SDA 4
#define OLED_SCL 15
#define OLED_RST 16
// RTC - I2C
// #define USE_RTC_DS3231
// #define RTC_ADDR 0x57
// #define USE_RTC_DS1307
// #define RTC_ADDR 0x68
// UART data interface pins (if available)
#define RXD2 14
#define TXD2 15

@ -45,14 +45,23 @@
#define FDRS_DEBUG // Enable USB-Serial debugging
// I2C - OLED or RTC
#define USE_I2C
#define I2C_SDA 21
#define I2C_SCL 22
// OLED -- Displays console debugging messages on an SSD1306 I²C OLED
///#define USE_OLED
#define OLED_HEADER "FDRS"
#define OLED_PAGE_SECS 30
#define OLED_SDA 4
#define OLED_SCL 15
#define OLED_RST 16
// RTC - I2C
// #define USE_RTC_DS3231
// #define RTC_ADDR 0x57
// #define USE_RTC_DS1307
// #define RTC_ADDR 0x68
// UART data interface pins (if available)
#define RXD2 14
#define TXD2 15

@ -45,14 +45,23 @@
#define FDRS_DEBUG // Enable USB-Serial debugging
// I2C - OLED or RTC
#define USE_I2C
#define I2C_SDA 21
#define I2C_SCL 22
// OLED -- Displays console debugging messages on an SSD1306 I²C OLED
///#define USE_OLED
#define OLED_HEADER "FDRS"
#define OLED_PAGE_SECS 30
#define OLED_SDA 4
#define OLED_SCL 15
#define OLED_RST 16
// RTC - I2C
// #define USE_RTC_DS3231
// #define RTC_ADDR 0x57
// #define USE_RTC_DS1307
// #define RTC_ADDR 0x68
// UART data interface pins (if available)
#define RXD2 14
#define TXD2 15

@ -65,6 +65,9 @@ void resendLog();
void releaseLogBuffer();
void printFDRS(DataReading*, int);
#ifdef USE_I2C
#include <Wire.h>
#endif
#ifdef USE_OLED
#include "fdrs_oled.h"
#endif
@ -139,6 +142,12 @@ void beginFDRS()
Serial.begin(115200);
UART_IF.begin(115200, SERIAL_8N1, RXD2, TXD2);
#endif
#ifdef USE_I2C
Wire.begin(I2C_SDA, I2C_SCL);
#endif
#if defined(USE_RTC_DS3231) || defined(USE_RTC_DS1307)
begin_rtc();
#endif
#ifdef USE_OLED
init_oled();
DBG("Display initialized!");

@ -271,7 +271,7 @@ void sendTimeLoRa(uint8_t *address) {
// DBG("Sending time " + String(now) + " to LoRa address 0x" + String(*address, HEX) + String(*(address + 1), HEX));
LoRaAddress = ((int16_t) *address << 8) + *(address + 1);
DBG("Sending time " + String(now) + " to LoRa address 0x" + String(LoRaAddress, HEX));
DBG("Sending time to LoRa address 0x" + String(LoRaAddress, HEX));
SystemPacket spTimeLoRa = {.cmd = cmd_time, .param = now};
transmitLoRa(&LoRaAddress, &spTimeLoRa, 1);

@ -31,6 +31,7 @@ time_t now; // Current time in UTC - number of seconds
struct tm timeinfo; // Structure containing time elements
struct timeval tv;
bool validTimeFlag = false; // Indicate whether we have reliable time
bool validRtcFlag = false; // Is RTC date and time valid?
time_t lastNTPFetchSuccess = 0; // Last time that a successful NTP fetch was made
bool isDST; // Keeps track of Daylight Savings Time vs Standard Time
long slewSecs = 0; // When time is set this is the number of seconds the time changes
@ -42,7 +43,61 @@ time_t lastDstCheck = 0;
void sendTimeLoRa();
void printTime();
esp_err_t sendTimeESPNow();
bool setTime(time_t);
#ifdef USE_RTC_DS3231
#include <RtcDS3231.h>
RtcDS3231<TwoWire> rtc(Wire);
#elif defined(USE_RTC_DS1307)
#include <RtcDS3231.h>
RtcDS3231<TwoWire> rtc(Wire);
#endif
#if defined(USE_RTC_DS3231) || defined(USE_RTC_DS1307)
void begin_rtc() {
DBG("Starting RTC");
rtc.Begin();
// Is Date and time valid?
if(!rtc.IsDateTimeValid()) {
uint8_t err = rtc.LastError();
if(err != 0) {
// Common Causes:
// 1) first time you ran and the device wasn't running yet
// 2) the battery on the device is low or even missing
Serial.println("RTC error: Date and Time not valid! Err: " + String(err));
validRtcFlag = false;
}
}
else {
validRtcFlag = true;
}
// Is RTC running?
if(!rtc.GetIsRunning()) {
uint8_t err = rtc.LastError();
if(err != 0) {
DBG("RTC was not actively running, starting now. Err: " + String(err));
rtc.SetIsRunning(true);
validRtcFlag = false;
}
}
if(validRtcFlag) {
// Set date and time on the system
DBG("Using Date and Time from RTC.");
setTime(rtc.GetDateTime().Unix32Time());
printTime();
}
// never assume the Rtc was last configured by you, so
// just clear them to your needed state
rtc.Enable32kHzPin(false);
rtc.SetSquareWavePin(DS3231SquareWavePin_ModeNone);
}
#endif // USE_RTC_DS3231 || USE_RTC_DS1307
bool validTime() {
if(now < 1672000000 || (millis() - lastNTPFetchSuccess > (24*60*60*1000))) {
@ -80,7 +135,7 @@ void printTime() {
}
void checkDST() {
if(validTime() && (time(NULL) - lastDstCheck > 5)) {
if(validTime() && ((time(NULL) - lastDstCheck > 5) || lastDstCheck == 0)) {
lastDstCheck = time(NULL);
int dstFlag = -1;
localtime_r(&now, &timeinfo);
@ -212,9 +267,14 @@ bool setTime(time_t currentTime) {
// Check for DST/STD time and adjust accordingly
checkDST();
tv.tv_sec = now;
#if defined(ESP32) || defined(ESP8266) // settimeofday may only work with Espressif chips
#if defined(ESP32) || defined(ESP8266) // settimeofday may only work with Espressif chips
settimeofday(&tv,NULL); // set the RTC time
#endif
#endif
#if defined(USE_RTC_DS3231) || defined(USE_RTC_DS1307)
RtcDateTime rtcNow;
rtcNow.InitWithUnix32Time(now);
rtc.SetDateTime(rtcNow);
#endif
// Uncomment below to send time and slew rate to the MQTT server
// loadFDRS(now, TIME_T, 111);
// loadFDRS(slewSecs, TIME_T, 111);

@ -4,7 +4,7 @@
#define DISPLAY_PAGES 4
String debug_buffer[5] = {"", "", "", "", ""};
SSD1306Wire display(0x3c, OLED_SDA, OLED_SCL); // ADDRESS, SDA, SCL
SSD1306Wire display(0x3c, I2C_SDA, I2C_SCL); // ADDRESS, SDA, SCL
unsigned long displayEvent = 0;
uint8_t displayPage = 0;
@ -137,7 +137,7 @@ void init_oled(){
digitalWrite(OLED_RST, LOW);
delay(30);
digitalWrite(OLED_RST, HIGH);
Wire.begin(OLED_SDA, OLED_SCL);
Wire.begin(I2C_SDA, I2C_SCL);
display.init();
display.flipScreenVertically();
draw_OLED_header();

Loading…
Cancel
Save