Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getTime appears to be returning the wrong date #284

Open
tigoe opened this issue Jan 11, 2020 · 10 comments
Open

getTime appears to be returning the wrong date #284

tigoe opened this issue Jan 11, 2020 · 10 comments

Comments

@tigoe
Copy link

tigoe commented Jan 11, 2020

WIFi.getTime() appears to be returning the wrong date by one day.

Using this code, I should be getting 11/1/2020 today, but I am getting 10/1/2020. I'm not clear on why, but the time is correct.

Running this on a MKR1000, library version 0.16.0

@S2Doc
Copy link

S2Doc commented Jan 14, 2020

I'm having the same or similar problem. 3 or 4 out of 100 devices I have in the field are suddenly showing times that are exactly 24 hours before the actual time.

Using Adafruit Feather M0 with WINC 15000.

This is causing significant problems. Any help would be greatly appreciated.

@S2Doc
Copy link

S2Doc commented Feb 5, 2020

In my case the problem turned out to be that the ATWINC1500 wifi module was running firmware v19.4.4. Microchip's revision notes indicate that version had problems of some sort with NTP servers. Upgrading the module's firmware to v19.5.4 or higher from the Arduino IDE fixed the problem.

Let us know if that works for you.

@tigoe
Copy link
Author

tigoe commented Feb 7, 2020

The problem persists on the ESP32 module on the MKR1010 and Nano33 IoT boards, though

@S2Doc
Copy link

S2Doc commented Feb 7, 2020

The ESP32 doesn't use WiFi101, does it?

@tigoe
Copy link
Author

tigoe commented Feb 14, 2020

No, but the WiFi101 and the WiFiNINA modules share the same WiFi core API. Posted it here, but had hit the problem on both.

@Rocketct
Copy link
Contributor

Hi @tigoe i tried the sketch in the the wifi1010 firmware v1.3.0 and i get the same results of the fixed mkr1000 tomorrow when i'll get i'll try also the 33 iot

@Rocketct
Copy link
Contributor

Rocketct commented Feb 28, 2020

@tigoe run on 33 Iot and works fine too

@yanliu888
Copy link

Same issue as @tigoe mentioned here - it suddenly changed to a wrong time during measuring. How to fix it? I don't see a higher version of firmware in Arduino software for NANO 33 IoT board - the latest version is version 1.4.5. Thank you very much in advance!

@yanliu888
Copy link

yanliu888 commented Jun 27, 2021

For my case, I just figured out the example code I'm using sets:

set_hour = rtc_wifi.getHours() + GMT;

but set_hour cannot be negative, so I fix the code by deleting GMT in the above line and converting UTC to GMT like this:

rtc_wifi.setEpoch(epoch + GMT * 60 *60);

Sorry, I didn't see your code posted. It seems it didn't convert UTC to GMT.

Here's my entire code:

#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:

}

@tigoe
Copy link
Author

tigoe commented Jun 28, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants