The RTC on the Uno R4 Wifi works fine until you try to use it while sending data to the Cloud. I am running a simple weather station that records data to the cloud. I need to know the time to record rainfall by the hour and day. I have to use an external RTC. For the on board RTC, I can set the time and it works fine but as soon as I upload and run my Sketch for my Thing. The on board RTC has the hour value reset to a different value. The Year, Month, Day, and Minute all remain correct. It is only the Hour value which changes.
Is the hour off by a specific amount? As in the difference between local and UTC time?
I thought that it may be as I was getting 7 hours difference around mid day in Thailand. However on further testing that was not the case. I tried setting the RTC to different times (including 24h 58m to see if the day changed) but it had not effect on the hour value which the RTC changed to. I tried it on the Arduino IDE and it worked fine. I tried it on a Sketch on the web site Cloud but without any wifi connection. It worked fine. It was only when I added it to my weather station Sketch which had wifi provisions for use in a Dashboard, that the problem occurred. Interestingly watching on the Serial Monitor, it gave me two correct values (one second apart) and then changed to the new Hour value. There has to be some interference between the provisions for wifi and the provisions to read the on board RTC.
Topic moved. Please do not post in "Uncategorized"; see the sticky topoics in Uncategorized - Arduino Forum.
Hello @anthonyalansullivan,
I'm sorry to hear you're having trouble with the RTC on your Uno R4 WiFi. If you're using Arduino Cloud, a straightforward solution would be to use a CloudTime variable (CloudTime variableName;
) to get the time directly from Arduino Cloud, which should provide an accurate time reference. You could, for instance, update the time in each iteration of your loop like this:
currentTime = ArduinoCloud.getLocalTime();
This value can also be displayed on your dashboard using a Time Picker Widget to visualize the time data in real-time.
If this approach doesn't fully meet your needs, please feel free to share a simplified sample of your code, specifically focusing on the RTC section where the issue occurs. Alternatively, if you'd prefer direct support for a deeper troubleshooting process, you can contact our support team via this form: Contact Support Form.
I hope this helps resolve the RTC issue so you can continue with your project!
Hello Arduino Team,
Thanks for your kind response. It is a shame there is a problem with the onboard RTC when using wifi but I will certainly try out your interesting proposal.
kind regards
Anthony
As usual nothing is simple.
Could you tell me the code to get hours, days etc as integer variables from this?
thanks
Anthony
Sure, I’ll walk you through it with an example!
When we retrieve time using ArduinoCloud.getLocalTime();
, we're getting an integer value representing time in epoch format (epoch time). This is essentially the number of seconds since January 1, 1970. We can convert this to a readable date and time using the setTime
function, which is part of the TimeLib library (be sure to include TimeLib.h
in your code).
Below is a code snippet showing how to convert the epoch time into individual integers for hours, minutes, seconds, day, month, and year:
#include "thingProperties.h"
#include <TimeLib.h>
int currentHour;
int currentMinute;
int currentSecond;
int currentDay;
int currentMonth;
int currentYear;
void setup() {
// Initialize serial and wait for port to open
Serial.begin(9600);
delay(1500);
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}
void loop() {
ArduinoCloud.update();
// Get the epoch time from Arduino Cloud
currentTime = ArduinoCloud.getLocalTime();
setTime(currentTime); // Convert epoch time to readable time
// Store individual date and time values as integers
currentHour = hour();
currentMinute = minute();
currentSecond = second();
currentDay = day();
currentMonth = month();
currentYear = year();
// Print values for debugging
Serial.print("Cloud Time (epochTime): ");
Serial.println(currentTime);
Serial.print("Hour: "); Serial.println(currentHour);
Serial.print("Minute: "); Serial.println(currentMinute);
Serial.print("Second: "); Serial.println(currentSecond);
Serial.print("Day: "); Serial.println(currentDay);
Serial.print("Month: "); Serial.println(currentMonth);
Serial.print("Year: "); Serial.println(currentYear);
}
Note: Make sure you've declared a variable of type CloudTime in the setup of your Thing. This will allow you to retrieve the time from Arduino Cloud accurately.
Give this a try and let me know if it helps!
Hello and many thanks for your kind help.
I had to declare the integer variable currentTime at the beginning. No problem.
I cannot find the library TimeLib.h in the list of libraries. Not sure if that is important. I have it on my Arduino IDE software.
However I ended up with this:
/usr/local/bin/arduino-cli compile --fqbn arduino:renesas_uno:unor4wifi --build-cache-path /tmp --output-dir /tmp/1288032619/build --build-path /tmp/arduino-build-2CDC74B8536ED364D554989A05E08ECC /tmp/1288032619/_Cloud_Time_oct27a
/tmp/arduino-build-2CDC74B8536ED364D554989A05E08ECC/sketch/objs.a(_Cloud_Time_oct27a.ino.cpp.o): In function initProperties()': /tmp/1288032619/_Cloud_Time_oct27a/thingProperties.h:30: **undefined reference to
onVCloudTimeChange()'**
collect2: error: ld returned 1 exit status
Multiple libraries were found for "RTC.h"
Used: /home/builder/.arduino15/packages/arduino/hardware/renesas_uno/1.2.2/libraries/RTC
Not used: /home/builder/opt/libraries/m5stickcplus_0_1_0
Not used: /home/builder/opt/libraries/m5core2_0_1_9
Not used: /home/builder/opt/libraries/m5stickc_0_3_0
Not used: /home/builder/opt/libraries/m5station_0_0_1
Multiple libraries were found for "TimeLib.h"
Used: /home/builder/opt/libraries/time_1_6_1
Not used: /home/builder/opt/libraries/esp32_control_1_2_8
Not used: /home/builder/opt/libraries/dfrobot_oled12864_1_0_0
Error during build: exit status 1
Can you help please?
best wishes
Anthony
The actual library is the Time library by Paul Stoffregen.
Originally the library used Time.h as the name of the header file, but it was changed to TimeLib.h because of confusion with the standard C library header file time.h (all lower case). On an operating system such as Windows, the file system is not case-sensitive, so there is no difference between Time.h and time.h, causing the compiler to sometimes choose the wrong library.
This is the error message you need to focus on fixing.
When you configure an Arduino Cloud IoT Variable as "Read & Write" (which is the default configuration), Arduino Cloud calls a "callback function" in your sketch whenever the value of the Variable is changed via the Arduino Cloud IoT dashboard. This error message indicates that your Thing has a Variable named VCloudTime
, but your sketch code does not contain the onVCloudTimeChange()
callback function definition for that Variable.
Arduino Cloud automatically adds empty callback function definitions to the sketch so you must have removed it from your sketch code, or else there is a bug in your code that causes the definition to not be recognized. If it is missing, add the function to your sketch code to fix the error:
void onVCloudTimeChange() {}
Dear Team,
Sorry for my stupid error. Need better quality control!
Works a treat now and great not to need any clock on the board.
best wishes
Anthony