Get Epoch Time Automatically and Convert it to Readable Time and Set The Arduino R4 RTC

Поділитися
Вставка
  • Опубліковано 27 січ 2025

КОМЕНТАРІ • 15

  • @learnelectronics
    @learnelectronics  11 місяців тому +2

    /*
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ^ Uno R4 Wifi Get NTP Time & Set RTC ^
    ^ Initial author: Sebastian Romero @sebromero ^
    ^ adapted by learnelectronics 24 Feb 2024 ^
    ^ ua-cam.com/users/learnelectronics ^
    ^ Use the code however you like ^
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    */
    // Include the RTC library
    #include "RTC.h"
    //Include the NTP library
    #include
    // Include drivers for OLED
    #include
    #include
    #include
    #if defined(ARDUINO_PORTENTA_C33)
    #include
    #elif defined(ARDUINO_UNOWIFIR4)
    #include
    #endif
    #include
    #include "arduino_secrets.h"
    #define SCREEN_WIDTH 128 // OLED display width, in pixels
    #define SCREEN_HEIGHT 64 // OLED display height, in pixels
    #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
    #define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
    Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
    ///////please enter your sensitive data in the Secret tab/arduino_secrets.h
    char ssid[] = SECRET_SSID; // your network SSID (name)
    char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
    int wifiStatus = WL_IDLE_STATUS;
    WiFiUDP Udp; // A UDP instance to let us send and receive packets over UDP
    NTPClient timeClient(Udp);
    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");
    }
    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, pass);
    // wait 10 seconds for connection:
    delay(10000);
    }
    Serial.println("Connected to WiFi");
    printWifiStatus();
    }
    void setup(){
    Serial.begin(9600);
    while (!Serial);
    connectToWiFi();
    RTC.begin();
    Serial.println("
    Starting connection to server...");
    timeClient.begin();
    timeClient.update();
    // Get the current date and time from an NTP server and convert
    // it to UTC -5 by passing the time zone offset in hours.
    // You may change the time zone offset to your local one.
    auto timeZoneOffsetHours = -5;
    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));
    // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
    if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
    }
    // Show initial display buffer contents on the screen --
    // the library initializes this with an Adafruit splash screen.
    display.display();
    delay(2000); // Pause for 2 seconds
    // Clear the buffer
    display.clearDisplay();
    }
    void loop(){
    RTCTime currentTime;
    RTC.getTime(currentTime);
    display.setTextSize(1);
    display.setTextColor(SSD1306_WHITE);
    display.println(String(currentTime));
    display.display();
    delay(2000);
    //display.clearDisplay();
    }

  • @warrenking1815
    @warrenking1815 11 місяців тому

    Pretty neat, a complete clock that gets the info from the internet!

  • @davidward9935
    @davidward9935 11 місяців тому

    Paul, Great project. I hope you are doing well. I think my next step with the NTP clock would likely be a world clock with programmable zones. I work with people in multiple countries and time zones every day. I would need all of the North American time zones along with Germany, London, Alaska, New Zealand, Australia, and India. Would the OLED have enough lines? I guess I could have it scroll. I would not need seconds, only out to minutes. I think I will have to see what I have in the kit once I find where it is stored again. …Maybe a wall clock? David (ET1, USCGR, Ret.)

  • @robertbauer6723
    @robertbauer6723 11 місяців тому

    This is really cool. I have been thinking about creating an "analog clock" simulation with a strip of addressable LEDs wrapped around a circle. Not original I know, it's just my exploration of this. I am thinking about starting with a basic Arduino with no RTC. It will drift for sure. Adding an RTC will probably drift less. Later adding NTP via ESP with WiFi will make it spot-on forever. That got me thinking, why not use just an ESP32, instead of the Arduino/ESP board? Is it a logic level consideration, 5v vs 3.3v? One other idea is to have the ability to set the clock using WWV (in the US) or some other broadcast time signal. I am not sure such a module exists, just spitballing.

  • @aliancemd
    @aliancemd 11 місяців тому +1

    It’s unavoidable that it’s going to shift a few seconds from the actual time - that’s one of the reasons we call to NTP servers, we let the big boys with their atomic clocks and GPS(which account for Einstein's theory of relativity/Gravitational time dilation, btw) do the complicated stuff which these tiny devices can’t.

  • @larryplatzek9017
    @larryplatzek9017 11 місяців тому

    NICE IDEA too bad I could not read the code on your monitor (font small and dark)..

    • @MikeBramm
      @MikeBramm 11 місяців тому +1

      Looks perfectly readable to me. Were you watching on a phone?

    • @larryplatzek9017
      @larryplatzek9017 11 місяців тому

      NO on a 15 inch laptop hp Probook 6550b old but works will check on anewer laptop later today.@@MikeBramm

    • @learnelectronics
      @learnelectronics  11 місяців тому +2

      I'll post it in the comments when I get home later

    • @learnelectronics
      @learnelectronics  11 місяців тому +1

      /*
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      ^ Uno R4 Wifi Get NTP Time & Set RTC ^
      ^ Initial author: Sebastian Romero @sebromero ^
      ^ adapted by learnelectronics 24 Feb 2024 ^
      ^ ua-cam.com/users/learnelectronics ^
      ^ Use the code however you like ^
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      */
      // Include the RTC library
      #include "RTC.h"
      //Include the NTP library
      #include
      // Include drivers for OLED
      #include
      #include
      #include
      #if defined(ARDUINO_PORTENTA_C33)
      #include
      #elif defined(ARDUINO_UNOWIFIR4)
      #include
      #endif
      #include
      #include "arduino_secrets.h"
      #define SCREEN_WIDTH 128 // OLED display width, in pixels
      #define SCREEN_HEIGHT 64 // OLED display height, in pixels
      #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
      #define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
      Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
      ///////please enter your sensitive data in the Secret tab/arduino_secrets.h
      char ssid[] = SECRET_SSID; // your network SSID (name)
      char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
      int wifiStatus = WL_IDLE_STATUS;
      WiFiUDP Udp; // A UDP instance to let us send and receive packets over UDP
      NTPClient timeClient(Udp);
      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");
      }
      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, pass);
      // wait 10 seconds for connection:
      delay(10000);
      }
      Serial.println("Connected to WiFi");
      printWifiStatus();
      }
      void setup(){
      Serial.begin(9600);
      while (!Serial);
      connectToWiFi();
      RTC.begin();
      Serial.println("
      Starting connection to server...");
      timeClient.begin();
      timeClient.update();
      // Get the current date and time from an NTP server and convert
      // it to UTC -5 by passing the time zone offset in hours.
      // You may change the time zone offset to your local one.
      auto timeZoneOffsetHours = -5;
      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));
      // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
      if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
      Serial.println(F("SSD1306 allocation failed"));
      for(;;); // Don't proceed, loop forever
      }
      // Show initial display buffer contents on the screen --
      // the library initializes this with an Adafruit splash screen.
      display.display();
      delay(2000); // Pause for 2 seconds
      // Clear the buffer
      display.clearDisplay();
      }
      void loop(){
      RTCTime currentTime;
      RTC.getTime(currentTime);
      display.setTextSize(1);
      display.setTextColor(SSD1306_WHITE);
      display.println(String(currentTime));
      display.display();
      delay(2000);
      //display.clearDisplay();
      }

    • @larryplatzek9017
      @larryplatzek9017 11 місяців тому

      Sorry for not replying sooner power went out and also my internet went out. Thank you for posting the code. went to library still could not read code but today I used a different laptop and was able to read your screen not easy but doable , Thank you!

  • @bblod4896
    @bblod4896 11 місяців тому

    😮 Software! 😢
    ☮️ brother

    • @learnelectronics
      @learnelectronics  11 місяців тому +1

      I know. I know. Everybody hates this ! Ill just go work at McDonalds or the new Duncan donuts they are building in town.

    • @bblod4896
      @bblod4896 11 місяців тому

      @@learnelectronics
      Not true. There are plenty of software people out there. I just get a headache when I try to write code. No worries my friend.