|
| 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 | +} |
0 commit comments