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

Commit 7af991d

Browse files
authored
v1.7.0 to add support to RTL8720DN
### Releases v1.7.0 1. Add support to RTL8720DN, RTL8722DM, RTM8722CSM, etc. boards 2. Add examples with new features
1 parent 7dd9209 commit 7af991d

19 files changed

+1133
-83
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Please ensure to specify the following:
2929
Arduino IDE version: 1.8.15
3030
Arduino SAMD Core Version 1.8.11
3131
OS: Ubuntu 20.04 LTS
32-
Linux xy-Inspiron-3593 5.4.0-77-generic #86-Ubuntu SMP Thu Jun 17 02:35:03 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
32+
Linux xy-Inspiron-3593 5.4.0-80-generic #90-Ubuntu SMP Fri Jul 9 22:49:44 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
3333
3434
Context:
3535
I encountered an endless loop while trying to connect to Local WiFi.

README.md

Lines changed: 137 additions & 44 deletions
Large diffs are not rendered by default.
Lines changed: 293 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,293 @@
1+
/****************************************************************************************************************************
2+
TZ_NTP_Clock_RTL8720DN.ino
3+
4+
For AVR, ESP8266/ESP32, SAMD21/SAMD51, nRF52, STM32, WT32_ETH01 boards
5+
6+
Based on and modified from Arduino Timezone Library (https://github.com/JChristensen/Timezone)
7+
to support other boards such as ESP8266/ESP32, SAMD21, SAMD51, Adafruit's nRF52 boards, etc.
8+
9+
Copyright (C) 2018 by Jack Christensen and licensed under GNU GPL v3.0, https://www.gnu.org/licenses/gpl.html
10+
11+
Built by Khoi Hoang https://github.com/khoih-prog/Timezone_Generic
12+
Licensed under MIT license
13+
Version: 1.6.0
14+
15+
Version Modified By Date Comments
16+
------- ----------- ---------- -----------
17+
1.2.4 K Hoang 17/10/2020 Initial porting to support SAM DUE, SAMD21, SAMD51, nRF52, ESP32/ESP8266, STM32, etc. boards
18+
using SPIFFS, LittleFS, EEPROM, FlashStorage, DueFlashStorage.
19+
1.2.5 K Hoang 28/10/2020 Add examples to use STM32 Built-In RTC.
20+
1.2.6 K Hoang 01/11/2020 Allow un-initialized TZ then use begin() method to set the actual TZ (Credit of 6v6gt)
21+
1.3.0 K Hoang 09/01/2021 Add support to ESP32/ESP8266 using LittleFS/SPIFFS, and to AVR, UNO WiFi Rev2, etc.
22+
Fix compiler warnings.
23+
1.4.0 K Hoang 04/06/2021 Add support to RP2040-based boards using RP2040 Arduino-mbed or arduino-pico core
24+
1.5.0 K Hoang 13/06/2021 Add support to ESP32-S2 and ESP32-C3. Fix bug
25+
1.6.0 K Hoang 16/07/2021 Add support to WT32_ETH01
26+
*****************************************************************************************************************************/
27+
28+
#include "defines.h"
29+
30+
//////////////////////////////////////////
31+
32+
#include <Timezone_Generic.h> // https://github.com/khoih-prog/Timezone_Generic
33+
34+
#define USING_INITIALIZED_TZ false //true
35+
36+
#if USING_INITIALIZED_TZ
37+
// US Eastern Time Zone (New York, Detroit,Toronto)
38+
TimeChangeRule myDST = {"EDT", Second, Sun, Mar, 2, -240}; // Daylight time = UTC - 4 hours
39+
TimeChangeRule mySTD = {"EST", First, Sun, Nov, 2, -300}; // Standard time = UTC - 5 hours
40+
Timezone *myTZ;
41+
#else
42+
// Allow a "blank" TZ object then use begin() method to set the actual TZ.
43+
// Feature added by 6v6gt (https://forum.arduino.cc/index.php?topic=711259)
44+
Timezone *myTZ;
45+
TimeChangeRule myDST;
46+
TimeChangeRule mySTD;
47+
#endif
48+
49+
// If TimeChangeRules are already stored in EEPROM, comment out the three
50+
// lines above and uncomment the line below.
51+
//Timezone myTZ(100); // assumes rules stored at EEPROM address 100
52+
53+
TimeChangeRule *tcr; // pointer to the time change rule, use to get TZ abbrev
54+
55+
//////////////////////////////////////////
56+
57+
int status = WL_IDLE_STATUS; // the Wifi radio's status
58+
59+
char timeServer[] = "time.nist.gov"; // NTP server
60+
unsigned int localPort = 2390; // local port to listen for UDP packets
61+
62+
const int NTP_PACKET_SIZE = 48; // NTP timestamp is in the first 48 bytes of the message
63+
const int UDP_TIMEOUT = 2000; // timeout in miliseconds to wait for an UDP packet to arrive
64+
65+
byte packetBuffer[NTP_PACKET_SIZE]; // buffer to hold incoming and outgoing packets
66+
67+
// A UDP instance to let us send and receive packets over UDP
68+
WiFiUDP Udp;
69+
70+
// send an NTP request to the time server at the given address
71+
void sendNTPpacket(char *ntpSrv)
72+
{
73+
// set all bytes in the buffer to 0
74+
memset(packetBuffer, 0, NTP_PACKET_SIZE);
75+
// Initialize values needed to form NTP request
76+
// (see URL above for details on the packets)
77+
78+
packetBuffer[0] = 0b11100011; // LI, Version, Mode
79+
packetBuffer[1] = 0; // Stratum, or type of clock
80+
packetBuffer[2] = 6; // Polling Interval
81+
packetBuffer[3] = 0xEC; // Peer Clock Precision
82+
// 8 bytes of zero for Root Delay & Root Dispersion
83+
packetBuffer[12] = 49;
84+
packetBuffer[13] = 0x4E;
85+
packetBuffer[14] = 49;
86+
packetBuffer[15] = 52;
87+
88+
// all NTP fields have been given values, now
89+
// you can send a packet requesting a timestamp:
90+
Udp.beginPacket(ntpSrv, 123); //NTP requests are to port 123
91+
92+
Udp.write(packetBuffer, NTP_PACKET_SIZE);
93+
94+
Udp.endPacket();
95+
}
96+
97+
//////////////////////////////////////////
98+
99+
// format and print a time_t value, with a time zone appended.
100+
void printDateTime(time_t t, const char *tz)
101+
{
102+
char buf[48];
103+
char m[4]; // temporary storage for month string (DateStrings.cpp uses shared buffer)
104+
105+
memset(buf, 0, sizeof(buf));
106+
memset(m, 0, sizeof(m));
107+
108+
strcpy(m, monthShortStr(month(t)));
109+
sprintf(buf, "%2d:%2d:%2d %s %2d %s %d %s",
110+
hour(t), minute(t), second(t), dayShortStr(weekday(t)), day(t), m, year(t), tz);
111+
Serial.println(buf);
112+
}
113+
114+
void displayClock(void)
115+
{
116+
time_t utc = now();
117+
118+
time_t local = myTZ->toLocal(utc, &tcr);
119+
120+
Serial.println();
121+
printDateTime(utc, "UTC");
122+
printDateTime(local, tcr -> abbrev);
123+
delay(10000);
124+
}
125+
126+
void getNTPTime(void)
127+
{
128+
static bool gotCurrentTime = false;
129+
130+
// Just get the correct ime once
131+
if (!gotCurrentTime)
132+
{
133+
sendNTPpacket(timeServer); // send an NTP packet to a time server
134+
// wait to see if a reply is available
135+
delay(1000);
136+
137+
if (Udp.parsePacket())
138+
{
139+
Serial.println(F("Packet received"));
140+
// We've received a packet, read the data from it
141+
Udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer
142+
143+
//the timestamp starts at byte 40 of the received packet and is four bytes,
144+
// or two words, long. First, esxtract the two words:
145+
146+
unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
147+
unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
148+
// combine the four bytes (two words) into a long integer
149+
// this is NTP time (seconds since Jan 1 1900):
150+
unsigned long secsSince1900 = highWord << 16 | lowWord;
151+
Serial.print(F("Seconds since Jan 1 1900 = "));
152+
Serial.println(secsSince1900);
153+
154+
// now convert NTP time into everyday time:
155+
Serial.print(F("Unix time = "));
156+
// Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
157+
const unsigned long seventyYears = 2208988800UL;
158+
// subtract seventy years:
159+
unsigned long epoch = secsSince1900 - seventyYears;
160+
161+
// Get the time_t from epoch
162+
time_t epoch_t = epoch;
163+
164+
// set the system time to UTC
165+
// warning: assumes that compileTime() returns US EDT
166+
// adjust the following line accordingly if you're in another time zone
167+
setTime(epoch_t);
168+
169+
// print Unix time:
170+
Serial.println(epoch);
171+
172+
// print the hour, minute and second:
173+
Serial.print(F("The UTC time is ")); // UTC is the time at Greenwich Meridian (GMT)
174+
Serial.print((epoch % 86400L) / 3600); // print the hour (86400 equals secs per day)
175+
Serial.print(':');
176+
177+
if (((epoch % 3600) / 60) < 10)
178+
{
179+
// In the first 10 minutes of each hour, we'll want a leading '0'
180+
Serial.print('0');
181+
}
182+
Serial.print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute)
183+
Serial.print(':');
184+
185+
if ((epoch % 60) < 10)
186+
{
187+
// In the first 10 seconds of each minute, we'll want a leading '0'
188+
Serial.print('0');
189+
}
190+
Serial.println(epoch % 60); // print the second
191+
192+
gotCurrentTime = true;
193+
}
194+
else
195+
{
196+
// wait ten seconds before asking for the time again
197+
delay(10000);
198+
}
199+
}
200+
}
201+
202+
//////////////////////////////////////////
203+
204+
void setup()
205+
{
206+
Serial.begin(115200);
207+
while (!Serial);
208+
209+
delay(200);
210+
211+
Serial.print(F("\nStart TZ_NTP_Clock_RTL8720DN on ")); Serial.println(BOARD_NAME);
212+
Serial.println(WIFI_WEBSERVER_RTL8720_VERSION);
213+
Serial.println(TIMEZONE_GENERIC_VERSION);
214+
215+
if (WiFi.status() == WL_NO_SHIELD)
216+
{
217+
Serial.println(F("WiFi shield not present"));
218+
// don't continue
219+
while (true);
220+
}
221+
222+
String fv = WiFi.firmwareVersion();
223+
224+
Serial.print("Current Firmware Version = "); Serial.println(fv);
225+
226+
if (fv != LATEST_RTL8720_FIRMWARE)
227+
{
228+
Serial.println("Please upgrade the firmware");
229+
}
230+
231+
// attempt to connect to Wifi network:
232+
while (status != WL_CONNECTED)
233+
{
234+
Serial.print("Attempting to connect to SSID: "); Serial.println(ssid);
235+
236+
// Connect to WPA/WPA2 network. 2.4G and 5G are all OK
237+
status = WiFi.begin(ssid, pass);
238+
239+
// wait 10 seconds for connection:
240+
delay(10000);
241+
}
242+
243+
Serial.print(F("TZ_NTP_Clock_RTL8720DN started @ IP address: "));
244+
Serial.println(WiFi.localIP());
245+
246+
#if (USING_INITIALIZED_TZ)
247+
248+
myTZ = new Timezone(myDST, mySTD);
249+
250+
#else
251+
252+
// Can read this info from EEPROM, storage, etc
253+
String tzName = "EDT/EST" ;
254+
255+
// Time zone rules can be set as below or dynamically built, say through a configuration
256+
// interface, or fetched from eeprom, flash etc.
257+
258+
if ( tzName == "EDT/EST" )
259+
{
260+
// America Eastern Time
261+
myDST = (TimeChangeRule) {"EDT", Second, Sun, Mar, 2, -240}; // Daylight time = UTC - 4 hours
262+
mySTD = (TimeChangeRule) {"EST", First, Sun, Nov, 2, -300}; // Standard time = UTC - 5 hours
263+
}
264+
else if ( tzName == "CET/CEST" )
265+
{
266+
// central Europe
267+
myDST = (TimeChangeRule) {"CEST", Last, Sun, Mar, 2, 120};
268+
mySTD = (TimeChangeRule) {"CET", Last, Sun, Oct, 3, 60};
269+
}
270+
271+
else if ( tzName == "GMT/BST" )
272+
{
273+
// UK
274+
myDST = (TimeChangeRule) {"BST", Last, Sun, Mar, 1, 60};
275+
mySTD = (TimeChangeRule) {"GMT", Last, Sun, Oct, 2, 0};
276+
}
277+
278+
myTZ = new Timezone();
279+
myTZ->init( myDST, mySTD );
280+
281+
#endif
282+
283+
Udp.begin(localPort);
284+
285+
Serial.print(F("Listening on port "));
286+
Serial.println(localPort);
287+
}
288+
289+
void loop()
290+
{
291+
getNTPTime();
292+
displayClock();
293+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/****************************************************************************************************************************
2+
defines.h
3+
4+
For AVR, ESP8266/ESP32, SAMD21/SAMD51, nRF52, STM32, WT32_ETH01 boards
5+
6+
Based on and modified from Arduino Timezone Library (https://github.com/JChristensen/Timezone)
7+
to support other boards such as ESP8266/ESP32, SAMD21, SAMD51, Adafruit's nRF52 boards, etc.
8+
9+
Copyright (C) 2018 by Jack Christensen and licensed under GNU GPL v3.0, https://www.gnu.org/licenses/gpl.html
10+
11+
Built by Khoi Hoang https://github.com/khoih-prog/Timezone_Generic
12+
Licensed under MIT license
13+
*****************************************************************************************************************************/
14+
15+
#ifndef defines_h
16+
#define defines_h
17+
18+
#define DEBUG_WIFI_WEBSERVER_PORT Serial
19+
#define NTP_DBG_PORT Serial
20+
21+
// Debug Level from 0 to 4
22+
#define _WIFI_LOGLEVEL_ 3
23+
#define _NTP_LOGLEVEL_ 0
24+
25+
#include <WiFiWebServer_RTL8720.h>
26+
27+
char ssid[] = "SSID_5GHz"; // your network SSID (name)
28+
char pass[] = "12345678"; // your network password
29+
30+
#endif //defines_h

0 commit comments

Comments
 (0)