Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //For Uno R4 WiFi
- //Compiles with a few lib conversion warnings
- //In "WiFiUDP.h" change "#define RX_BUFFER_DIM 1461" to "#define RX_BUFFER_DIM 1024" fixes one warning
- //Untested
- #include "RTC.h"
- #include <NTPClient.h>
- #include <WiFiS3.h>
- #include <WiFiUdp.h>
- //Enter Your WiFi details
- char ssid[] = "YOUR_SSID";
- char password[] = "YOUR_PASSWORD";
- uint8_t lastSecond;
- int wifiStatus = WL_IDLE_STATUS;
- WiFiUDP ntpUDP;
- NTPClient timeClient(ntpUDP, "oceania.pool.ntp.org", 0, 66666); //update every hour and a bit
- void printWifiStatus() {
- // print the SSID of the network you're attached to:
- Serial.print("SSID: ");
- Serial.println(WiFi.SSID());
- // print your board's IP address:
- IPAddress ip = WiFi.localIP();
- Serial.print("IP Address: ");
- Serial.println(ip);
- // print the received signal strength:
- long rssi = WiFi.RSSI();
- Serial.print("signal strength (RSSI):");
- Serial.print(rssi);
- Serial.println(" dBm");
- return;
- }
- void connectToWiFi(){
- // check for the WiFi module:
- if (WiFi.status() == WL_NO_MODULE) {
- Serial.println("Communication with WiFi module failed!");
- // don't continue
- while (true);
- }
- String fv = WiFi.firmwareVersion();
- if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
- Serial.println("Please upgrade the firmware");
- }
- // attempt to connect to WiFi network:
- while (wifiStatus != WL_CONNECTED) {
- Serial.print("Attempting to connect to SSID: ");
- Serial.println(ssid);
- // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
- wifiStatus = WiFi.begin(ssid, password);
- // wait 10 seconds for connection:
- delay(10000);
- }
- Serial.println("Connected to WiFi");
- printWifiStatus();
- return;
- }
- void setup() {
- Serial.begin(115200);
- connectToWiFi();
- RTC.begin();
- Serial.println("\nStarting connection to server...");
- timeClient.begin();
- timeClient.update();
- // Get the current date and time from an NTP server and convert
- // it to UTC +2 by passing the time zone offset in hours.
- // You may change the time zone offset to your local one.
- auto timeZoneOffsetHours = 10;
- auto unixTime = timeClient.getEpochTime() + (timeZoneOffsetHours * 3600);
- Serial.print("Unix time = ");
- Serial.println(unixTime);
- RTCTime timeToSet = RTCTime(unixTime);
- RTC.setTime(timeToSet);
- // Retrieve the date and time from the RTC and print them
- RTCTime currentTime;
- RTC.getTime(currentTime);
- Serial.println("The RTC was just set to: " + String(currentTime));
- lastSecond = currentTime.getSeconds();
- }
- void myClock(void){
- RTCTime currentTime;
- RTC.getTime(currentTime);
- if(currentTime.getSeconds() != lastSecond){
- // Print out date (DD/MM//YYYY)
- Serial.print(currentTime.getDayOfMonth());
- Serial.print("/");
- Serial.print(Month2int(currentTime.getMonth()));
- Serial.print("/");
- Serial.print(currentTime.getYear());
- Serial.print(" - ");
- // Print time (HH/MM/SS)
- Serial.print(currentTime.getHour());
- Serial.print(":");
- Serial.print(currentTime.getMinutes());
- Serial.print(":");
- Serial.println(currentTime.getSeconds());
- //Update one second delay
- lastSecond = currentTime.getSeconds();
- }
- }
- void loop() {
- myClock();
- }
Advertisement
Add Comment
Please, Sign In to add comment