marciot / esp32-tutorials

Small ESP32 Hacks and Tutorials
GNU General Public License v3.0
3 stars 1 forks source link

It cannot obtain time ... #1

Open mrWheel opened 2 years ago

mrWheel commented 2 years ago

Great project!

I tried it without any change (accept for the WiFi credentials) and this is the output:

⸮⸮�E (169) psram: PSRAM ID read error: 0xffffffff
Connecting to myNetwork ..... CONNECTED
Failed to obtain time
Failed to obtain time
Failed to obtain time
Failed to obtain time
Failed to obtain time
Failed to obtain time
Failed to obtain time
Failed to obtain time
Failed to obtain time

Is something broken?

"bare" ESP32 Wrover Module

Just tested this code:

/*
  Rui Santos
  Complete project details at https://RandomNerdTutorials.com/esp32-date-time-ntp-client-server-arduino/

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files.

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.
*/

#include <WiFi.h>
#include "time.h"

const char* ssid     = "myNetwork";
const char* password = "********";

const char* ntpServer = "pool.ntp.org";
const long  gmtOffset_sec = 0;
const int   daylightOffset_sec = 3600;

void setup(){
  Serial.begin(115200);

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

  // Init and get the time
  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
  printLocalTime();

  //disconnect WiFi as it's no longer needed
  WiFi.disconnect(true);
  WiFi.mode(WIFI_OFF);
}

void loop(){
  delay(1000);
  printLocalTime();
}

void printLocalTime(){
  struct tm timeinfo;
  if(!getLocalTime(&timeinfo)){
    Serial.println("Failed to obtain time");
    return;
  }
  Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
  Serial.print("Day of week: ");
  Serial.println(&timeinfo, "%A");
  Serial.print("Month: ");
  Serial.println(&timeinfo, "%B");
  Serial.print("Day of Month: ");
  Serial.println(&timeinfo, "%d");
  Serial.print("Year: ");
  Serial.println(&timeinfo, "%Y");
  Serial.print("Hour: ");
  Serial.println(&timeinfo, "%H");
  Serial.print("Hour (12 hour format): ");
  Serial.println(&timeinfo, "%I");
  Serial.print("Minute: ");
  Serial.println(&timeinfo, "%M");
  Serial.print("Second: ");
  Serial.println(&timeinfo, "%S");

  Serial.println("Time variables");
  char timeHour[3];
  strftime(timeHour,3, "%H", &timeinfo);
  Serial.println(timeHour);
  char timeWeekDay[10];
  strftime(timeWeekDay,10, "%A", &timeinfo);
  Serial.println(timeWeekDay);
  Serial.println();
}

.. and that works ..

Minute: 12
Second: 08
Time variables
12
Saturday

Saturday, March 26 2022 12:12:09
Day of week: Saturday
Month: March
Day of Month: 26
Year: 2022
Hour: 12
Hour (12 hour format): 12
Minute: 12
Second: 09
Time variables
12
Saturday

Saturday, March 26 2022 12:12:10
Day of week: Saturday
Month: March
Day of Month: 26
Year: 2022
Hour: 12
Hour (12 hour format): 12
Minute: 12
Second: 10
Time variables
12
Saturday

Saturday, March 26 2022 12:12:11
.
.
mrWheel commented 2 years ago

Strange ..?

If I put this in setup():

  //-- 0, 0 because we will use TZ in the next line
  configTime(0, 0, ntpServer);
  //-- Set environment variable with your time zone
  setenv("TZ", getTzByLocation(TzLocation).c_str(), 1);
  tzset();
  printLocalTime();

then it "works"!

Attempting to connect to WPA SSID: myNetwork
setup          (  79): Connected to WiFi network.
setup          (  80): Connect with Telnet client to 192.168.12.141
printLocalTime (  22): Saturday, March 26 2022 13:27:25
HTTP httpServer started
printLocalTime (  22): Saturday, March 26 2022 13:28:20
marciot commented 2 years ago

@mrWheel: Can you test something for me? In the original code, can you add the following for me?

#define ARDUINO_ARCH_ESP32

Right above:

#include "time_zones.h"

My guess is that the identifier ARDUINO_ARCH_ESP32 not being defined for your board, so instead of using the ESP32 code, it was using the code for the ESP8266.

mrWheel commented 2 years ago

@marciot Ok. added the #define:

#include <WiFi.h>
#define ARDUINO_ARCH_ESP32
#include "time_zones.h"

With no change. Still cannot obtain the time :-(

Failed to obtain time
Failed to obtain time
.

I had already changed configTimeWithTz() to:

void configTimeWithTz(String tz, String ntp_addr) {
    #ifdef ESP8266
        // ESP8266
        configTime(tz.c_str(), ntp_addr.c_str()); // --> for the ESP8266 only
    #else
        // ESP32 seems to be a little more complex:
        configTime(0, 0, ntp_addr.c_str()); // 0, 0 because we will use TZ in the next line
        setenv("TZ", tz.c_str(), 1); // Set environment variable with your time zone
        tzset();
    #endif
}

As most libraries nowadays test for ESP8266

marciot commented 2 years ago

@mrWheel: Did you have an extra #include? If that wasn't a typo, then try removing that and see if it makes a difference.

#include <-- Not this
#define ARDUINO_ARCH_ESP32
#include "time_zones.h"

As most libraries nowadays test for ESP8266

Good to know! I can change that.

mrWheel commented 2 years ago

Did you have an extra #include? No, it is "#include

Good to know! I can change that.

But it did not make it "work"!!!

marciot commented 2 years ago

@mrWheel: How about changing configTimeWithTz to:

void configTimeWithTz(String tz, String ntp_addr) {
        // ESP32 seems to be a little more complex:
        configTime(0, 0, ntp_addr.c_str()); // 0, 0 because we will use TZ in the next line
        setenv("TZ", tz.c_str(), 1); // Set environment variable with your time zone
        tzset();
}

Since this is a ESP32 example, I guess I don't really need code for the ESP8266

mrWheel commented 2 years ago

Already did that too.

I think the problem is in the scope of setenv() or tzset() ..

marciot commented 2 years ago

@mrWheel: That's so peculiar.

mrWheel commented 2 years ago

Maybe it has to do with executing “something” on the wrong CPU and than losing focus?

BioEngLuisPereira commented 2 years ago

Hi

I have the same problem, and already tried the #define ARDUINO_ARCH_ESP32 suggestion. I'm on arduino IDE 1.8.19, board:ESP32 Dev Moduke, esp32 library 2.0.3 .

andrewbierer commented 1 year ago

Hello,

Writing to inquire if anyone has found a solution at this time. I am running Arduino 2.0.1 on board Adafruit ESP32 Feather.

andrewbierer commented 1 year ago

For anyone looking for a workaround, if you identify the element number of the timezone of interest you can still call: configTzTime(timezones[element_of_interest], ntpserver1, ntpserver2);