Fixed RTC and Ethernet drivers, with NTP
This commit is contained in:
72
lib/ETH/ETH_Driver.cpp
Normal file
72
lib/ETH/ETH_Driver.cpp
Normal file
@@ -0,0 +1,72 @@
|
||||
#include "ETH_Driver.h"
|
||||
|
||||
namespace drivers
|
||||
{
|
||||
|
||||
Ethernet::Ethernet(const std::string hostname) : m_hostname(hostname), m_connected(false), m_localIP(IPAddress()), m_udp(NetworkUDP()), m_timeClient(m_udp)
|
||||
{
|
||||
SPI.begin(ETH_SPI_SCK, ETH_SPI_MISO, ETH_SPI_MOSI);
|
||||
ETH.begin(ETH_PHY_TYPE, ETH_PHY_ADDR, ETH_PHY_CS, ETH_PHY_IRQ, ETH_PHY_RST, SPI);
|
||||
|
||||
m_timeClient = std::move(NTPClient(m_udp, "pool.ntp.org", 0, 3600)); // NTP server, time offset in seconds, update interval
|
||||
m_timeClient.begin();
|
||||
}
|
||||
|
||||
Ethernet::~Ethernet()
|
||||
{
|
||||
m_timeClient.end();
|
||||
ETH.end();
|
||||
SPI.end();
|
||||
}
|
||||
|
||||
const bool Ethernet::getNtpTime(time_t &time)
|
||||
{
|
||||
if (m_connected && m_timeClient.update())
|
||||
{
|
||||
time = m_timeClient.getEpochTime();
|
||||
LOG_DEBUG("Epoch Time:", (long)time);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
const bool Ethernet::isConnected()
|
||||
{
|
||||
return m_connected;
|
||||
}
|
||||
|
||||
void Ethernet::onEvent(arduino_event_id_t event, arduino_event_info_t info)
|
||||
{
|
||||
switch (event)
|
||||
{
|
||||
case ARDUINO_EVENT_ETH_START:
|
||||
ETH.setHostname("waveshare-esp32s3");
|
||||
break;
|
||||
case ARDUINO_EVENT_ETH_CONNECTED:
|
||||
LOG_INFO("ETH Connected");
|
||||
break;
|
||||
case ARDUINO_EVENT_ETH_GOT_IP:
|
||||
m_localIP = ETH.localIP();
|
||||
LOG_INFO("ETH ", esp_netif_get_desc(info.got_ip.esp_netif), " Got IP:", m_localIP.toString().c_str());
|
||||
LOG_INFO("ETH ", esp_netif_get_desc(info.got_ip.esp_netif), " Gateway:", ETH.gatewayIP().toString().c_str());
|
||||
LOG_INFO("ETH ", esp_netif_get_desc(info.got_ip.esp_netif), " Netmask:", ETH.subnetMask().toString().c_str());
|
||||
m_connected = true;
|
||||
break;
|
||||
case ARDUINO_EVENT_ETH_LOST_IP:
|
||||
LOG_INFO("ETH Lost IP");
|
||||
m_connected = false;
|
||||
break;
|
||||
case ARDUINO_EVENT_ETH_DISCONNECTED:
|
||||
LOG_INFO("ETH Disconnected");
|
||||
m_connected = false;
|
||||
break;
|
||||
case ARDUINO_EVENT_ETH_STOP:
|
||||
LOG_INFO("ETH Stopped");
|
||||
m_connected = false;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
46
lib/ETH/ETH_Driver.h
Normal file
46
lib/ETH/ETH_Driver.h
Normal file
@@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
|
||||
#include <DebugLog.h>
|
||||
#include <Arduino.h>
|
||||
#include <Network.h>
|
||||
#include <NTPClient.h>
|
||||
#include <ETH.h>
|
||||
#include <SPI.h>
|
||||
|
||||
// PHY defines hardware related
|
||||
#ifndef ETH_PHY_TYPE
|
||||
#define ETH_PHY_TYPE ETH_PHY_W5500
|
||||
#define ETH_PHY_ADDR 1
|
||||
#define ETH_PHY_CS 16
|
||||
#define ETH_PHY_IRQ 12
|
||||
#define ETH_PHY_RST 39
|
||||
#endif
|
||||
|
||||
// SPI pins
|
||||
#define ETH_SPI_SCK 15
|
||||
#define ETH_SPI_MISO 14
|
||||
#define ETH_SPI_MOSI 13
|
||||
|
||||
namespace drivers
|
||||
{
|
||||
|
||||
class Ethernet
|
||||
{
|
||||
|
||||
public:
|
||||
Ethernet(const std::string hostname);
|
||||
~Ethernet();
|
||||
|
||||
void onEvent(arduino_event_id_t event, arduino_event_info_t info);
|
||||
const bool isConnected();
|
||||
const bool getNtpTime(time_t &time);
|
||||
|
||||
private:
|
||||
const std::string m_hostname;
|
||||
bool m_connected;
|
||||
NetworkUDP m_udp;
|
||||
IPAddress m_localIP;
|
||||
NTPClient m_timeClient;
|
||||
};
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user