Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9006eab

Browse files
authored
Merge pull request #394 from pennam/gettime-fix
WiFiS3: add WiFi.getTime()
2 parents 7ea9dfb + 0afd739 commit 9006eab

File tree

3 files changed

+127
-5
lines changed

3 files changed

+127
-5
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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 at least version 0.5.0 of USB Wifi bridge firmware 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.");
45+
// don't continue
46+
while (true);
47+
}
48+
49+
// attempt to connect to WiFi network:
50+
while (status != WL_CONNECTED) {
51+
Serial.print("Attempting to connect to SSID: ");
52+
Serial.println(ssid);
53+
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
54+
status = WiFi.begin(ssid, pass);
55+
56+
// wait 10 seconds for connection:
57+
delay(10000);
58+
}
59+
60+
printWifiStatus();
61+
}
62+
63+
/* -------------------------------------------------------------------------- */
64+
void loop() {
65+
/* -------------------------------------------------------------------------- */
66+
67+
unsigned long EpochTime;
68+
69+
EpochTime = WiFi.getTime();
70+
71+
if (EpochTime > 0) {
72+
UpdateRTC(EpochTime);
73+
}
74+
else {
75+
Serial.println("Error during reading epoch time.");
76+
Serial.println("Make sure your WiFi firmware version is greater than 0.5.0");
77+
}
78+
79+
Serial.println();
80+
delay(10000);
81+
}
82+
83+
/* -------------------------------------------------------------------------- */
84+
void UpdateRTC(time_t EpochTime) {
85+
/* -------------------------------------------------------------------------- */
86+
87+
auto timeZoneOffsetHours = GMTOffset_hour + DayLightSaving;
88+
auto unixTime = EpochTime + (timeZoneOffsetHours * 3600);
89+
Serial.print("Unix time = ");
90+
Serial.println(unixTime);
91+
RTCTime timeToSet = RTCTime(unixTime);
92+
RTC.setTime(timeToSet);
93+
94+
// Retrieve the date and time from the RTC and print them
95+
RTCTime currentTime;
96+
RTC.getTime(currentTime);
97+
Serial.println("The RTC was just set to: " + String(currentTime));
98+
}
99+
100+
/* -------------------------------------------------------------------------- */
101+
void printWifiStatus() {
102+
/* -------------------------------------------------------------------------- */
103+
// print the SSID of the network you're attached to:
104+
Serial.print("SSID: ");
105+
Serial.println(WiFi.SSID());
106+
107+
// print your board's IP address:
108+
IPAddress ip = WiFi.localIP();
109+
Serial.print("IP Address: ");
110+
Serial.println(ip);
111+
112+
// print the received signal strength:
113+
long rssi = WiFi.RSSI();
114+
Serial.print("signal strength (RSSI):");
115+
Serial.print(rssi);
116+
Serial.println(" dBm");
117+
}
Lines changed: 2 additions & 0 deletions
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‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -542,18 +542,21 @@ 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+
if(modem.write(string(PROMPT(_GETTIME)),res,"%s\r\n", CMD_WRITE(_GETTIME))) {
555+
return strtol(res.c_str(), NULL, 10);
556+
}
557+
return 0;
553558
}
554559

555-
556-
557560
void CWifi::setTimeout(unsigned long timeout) {
558561
_timeout = timeout;
559562
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /