macca-nz

sync_Uno_R4_RTC_to_NTP

Mar 29th, 2024
1,010
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 3.26 KB | Source Code | 0 0
  1. //For Uno R4 WiFi
  2. //Compiles with a few lib conversion warnings
  3. //In "WiFiUDP.h" change "#define RX_BUFFER_DIM 1461" to "#define RX_BUFFER_DIM 1024" fixes one warning
  4. //Untested
  5.  
  6.  
  7. #include "RTC.h"
  8. #include <NTPClient.h>
  9. #include <WiFiS3.h>
  10. #include <WiFiUdp.h>
  11.  
  12. //Enter Your WiFi details
  13. char ssid[] = "YOUR_SSID";
  14. char password[] = "YOUR_PASSWORD";
  15.  
  16. uint8_t lastSecond;
  17.  
  18. int wifiStatus = WL_IDLE_STATUS;
  19. WiFiUDP ntpUDP;
  20. NTPClient timeClient(ntpUDP, "oceania.pool.ntp.org", 0, 66666); //update every hour and a bit
  21.  
  22. void printWifiStatus() {
  23.   // print the SSID of the network you're attached to:
  24.   Serial.print("SSID: ");
  25.   Serial.println(WiFi.SSID());
  26.  
  27.   // print your board's IP address:
  28.   IPAddress ip = WiFi.localIP();
  29.   Serial.print("IP Address: ");
  30.   Serial.println(ip);
  31.  
  32.   // print the received signal strength:
  33.   long rssi = WiFi.RSSI();
  34.   Serial.print("signal strength (RSSI):");
  35.   Serial.print(rssi);
  36.   Serial.println(" dBm");
  37.   return;
  38. }
  39.  
  40. void connectToWiFi(){
  41.   // check for the WiFi module:
  42.   if (WiFi.status() == WL_NO_MODULE) {
  43.     Serial.println("Communication with WiFi module failed!");
  44.     // don't continue
  45.     while (true);
  46.   }
  47.  
  48.   String fv = WiFi.firmwareVersion();
  49.   if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
  50.     Serial.println("Please upgrade the firmware");
  51.   }
  52.  
  53.   // attempt to connect to WiFi network:
  54.   while (wifiStatus != WL_CONNECTED) {
  55.     Serial.print("Attempting to connect to SSID: ");
  56.     Serial.println(ssid);
  57.     // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
  58.     wifiStatus = WiFi.begin(ssid, password);
  59.  
  60.     // wait 10 seconds for connection:
  61.     delay(10000);
  62.   }
  63.  
  64.   Serial.println("Connected to WiFi");
  65.   printWifiStatus();
  66.   return;
  67. }
  68.  
  69. void setup() {
  70.   Serial.begin(115200);
  71.   connectToWiFi();
  72.   RTC.begin();
  73.   Serial.println("\nStarting connection to server...");
  74.   timeClient.begin();
  75.   timeClient.update();
  76.  
  77.   // Get the current date and time from an NTP server and convert
  78.   // it to UTC +2 by passing the time zone offset in hours.
  79.   // You may change the time zone offset to your local one.
  80.   auto timeZoneOffsetHours = 10;
  81.   auto unixTime = timeClient.getEpochTime() + (timeZoneOffsetHours * 3600);
  82.   Serial.print("Unix time = ");
  83.   Serial.println(unixTime);
  84.   RTCTime timeToSet = RTCTime(unixTime);
  85.   RTC.setTime(timeToSet);
  86.  
  87.   // Retrieve the date and time from the RTC and print them
  88.   RTCTime currentTime;
  89.   RTC.getTime(currentTime);
  90.   Serial.println("The RTC was just set to: " + String(currentTime));
  91.   lastSecond = currentTime.getSeconds();
  92. }
  93.  
  94. void myClock(void){
  95.   RTCTime currentTime;
  96.   RTC.getTime(currentTime);
  97.  
  98.   if(currentTime.getSeconds() != lastSecond){
  99.  
  100.     // Print out date (DD/MM//YYYY)
  101.     Serial.print(currentTime.getDayOfMonth());
  102.     Serial.print("/");
  103.     Serial.print(Month2int(currentTime.getMonth()));
  104.     Serial.print("/");
  105.     Serial.print(currentTime.getYear());
  106.     Serial.print(" - ");
  107.  
  108.     // Print time (HH/MM/SS)
  109.     Serial.print(currentTime.getHour());
  110.     Serial.print(":");
  111.     Serial.print(currentTime.getMinutes());
  112.     Serial.print(":");
  113.     Serial.println(currentTime.getSeconds());
  114.  
  115.     //Update one second delay
  116.     lastSecond = currentTime.getSeconds();
  117.   }
  118. }
  119.  
  120. void loop() {
  121.   myClock();
  122. }
Advertisement
Add Comment
Please, Sign In to add comment