Display Uno R4 RTC time with u8g2

I thought this would be simple but I am having major troubles. All I want to do is update the RTC with the WiFi and then print the time and date to an LCD screen using u8g2.

I don’t know if I am taking the correct approach. I just wanted to keep it simple. Disregarding the WiFi stuff as I have got that sorted out, the main problem is printing the time and date to the screen. I’m not interested in printing it to serial, which I have done.

Whenever I try and call a function from the RTC library it just says it wasn’t declared in this scope. When I try and use dtostrf to convert a value to a string, it says something about it being a double. I don’t understand and I was only copying someone else using dtostrf in the first place.

#include <U8g2lib.h>
#include "WiFiS3.h"
#include "arduino_secrets.h"
#include "RTC.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 keyIndex = 0;

#define GMTOffset_hour  10     // # hours difference to GMT
#define DayLightSaving  1

int status = WL_IDLE_STATUS;
char temp_string[6];

U8G2_ST7920_128X64_1_SW_SPI u8g2(U8G2_R0, /* clock=E*/ 13, /* data=*/ 11, /* CS=*/ 10, /* reset=*/ 8);

String GetRTC()
{
RTCTime currentTime;
RTC.getTime(currentTime);
return String(currentTime);
}

void setup() 
{  


u8g2.begin();
RTC.begin();
Serial.begin(115200);

while (!Serial) 
{
 ; // wait for serial port to connect. Needed for native USB port only
}

while (status != WL_CONNECTED) 
{
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
delay(10000);
}

if (WiFi.status() == WL_NO_MODULE) 
{
Serial.println("Communication with WiFi module failed.");
// don't continue
while (true);
}
printWifiStatus();
}

void loop() 
{
unsigned long EpochTime;
EpochTime = WiFi.getTime();

u8g2.firstPage();   
do 
{
u8g2.setFont(u8g2_font_lastapprenticebold_te);
u8g2.drawRFrame(0, 0, 128, 20, 7);
u8g2.drawStr(6, 15, "Time");

WHAT DO I PUT HERE TO PRINT TO THE TIME AND DATE TO THE LCD??

} 
while( u8g2.nextPage() );
delay(1000);  
}

void UpdateRTC(time_t EpochTime) 
{
auto timeZoneOffsetHours = GMTOffset_hour + DayLightSaving;
auto unixTime = EpochTime + (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));

// 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(":");
if (currentTime.getSeconds() < 10) 
{
Serial.print('0');
}
Serial.println(currentTime.getSeconds());
}

void printWifiStatus() 
{
Serial.print("SSID: ");
Serial.println(WiFi.SSID());

IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);

long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}

Can someone please help call the time and date to the LCD screen? I am completely out of ideas and it was out of my league to begin with.

Hi @tognacht.

The two statements seem to be contradictory. You say that you have been able to print the time and data to serial, but then you also say that whenever you try to call a function from the "RTC" library you get an compilation error.

Please provide the sketch code that produces the compilation error.

Please also provide the full and exact text of the output from compiling the sketch.

Please provide the full sketch code.

Have you successfully printed a simple message to the display? It is always good to start with a very simple sketch. This serves two purposes:

  • Gives you some familiarity with the basic operation of the library
  • Serves as a verification that the hardware is functional

You will usually be able to find such a sketch in the examples provided by the library author.

In this specific case, the example sketch accessed via File > Examples > U8g2 > u8x8 > HelloWorld in the Arduino IDE menus looks promising.

Even after you have been successful in printing that simple message, make sure to save a copy of the sketch somewhere handy. The reason is that it is useful to be able to fall back on a "known good" sketch if you later run into problems you need to troubleshoot. This "Hello, world!" sketch can serve as a "smoke test" to allow you to understand if the problem you are having is specific to the code of the larger program of your project (in which case the "Hello, world!" will still work as expected), or if something has instead happened to the hardware (in which case even the "Hello, world!" will not work).