Skip to content

Commit 28bf6ff

Browse files
paulvhapennam
authored andcommitted
add getTime
1 parent 7ea9dfb commit 28bf6ff

File tree

3 files changed

+134
-5
lines changed

3 files changed

+134
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
Get the time in seconds since January 1st, 1970.
3+
4+
The time is retrieved with the WiFi module by fetching the NTP time from an NTP server.
5+
6+
It requires the latest USB Wifi bridge firmware level and WiFiS3 library.
7+
8+
This example is written for a network using WPA encryption. For
9+
WEP or WPA, change the WiFi.begin() call accordingly.
10+
11+
created 21 february 2024
12+
13+
*/
14+
15+
#include "WiFiS3.h"
16+
#include "arduino_secrets.h"
17+
#include "RTC.h"
18+
19+
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
20+
char ssid[] = SECRET_SSID; // your network SSID (name)
21+
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
22+
int keyIndex = 0; // your network key index number (needed only for WEP)
23+
24+
/// set offsets to GMT from local
25+
#define GMTOffset_hour 1 // # hours difference to GMT
26+
#define DayLightSaving 0 // 1 = daylight saving is active
27+
28+
int status = WL_IDLE_STATUS;
29+
30+
/* -------------------------------------------------------------------------- */
31+
void setup() {
32+
/* -------------------------------------------------------------------------- */
33+
34+
//Initialize serial and wait for port to open:
35+
Serial.begin(115200);
36+
while (!Serial) {
37+
; // wait for serial port to connect. Needed for native USB port only
38+
}
39+
40+
RTC.begin();
41+
42+
// check for the WiFi module:
43+
if (WiFi.status() == WL_NO_MODULE) {
44+
Serial.println("Communication with WiFi module failed. freeze !");
45+
// don't continue
46+
while (true);
47+
}
48+
49+
String fv = WiFi.firmwareVersion();
50+
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
51+
Serial.println("Please upgrade to the WiFi USB bridge firmware. freeze !");
52+
// don't continue
53+
while (true);
54+
}
55+
56+
// attempt to connect to WiFi network:
57+
while (status != WL_CONNECTED) {
58+
Serial.print("Attempting to connect to SSID: ");
59+
Serial.println(ssid);
60+
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
61+
status = WiFi.begin(ssid, pass);
62+
63+
// wait 10 seconds for connection:
64+
delay(10000);
65+
}
66+
67+
printWifiStatus();
68+
}
69+
70+
/* -------------------------------------------------------------------------- */
71+
void loop() {
72+
/* -------------------------------------------------------------------------- */
73+
74+
unsigned long EpochTime;
75+
76+
EpochTime = WiFi.getTime();
77+
78+
if (EpochTime > 0) {
79+
UpdateRTC(EpochTime);
80+
}
81+
else {
82+
Serial.println("Error during reading epoch time.");
83+
}
84+
85+
Serial.println();
86+
delay(10000);
87+
}
88+
89+
/* -------------------------------------------------------------------------- */
90+
void UpdateRTC(time_t EpochTime) {
91+
/* -------------------------------------------------------------------------- */
92+
93+
auto timeZoneOffsetHours = GMTOffset_hour + DayLightSaving;
94+
auto unixTime = EpochTime + (timeZoneOffsetHours * 3600);
95+
Serial.print("Unix time = ");
96+
Serial.println(unixTime);
97+
RTCTime timeToSet = RTCTime(unixTime);
98+
RTC.setTime(timeToSet);
99+
100+
// Retrieve the date and time from the RTC and print them
101+
RTCTime currentTime;
102+
RTC.getTime(currentTime);
103+
Serial.println("The RTC was just set to: " + String(currentTime));
104+
}
105+
106+
/* -------------------------------------------------------------------------- */
107+
void printWifiStatus() {
108+
/* -------------------------------------------------------------------------- */
109+
// print the SSID of the network you're attached to:
110+
Serial.print("SSID: ");
111+
Serial.println(WiFi.SSID());
112+
113+
// print your board's IP address:
114+
IPAddress ip = WiFi.localIP();
115+
Serial.print("IP Address: ");
116+
Serial.println(ip);
117+
118+
// print the received signal strength:
119+
long rssi = WiFi.RSSI();
120+
Serial.print("signal strength (RSSI):");
121+
Serial.print(rssi);
122+
Serial.println(" dBm");
123+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#define SECRET_SSID ""
2+
#define SECRET_PASS ""

libraries/WiFiS3/src/WiFi.cpp

+9-5
Original file line numberDiff line numberDiff line change
@@ -542,18 +542,22 @@ int CWifi::hostByName(const char* aHostname, IPAddress& aResult) {
542542
return 0;
543543
}
544544

545-
546-
547545
uint8_t CWifi::reasonCode() {
548546
return 0;
549547
}
550548

549+
/* ----------------------------------------------*/
551550
unsigned long CWifi::getTime() {
552-
return 0;
551+
/* ----------------------------------------------*/
552+
modem.begin();
553+
string res = "";
554+
unsigned long tt = 0;
555+
if(modem.write(string(PROMPT(_GETTIME)),res,"%s\r\n", CMD_WRITE(_GETTIME))) {
556+
tt = strtol(res.c_str(), NULL, 10);
557+
}
558+
return tt;
553559
}
554560

555-
556-
557561
void CWifi::setTimeout(unsigned long timeout) {
558562
_timeout = timeout;
559563
}

0 commit comments

Comments
 (0)