Fixed RTC and Ethernet drivers, with NTP

This commit is contained in:
Emanuele Trabattoni
2025-07-10 21:48:30 +02:00
parent 7fd4a284af
commit 208f5f7534
5 changed files with 160 additions and 26 deletions

View File

@@ -6,15 +6,11 @@
#include <RS485_Driver.h>
#include <TCA9554PWR_Driver.h>
#include <PCF85063_Driver.h>
#include <ETH.h>
#include <ETH_Driver.h>
#include "utils.h"
/////////////// GLOBALS ///////////////
uint16_t k(0);
bool v(true);
void setup()
{
Serial.begin(9600);
@@ -33,22 +29,37 @@ void loop()
auto relays = drivers::TCA9554PWR(i2c, TCA9554_ADDRESS);
auto bus = drivers::MODBUS(9600, SERIAL_8N1);
auto rtc = drivers::PCF85063(i2c, PCF85063_ADDRESS);
auto eth = drivers::Ethernet("waveshare-test");
Network.onEvent([&eth](arduino_event_id_t event, arduino_event_info_t info)
{ eth.onEvent(event, info); });
while (!eth.isConnected())
{
LOG_WARN("Waiting for Ethernet");
delay(1000);
}
// Set RTC time at startup
time_t ntpTime;
drivers::PCF85063::datetime_t dt;
eth.getNtpTime(ntpTime);
rtc.setDatetime(drivers::PCF85063::fromEpoch(ntpTime));
////////////////////////////////////////
///////// MAIN LOOP INSIDE LOOP ////////
////////////////////////////////////////
while (true)
{
LOG_INFO("[", k++, "] Loop");
std::vector<uint16_t> results;
std::vector<bool> values;
drivers::PCF85063::datetime_t dt;
rtc.readDatetime(dt);
LOG_INFO("Current Datetime", rtc.getTimeStr().c_str());
eth.getNtpTime(ntpTime);
dt = drivers::PCF85063::fromEpoch(ntpTime);
LOG_INFO("Netwrok Datetime", rtc.datetime2str(dt).c_str());
LOG_INFO("Current Datetime", rtc.getTimeStr().c_str());
if (bus.readHoldingRegisters(tempBoardAddr, baseRegister, 8, results))
{
@@ -58,20 +69,8 @@ void loop()
}
results.clear();
}
delay(5000);
LOG_INFO("\n\n====>Write Status 1");
bus.writeCoils(relayBoardAddr, 0x00, {true, false, true, false, true, false, true, false});
bus.readCoils(relayBoardAddr, 0x00, 8, values);
printBool("====>Status 1", values);
delay(5000);
LOG_INFO("\n\n====>Write Status 2");
bus.writeCoils(relayBoardAddr, 0x00, {false, true, false, true, false, true, false, true});
bus.readCoils(relayBoardAddr, 0x00, 8, values);
printBool("====>Status 2", values);
delay(5000);
delay(100);
LOG_INFO("\n\n====>Read Inputs");
bus.readInputs(relayBoardAddr, 0x00, 8, values);
printBool("====>Inputs", values);