Hi there,
I'm using NANO 33 IoT board. I try to fetch time online by using RTCZero and will also need to use WiFi.getTime to get the initial epoch time. However, the epoch time is 11 hours ahead. Do you know how to fix this?
Thanks in advance!
Hi there,
I'm using NANO 33 IoT board. I try to fetch time online by using RTCZero and will also need to use WiFi.getTime to get the initial epoch time. However, the epoch time is 11 hours ahead. Do you know how to fix this?
Thanks in advance!
Can you please post your sketch, so I can run a test on my board?
Please see attached. It needs to configure WiFi ID and password and GMT timezone. I'm now trying to change rtc_wifi.setEpoch(epoch); to rtc_wifi.setEpoch(epoch - 11 * 60 * 60); to temporarily solve the time issue.
But I still wonder if I did something wrong or there's anything I can fix.
Thank you!
real_time_clock.ino (2.0 KB)
The following line in your code is definitively not correct.
set_hour = rtc_wifi.getHours() + GMT;
e.g., UTC 01:00:00, hour = 1, 1 + (-5) = -4 (that is not a valid value)
All time calculation should be done in epoch format. That ensures you get all the time overflow from one time element to another e.g., seconds to minutes and hours to days correct.
When I set GMT to 0, I get correct UTC time information printed by your sketch.
For future reference, short sketches are better posted directly using code tags. When you click on this icon </> you get.
```
type or paste code here
```
Make sure your entire code ends up in one box. It should look like this.
// Your example code
#include "arduino_secrets.h"
///////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
I also recommend you have a look at the WiFiNINA examples. They use a separate file called arduino_secrets.h for the WiFi credentials. Posting examples using this allows everyone to just copy their file into the sketch directory and compile your example code. And becomes unlikely that you publish your ssid and password by accident.
Thank you very much for pointing it out!
I just realized the epoch is right!
Haha, sorry! I fixed again the code by trying to converting UTC to GMT as follows:
rtc_wifi.setEpoch(epoch + GMT * 60 *60);
and deleting the GMT in the following line:
set_hour = rtc_wifi.getHours();
Now, the code becomes:
#include <WiFiNINA.h>
#include <WiFiUdp.h>
#include <RTCZero.h>
char *ssid = " "; // WiFi credentials and setup
char *password = " ";
const int GMT = -5; // Time zone constant - change as required for your location
RTCZero rtc_wifi;
int status = WL_IDLE_STATUS;
byte set_year = 0; // can only be 2-digit year
byte set_month = 0;
byte set_date = 0;
byte set_hour = 0;
byte set_minute = 0;
byte set_second = 0;
void initRealTime() {
set_year = rtc_wifi.getYear(); // can only be 2-digit year
set_month = rtc_wifi.getMonth();
set_date = rtc_wifi.getDay();
set_hour = rtc_wifi.getHours();
set_minute = rtc_wifi.getMinutes();
set_second = rtc_wifi.getSeconds();
}
void setup() {
Serial.begin(115200);
Serial.println("Serial begins!");
// Establish a WiFi connection
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, password);
// Wait 10 seconds for connection:
delay(10000);
}
// // Print connection status
// printWiFiStatus();
// Start Real Time Clock
rtc_wifi.begin();
// Variable to represent epoch
unsigned long epoch;
// Variable for number of tries to NTP service
int numberOfTries = 0, maxTries = 6;
// Get epoch
do {
epoch = WiFi.getTime();
numberOfTries++;
}
while ((epoch == 0) && (numberOfTries < maxTries));
if (numberOfTries == maxTries) {
Serial.print("NTP unreachable!!");
while (1);
}
else {
Serial.print("Epoch received: ");
Serial.println(epoch);
rtc_wifi.setEpoch(epoch + GMT * 60 *60);
Serial.println();
}
initRealTime();
Serial.println("Initializing real time done.");
Serial.println("Online date & time: " + String(set_year) + "-" + String(set_month) + "-" + String(set_date) + " " + String(set_hour) + ":" + String(set_minute) + ":" + String(set_second));
}
void loop() {
// put your main code here, to run repeatedly:
}
Thank you so much again, also for the WiFi setting advice! And please correct me if I did something redundant or wrong!
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.