This commit is contained in:
Siwat Sirichai 2023-09-28 13:57:51 +07:00
parent 63d9bfb524
commit cf35e07c2f
7 changed files with 68 additions and 38 deletions

View File

@ -100,7 +100,8 @@ void IRAM_ATTR refreshInputBankB()
inputBufferB = inputBankB.read8();
}
rtctime_t ESPMega_getTime() {
rtctime_t ESPMega_getTime()
{
tmElements_t timeElement;
RTC.read(timeElement);
rtctime_t time;
@ -109,18 +110,19 @@ rtctime_t ESPMega_getTime() {
time.seconds = timeElement.Second;
time.day = timeElement.Day;
time.month = timeElement.Month;
time.year = timeElement.Year+1970;
time.year = timeElement.Year + 1970;
return time;
}
void ESPMega_setTime(int hours,int minutes, int seconds, int day, int month, int year) {
void ESPMega_setTime(int hours, int minutes, int seconds, int day, int month, int year)
{
tmElements_t timeElement;
timeElement.Hour = hours;
timeElement.Minute = minutes;
timeElement.Second = seconds;
timeElement.Day = day;
timeElement.Month = month;
timeElement.Year = year-1970;
timeElement.Year = year - 1970;
RTC.write(timeElement);
}
@ -154,24 +156,23 @@ void ESPMega_dacWrite(int id, int value)
}
}
/* void ESPMega_rtcNTPUpdate()
bool ESPMega_updateTimeFromNTP()
{
ntpTimeClient.update();
if (ntpTimeClient.updated())
struct tm timeinfo;
if (getLocalTime(&timeinfo))
{
int day = ntpTimeClient.getDay();
int month = ntpTimeClient.getMonth();
int year = ntpTimeClient.getYear();
int hours = ntpTimeClient.getHours();
int minutes = ntpTimeClient.getMinutes();
int seconds = ntpTimeClient.getSeconds();
ESPMega_RTC.setYear(year);
ESPMega_RTC.setMonth(month);
ESPMega_RTC.setDay(day);
ESPMega_RTC.setHour(hours);
ESPMega_RTC.setMinute(minutes);
ESPMega_RTC.setSecond(seconds);
rtctime_t rtctime = ESPMega_getTime();
if (rtctime.hours != timeinfo.tm_hour || rtctime.minutes != timeinfo.tm_min ||
rtctime.seconds != timeinfo.tm_sec || rtctime.day != timeinfo.tm_mday ||
rtctime.month != timeinfo.tm_mon + 1 || rtctime.year != timeinfo.tm_year + 1900)
{
ESPMega_setTime(timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec,
timeinfo.tm_mday, timeinfo.tm_mon + 1, timeinfo.tm_year + 1900);
}
} */
return true;
}
return false;
}
#endif

View File

@ -5,12 +5,13 @@
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
#include <PCF8574.h>
#ifdef ANALOG_CARD_ENABLE
#include <Adafruit_ADS1X15.h>
#include <MCP4725.h>
#include <I2C_eeprom.h>
#include <TimeLib.h>
#include <DS1307RTC.h>
#include <time.h>
#ifdef ANALOG_CARD_ENABLE
#include <Adafruit_ADS1X15.h>
#include <MCP4725.h>
#endif
#define INPUT_BANK_A_ADDRESS 0x21
@ -23,12 +24,13 @@
#define DAC1_ADDRESS 0x61
#define DAC2_ADDRESS 0x62
#define DAC3_ADDRESS 0x63
#define EEPROM_ADDRESS 0x70
#define EEPROM_ADDRESS 0x5A
//#define USE_INTERRUPT
#define INPUT_BANK_A_INTERRUPT 36
#define INPUT_BANK_B_INTERRUPT 39
extern I2C_eeprom ESPMega_EEPROM;
#define ESPMega_configNTP configTime
struct rtctime_t {
uint8_t hours;
uint8_t minutes;
@ -99,6 +101,14 @@ rtctime_t ESPMega_getTime();
*/
void ESPMega_setTime(int hours,int minutes, int seconds, int day, int month, int year);
/**
* Update the onboard RTC's time
* by using time from the NTP server
* configured with ESPMega_configNTP();
* @return true when updated successfully.
*/
bool ESPMega_updateTimeFromNTP();
#ifdef ANALOG_CARD_ENABLE
/**
* Read one of the ESPMega Analog Card's Analog Input pins (A0-A7)

View File

@ -22,5 +22,5 @@ lib_deps = adafruit/Adafruit PWM Servo Driver Library@^2.4.1
robtillaart/MCP4725@^0.3.7
robtillaart/I2C_EEPROM@^1.7.3
paulstoffregen/Time@^1.6.1
paulstoffregen/DS1307RTC
paulstoffregen/DS1307RTC@0.0.0-alpha+sha.c2590c0033
monitor_speed = 115200

View File

@ -33,6 +33,7 @@
void setup()
{
Wire.setClock(50000);
Wire.begin(14,33);
Serial.begin(115200); // Leonardo: wait for serial monitor

View File

@ -1,12 +0,0 @@
#include <ESPMegaPRO.h>
void setup() {
ESPMega_begin();
Serial.begin(115200);
ESPMega_setTime(16,35,0,16,9,2023);
}
void loop() {
delay(1000);
rtctime_t tm = ESPMega_getTime();
Serial.printf("%02d:%02d:%02d %02d/%02d/%04d\n", tm.hours,tm.minutes,tm.seconds,tm.day,tm.month,tm.year);
}

View File

@ -0,0 +1,30 @@
#include <ESPMegaPRO.h>
#include <ETH.h>
uint8_t utc_offset = 7;
IPAddress IP(192, 168, 0, 241);
IPAddress SUBNET(255, 255, 255, 0);
IPAddress GATEWAY(192, 168, 0, 1);
IPAddress DNS(10, 192, 1, 1);
void setup()
{
ESPMega_begin();
Serial.begin(115200);
ETH.begin();
ETH.config(IP, GATEWAY, SUBNET, DNS, DNS);
delay(5000);
char ntp[19];
DNS.toString().toCharArray(ntp, 19);
ESPMega_configNTP(utc_offset * 3600, 0, ntp);
ESPMega_updateTimeFromNTP();
//ESPMega_setTime(0,10,52,9,11,2008);
}
void loop()
{
rtctime_t time = ESPMega_getTime();
Serial.printf("RTC: %02d:%02d:%02d %02d/%02d/%04d\n", time.hours, time.minutes, time.seconds, time.day, time.month, time.year);
delay(1000);
}