Skip to content
This repository has been archived by the owner on Jan 29, 2023. It is now read-only.

Commit

Permalink
v1.1.0 to use Ethernet_Generic library
Browse files Browse the repository at this point in the history
### Releases v1.1.0

1. Use new [Ethernet_Generic library](https://github.com/khoih-prog/Ethernet_Generic) as default for W5x00.
2. Add support to `SPI1` for `RP2040` using [`Earle Philhower's arduino-pico` core](https://github.com/earlephilhower/arduino-pico)
3. Add support to WIZNet W5100S, such as  [**WIZnet Ethernet HAT**](https://docs.wiznet.io/Product/Open-Source-Hardware/wiznet_ethernet_hat) and [**W5100S-EVB-Pico**](https://docs.wiznet.io/Product/iEthernet/W5100S/w5100s-evb-pico)
  • Loading branch information
khoih-prog authored Apr 25, 2022
1 parent e8a9591 commit 7008267
Showing 1 changed file with 5 additions and 338 deletions.
343 changes: 5 additions & 338 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
[![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](#Contributing)
[![GitHub issues](https://img.shields.io/github/issues/khoih-prog/RP2040_RTC.svg)](http://github.com/khoih-prog/RP2040_RTC/issues)

<a href="https://www.buymeacoffee.com/khoihprog6" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Buy Me A Coffee" style="height: 50px !important;width: 181px !important;" ></a>
<a href="https://www.buymeacoffee.com/khoihprog6" title="Donate to my libraries using BuyMeACoffee"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Donate to my libraries using BuyMeACoffee" style="height: 50px !important;width: 181px !important;" ></a>
<a href="https://www.buymeacoffee.com/khoihprog6" title="Donate to my libraries using BuyMeACoffee"><img src="https://img.shields.io/badge/buy%20me%20a%20coffee-donate-orange.svg?logo=buy-me-a-coffee&logoColor=FFDD00" style="height: 20px !important;width: 200px !important;" ></a>


---
Expand Down Expand Up @@ -161,7 +162,7 @@ Another way to install is to:

1. Install [VS Code](https://code.visualstudio.com/)
2. Install [PlatformIO](https://platformio.org/platformio-ide)
3. Install [**RP2040_RTC** library](https://registry.platformio.org/libraries/khoih-prog/RP2040_RTC) or [**RP2040_RTC** library](https://registry.platformio.org/libraries/khoih-prog/RP2040_RTC) by using [Library Manager](https://platformio.org/lib/show/12433/RP2040_RTC/installation). Search for **RP2040_RTC** in [Platform.io Author's Libraries](https://platformio.org/lib/search?query=author:%22Khoi%20Hoang%22)
3. Install [**RP2040_RTC** library](https://registry.platformio.org/libraries/khoih-prog/RP2040_RTC) by using [Library Manager](https://registry.platformio.org/libraries/khoih-prog/RP2040_RTC/installation). Search for **RP2040_RTC** in [Platform.io Author's Libraries](https://platformio.org/lib/search?query=author:%22Khoi%20Hoang%22)
4. Please visit documentation for the other options and examples at [Project Configuration File](https://docs.platformio.org/page/projectconf.html)


Expand Down Expand Up @@ -399,7 +400,6 @@ class DateTime
/** ISO 8601 Timestamp function */
String timestamp(timestampOpt opt = TIMESTAMP_FULL)
}
```


Expand All @@ -422,344 +422,11 @@ class DateTime

#### 1. File [RP2040_RTC_Time_WiFiNINA.ino](examples/Time/RP2040_RTC_Time_WiFiNINA/RP2040_RTC_Time_WiFiNINA.ino)

```
// Important notes: Currently, RP2040-based boards RTC has no battery backup. So the time will be lost when power down
// Therefore, NTP client is necessary to get NTP time and update RTC.
#include "defines.h"
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
#include <Timezone_Generic.h> // https://github.com/khoih-prog/Timezone_Generic
//////////////////////////////////////////
// US Eastern Time Zone (New York, Detroit)
TimeChangeRule myDST = {"EDT", Second, Sun, Mar, 2, -240}; //Daylight time = UTC - 4 hours
TimeChangeRule mySTD = {"EST", First, Sun, Nov, 2, -300}; //Standard time = UTC - 5 hours
Timezone *myTZ;
TimeChangeRule *tcr; //pointer to the time change rule, use to get TZ abbrev
//////////////////////////////////////////
int status = WL_IDLE_STATUS; // the Wifi radio's status
char timeServer[] = "time.nist.gov"; // NTP server
unsigned int localPort = 2390; // local port to listen for UDP packets
const int NTP_PACKET_SIZE = 48; // NTP timestamp is in the first 48 bytes of the message
const int UDP_TIMEOUT = 2000; // timeout in miliseconds to wait for an UDP packet to arrive
byte packetBuffer[NTP_PACKET_SIZE]; // buffer to hold incoming and outgoing packets
// A UDP instance to let us send and receive packets over UDP
WiFiUDP Udp;
datetime_t currTime;
char datetime_buf[256];
// send an NTP request to the time server at the given address
void sendNTPpacket(char *ntpSrv)
{
// set all bytes in the buffer to 0
memset(packetBuffer, 0, NTP_PACKET_SIZE);
// Initialize values needed to form NTP request
// (see URL above for details on the packets)
packetBuffer[0] = 0b11100011; // LI, Version, Mode
packetBuffer[1] = 0; // Stratum, or type of clock
packetBuffer[2] = 6; // Polling Interval
packetBuffer[3] = 0xEC; // Peer Clock Precision
// 8 bytes of zero for Root Delay & Root Dispersion
packetBuffer[12] = 49;
packetBuffer[13] = 0x4E;
packetBuffer[14] = 49;
packetBuffer[15] = 52;
// all NTP fields have been given values, now
// you can send a packet requesting a timestamp:
Udp.beginPacket(ntpSrv, 123); //NTP requests are to port 123
Udp.write(packetBuffer, NTP_PACKET_SIZE);
Udp.endPacket();
}
void getNTPTime()
{
static bool gotCurrentTime = false;
// Just get the correct ime once
if (!gotCurrentTime)
{
sendNTPpacket(timeServer); // send an NTP packet to a time server
// wait to see if a reply is available
delay(1000);
if (Udp.parsePacket())
{
Serial.println(F("Packet received"));
// We've received a packet, read the data from it
Udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer
//the timestamp starts at byte 40 of the received packet and is four bytes,
// or two words, long. First, esxtract the two words:
unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
// combine the four bytes (two words) into a long integer
// this is NTP time (seconds since Jan 1 1900):
unsigned long secsSince1900 = highWord << 16 | lowWord;
Serial.print(F("Seconds since Jan 1 1900 = "));
Serial.println(secsSince1900);
// now convert NTP time into everyday time:
Serial.print(F("Unix time = "));
// Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
const unsigned long seventyYears = 2208988800UL;
// subtract seventy years:
unsigned long epoch = secsSince1900 - seventyYears;
// print Unix time:
Serial.println(epoch);
// Get the time_t from epoch
time_t epoch_t = epoch;
// set the system time to UTC
// warning: assumes that compileTime() returns US EDT
// adjust the following line accordingly if you're in another time zone
setTime(epoch_t);
// Update RTC
// Can use either one of these functions
// 1) DateTime(tmElements_t). Must create tmElements_t if not present
//tmElements_t tm;
//breakTime(epoch_t, tm);
//rtc_set_datetime( DateTime(tm) );
// 2) DateTime(year, month, day, hour, min, sec)
//rtc_set_datetime( DateTime(year(epoch_t), month(epoch_t), day(epoch_t), hour(epoch_t), minute(epoch_t), second(epoch_t) ) );
// 3) DateTime (time_t)
//rtc_set_datetime( DateTime(epoch_t) );
// 4) DateTime(unsigned long epoch). The best and easiest way
//rtc_set_datetime( DateTime((uint32_t) epoch) );
// New function in DateTime_Generic.h
// To be called before while loop to work. Why ???
rtc_set_datetime(DateTime((uint32_t) epoch));
uint8_t loopCount = 0;
while( (loopCount++ < 10 ) && ( ! rtc_set_datetime(DateTime((uint32_t) epoch)) ) )
{
Serial.println(F("rtc_set_datetime failed"));
sleep_ms(500);
}
// print the hour, minute and second:
Serial.print(F("The UTC time is ")); // UTC is the time at Greenwich Meridian (GMT)
Serial.print((epoch % 86400L) / 3600); // print the hour (86400 equals secs per day)
Serial.print(':');
if (((epoch % 3600) / 60) < 10)
{
// In the first 10 minutes of each hour, we'll want a leading '0'
Serial.print('0');
}
Serial.print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute)
Serial.print(':');
if ((epoch % 60) < 10)
{
// In the first 10 seconds of each minute, we'll want a leading '0'
Serial.print('0');
}
Serial.println(epoch % 60); // print the second
gotCurrentTime = true;
}
else
{
// wait ten seconds before asking for the time again
delay(10000);
}
}
}
//////////////////////////////////////////
// format and print a time_t value, with a time zone appended.
void printDateTime(time_t t, const char *tz)
{
char buf[32];
char m[4]; // temporary storage for month string (DateStrings.cpp uses shared buffer)
strcpy(m, monthShortStr(month(t)));
sprintf(buf, "%.2d:%.2d:%.2d %s %.2d %s %d %s",
hour(t), minute(t), second(t), dayShortStr(weekday(t)), day(t), m, year(t), tz);
Serial.println(buf);
}
void setup()
{
Serial.begin(115200);
while (!Serial);
delay(200);
Serial.print(F("\nStart RP2040_RTC_Time_WiFiNINA on ")); Serial.print(BOARD_NAME);
Serial.print(F(" with ")); Serial.println(SHIELD_TYPE);
Serial.println(RP2040_RTC_VERSION);
Serial.println(TIMEZONE_GENERIC_VERSION);
// check for the presence of the shield
if (WiFi.status() == WL_NO_MODULE)
{
Serial.println(F("WiFi shield not present"));
// don't continue
while (true);
}
String fv = WiFi.firmwareVersion();
if (fv < WIFI_FIRMWARE_LATEST_VERSION)
{
Serial.println(F("Please upgrade the firmware"));
}
// attempt to connect to WiFi network
while ( status != WL_CONNECTED)
{
Serial.print(F("Connecting to WPA SSID: "));
Serial.println(ssid);
// Connect to WPA/WPA2 network
status = WiFi.begin(ssid, pass);
}
// you're connected now, so print out the data
Serial.print(F("You're connected to the network, IP = "));
Serial.println(WiFi.localIP());
myTZ = new Timezone(myDST, mySTD);
// Start the RTC
rtc_init();
Udp.begin(localPort);
}
void displayTime()
{
rtc_get_datetime(&currTime);
// Display time from RTC
DateTime now = DateTime(currTime);
time_t utc = now.get_time_t();
time_t local = myTZ->toLocal(utc, &tcr);
printDateTime(utc, "UTC");
printDateTime(local, tcr -> abbrev);
}
void displayRTCTime()
{
static unsigned long displayRTCTime_timeout = 0;
#define DISPLAY_RTC_INTERVAL 60000L
// Send status report every STATUS_REPORT_INTERVAL (60) seconds: we don't need to display frequently.
if ((millis() > displayRTCTime_timeout) || (displayRTCTime_timeout == 0))
{
Serial.println("============================");
displayTime();
displayRTCTime_timeout = millis() + DISPLAY_RTC_INTERVAL;
}
}
void loop()
{
// Get time from NTP once, then update RTC
// You certainly can make NTP check every hour/day to update RTC to have better accuracy
getNTPTime();
displayRTCTime();
}
```
https://github.com/khoih-prog/RP2040_RTC/blob/e8a9591a9460cb478d88de0d130f8aeaf97fa8f7/examples/Time/RP2040_RTC_Time_WiFiNINA/RP2040_RTC_Time_WiFiNINA.ino#L11-L276

#### 2. File [defines.h](examples/Time/RP2040_RTC_Time_WiFiNINA/defines.h)

```
#ifndef defines_h
#define defines_h
#define DEBUG_WIFI_WEBSERVER_PORT Serial
// Debug Level from 0 to 4
#define _WIFI_LOGLEVEL_ 1
#define _WIFININA_LOGLEVEL_ 1
#define USE_WIFI_NINA true
//#define USE_WIFI_NINA false
#define RTC_DEBUG false
#if USE_WIFI_NINA
#warning Using WiFiNINA using WiFiNINA_Generic Library
#define SHIELD_TYPE "WiFiNINA using WiFiNINA_Generic Library"
#endif
#if ( defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_RASPBERRY_PI_PICO) || defined(ARDUINO_ADAFRUIT_FEATHER_RP2040) || defined(ARDUINO_GENERIC_RP2040) )
#if defined(WIFININA_USE_RP2040)
#undef WIFININA_USE_RP2040
#endif
#define WIFININA_USE_RP2040 true
#if defined(ARDUINO_ARCH_MBED)
#if defined(BOARD_NAME)
#undef BOARD_NAME
#endif
#if defined(ARDUINO_NANO_RP2040_CONNECT)
#define BOARD_NAME "MBED NANO_RP2040_CONNECT"
#elif defined(ARDUINO_RASPBERRY_PI_PICO)
#define BOARD_NAME "MBED RASPBERRY_PI_PICO"
#elif defined(ARDUINO_ADAFRUIT_FEATHER_RP2040)
#define BOARD_NAME "MBED DAFRUIT_FEATHER_RP2040"
#elif defined(ARDUINO_GENERIC_RP2040)
#define BOARD_NAME "MBED GENERIC_RP2040"
#else
#define BOARD_NAME "MBED Unknown RP2040"
#endif
#endif
#else
#error This code is designed to run on RP2040 platform! Please check your Tools->Board setting.
#endif
#ifndef BOARD_NAME
#if defined(ARDUINO_BOARD)
#define BOARD_NAME ARDUINO_BOARD
#else
#define BOARD_NAME BOARD_TYPE
#endif
#endif
#include <RP2040_RTC.h>
#include <WiFiNINA_Generic.h>
char ssid[] = "ssid"; // your network SSID (name)
char pass[] = "12345678"; // your network password
#endif //defines_h
```
https://github.com/khoih-prog/RP2040_RTC/blob/e8a9591a9460cb478d88de0d130f8aeaf97fa8f7/examples/Time/RP2040_RTC_Time_WiFiNINA/defines.h#L11-L75

---
---
Expand Down

0 comments on commit 7008267

Please sign in to comment.