Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
cloudDev
Published © LGPL

RTC with WiFi and MKR IoT Carrier Rev2

Here is code for a modern awesome real time clock (RTC) for MKR IoT Carrier Rev 2. There is a date and time display.

BeginnerFull instructions provided6 minutes568
RTC with WiFi and MKR IoT Carrier Rev2

Things used in this project

Hardware components

Arduino MKR Carrier Rev2
×1
Arduino MKR WiFi 1010
Arduino MKR WiFi 1010
×1
Li-Ion Battery 5000mAh (18650)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

01.04.2023 Ver2

Arduino
New update!
#include <WiFiNINA.h>
#include <Arduino_MKRIoTCarrier.h>
#include <WiFiUdp.h>
#include <NTPClient.h>
#include <TimeLib.h> // Include the TimeLib library
#include <Fonts/FreeMonoBold9pt7b.h> // Include the FreeSansBold9pt7b font

MKRIoTCarrier carrier;

// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);

void setup() {
  const char* ssid = "XXX";
  const char* password = "XXX";

  Serial.begin(9600);
  carrier.begin();
  carrier.display.setRotation(0);

  carrier.display.fillScreen(ST77XX_BLACK);
  carrier.display.setTextSize(3);

  // Connect to Wi-Fi network with SSID and password
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }

  timeClient.begin();
  // Set time offset to your local timezone in seconds
  timeClient.setTimeOffset(7200);
}


void printTime() {
  timeClient.update();

  if (timeClient.getSeconds() == 0) { // Check if a full minute has been reached
    String formattedTime = timeClient.getFormattedTime().substring(0, 5); // Get the formatted time string and keep only the hours and minutes

    setTime(timeClient.getEpochTime()); // Set the internal time using the epoch time from the NTP client
    String formattedDate = String(day()) + "." + String(month()) + "." + String(year()); // Get the formatted date string using the TimeLib functions

int16_t x1, y1, x2, y2;
uint16_t w1, h1, w2, h2;
uint16_t width = carrier.display.width();
uint16_t height = carrier.display.height();

uint8_t r, g, b;
if (hour() >= 5 && hour() < 7) {
  for (int y = 0; y < height; y++) {
    r = map(y, 0, height-1, 255, 255);
    g = map(y, 0, height-1, 255, 128);
    b = map(y, 0, height-1, 0, 0);
    carrier.display.drawFastHLine(0, y, width, carrier.display.color565(r, g, b));
    carrier.display.setTextColor(ST77XX_BLACK);
  }
} else if (hour() >= 20 || hour() < 4) {
  // set background color to gradient from gray to black
  for (int y = 0; y < height; y++) {
    r = map(y, 0, height-1, 64, 0);
    g = map(y, 0, height-1, 64, 0);
    b = map(y, 0, height-1, 64, 0);
    carrier.display.drawFastHLine(0, y, width, carrier.display.color565(r, g, b));
    carrier.display.setTextColor(ST77XX_WHITE);
  }
} else {
  // set background color to gradient from red to blue
  for (int y = 0; y < height; y++) {
    r = map(y, 0, height-1, 255, 0);
    g = 0;
    b = map(y, 0, height-1, 0, 255);
    carrier.display.drawFastHLine(0, y, width, carrier.display.color565(r, g, b));
    carrier.display.setTextColor(ST77XX_WHITE);
  }
}


 //   carrier.display.setTextColor(ST77XX_WHITE);
    carrier.display.setTextSize(3);

    carrier.display.getTextBounds(formattedDate, 0, 0, &x1, &y1, &w1, &h1); // Get the bounds of the date text
    carrier.display.setTextSize(4);
    carrier.display.getTextBounds(formattedTime, 0, 0, &x2, &y2, &w2, &h2); // Get the bounds of the time text
    carrier.display.setTextSize(3);

    x1 = (carrier.display.width() - w1) / 2; // Calculate the X position to center the date text
    y1 = (carrier.display.height() - h1 - h2 - h2) / 2; // Calculate the Y position to place the date text above the time text and the empty line

    x2 = (carrier.display.width() - w2) / 2; // Calculate the X position to center the time text
    y2 = y1 + h1 + h2; // Calculate the Y position to place the time text below the date text and the empty line

    carrier.display.setCursor(x1, y1); // Set cursor position for the date text
    carrier.display.println(formattedDate); // Print the date text
    
    carrier.display.setTextSize(4);
    carrier.display.setCursor(x2, y2); // Set cursor position for the time text
    carrier.display.println(formattedTime); // Print the time text
  }

  delay(1000);
}



void loop() {
printTime();

}

01.04.2023

Arduino
#include <WiFiNINA.h>
#include <Arduino_MKRIoTCarrier.h>
#include <WiFiUdp.h>
#include <NTPClient.h>
#include <TimeLib.h> // Include the TimeLib library

MKRIoTCarrier carrier;

// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);

void setup() {
  const char* ssid = "XXX";
  const char* password = "XXX";

  Serial.begin(9600);
  carrier.begin();
  carrier.display.setRotation(0);

  carrier.display.fillScreen(ST77XX_BLACK);
  carrier.display.setTextSize(2);

  // Connect to Wi-Fi network with SSID and password
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }

  timeClient.begin();
  // Set time offset to your local timezone in seconds
  timeClient.setTimeOffset(7200);
}

void printTime() {
  timeClient.update();

  if (timeClient.getSeconds() == 0) { // Check if a full minute has been reached
    String formattedTime = timeClient.getFormattedTime().substring(0, 5); // Get the formatted time string and keep only the hours and minutes

    setTime(timeClient.getEpochTime()); // Set the internal time using the epoch time from the NTP client
    String formattedDate = String(day()) + "." + String(month()) + "." + String(year()); // Get the formatted date string using the TimeLib functions

    int16_t x1, y1, x2, y2;
    uint16_t w1, h1, w2, h2;
    uint16_t width = carrier.display.width();
uint16_t height = carrier.display.height();

for (int y = 0; y < height; y++) {
  uint8_t r = map(y, 0, height-1, 255, 0);
  uint8_t g = 0;
  uint8_t b = map(y, 0, height-1, 0, 255);
  
  carrier.display.drawFastHLine(0, y, width, carrier.display.color565(r, g, b));
}
    carrier.display.setTextColor(ST77XX_WHITE);
    carrier.display.setTextSize(2);

    carrier.display.getTextBounds(formattedDate, 0, 0, &x1, &y1, &w1, &h1); // Get the bounds of the date text
    carrier.display.getTextBounds(formattedTime, 0, 0, &x2, &y2, &w2, &h2); // Get the bounds of the time text

    x1 = (carrier.display.width() - w1) / 2; // Calculate the X position to center the date text
    y1 = (carrier.display.height() - h1 - h2 - h2) / 2; // Calculate the Y position to place the date text above the time text and the empty line

    x2 = (carrier.display.width() - w2) / 2; // Calculate the X position to center the time text
    y2 = y1 + h1 + h2; // Calculate the Y position to place the time text below the date text and the empty line

    carrier.display.setCursor(x1, y1); // Set cursor position for the date text
    carrier.display.println(formattedDate); // Print the date text
    
    carrier.display.println(); // Print an empty line

    carrier.display.setCursor(x2, y2); // Set cursor position for the time text
    carrier.display.println(formattedTime); // Print the time text
  }

  delay(1000);
}

void loop() {
printTime();

}

Credits

cloudDev
2 projects • 2 followers
Contact

Comments

Please log in or sign up to comment.