Getting Local Time without gmt Offset after Deep Sleep

User avatar
carlossilva132
Posts: 2
Joined: Sun Apr 15, 2018 10:28 pm

Getting Local Time without gmt Offset after Deep Sleep

Postby carlossilva132 » Sun Apr 15, 2018 10:48 pm

I'm getting time with SNTP (-3:00GMT), setting RTC with configTime. Check time, is ok.
Go to sleep.
After wake up, the time is wrong (0GMT), without gmtOffset.
I need to use setenv(); and tzset()? How?

Code: Select all

/* Pega horário no SNTP e acerta o relógio interno
faz o sleep para verificar se o relógio continua funcionando.
*/
#include <WiFi.h>
#include "time.h"

#include "esp_deep_sleep.h"
#define uS_TO_m_FACTOR 60000000   // Conversion factor for micro seconds to minutes
#define TIME_TO_SLEEP  1        // Time ESP32 will go to sleep (in minutes)

const char* ssid       = "xxxxxx";
const char* password   = "xxxxxxx";
const char* ntpServer = "200.160.7.186"; // "a.st1.ntp.br";
const long  gmtOffset_sec = -3 * 3600;
const int   daylightOffset_sec = 0;  // se estiver horário verão=3600;

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("Epoch:");
  Serial.println(mktime(&timeinfo));
}

void setup()
{
  Serial.begin(115200);
  Serial.printf("Connecting to %s ", ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
      delay(500);
  }
  //init and get the time
  struct tm timeinfo2;
  if(!getLocalTime(&timeinfo2)){
    Serial.println("Relógio nao foi configurado.");
    configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
  }
  printLocalTime();

  WiFi.disconnect(true);
  WiFi.mode(WIFI_OFF);

  esp_deep_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_m_FACTOR);
  Serial.println("Going to sleep now");
  esp_deep_sleep_start();
}

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

papaluna
Posts: 50
Joined: Tue Jan 30, 2018 11:27 am

Re: Getting Local Time without gmt Offset after Deep Sleep

Postby papaluna » Mon Apr 16, 2018 9:07 am

In ESP-IDF you would do this:

Code: Select all

void mjd_set_timezone_utc() {
    ESP_LOGD(TAG, "%s()", __FUNCTION__);

    // @special "" means timezone UTC (Specifying "UTC" does not work!)
    setenv("TZ", "", 1);
    tzset();
}

void mjd_set_timezone_amsterdam() {
    ESP_LOGD(TAG, "%s()", __FUNCTION__);

    // @doc https://remotemonitoringsystems.ca/time-zone-abbreviations.php
    // @doc timezone UTC = UTC
    setenv("TZ", "CET-1CEST-2,M3.5.0/02:00:00,M10.5.0/03:00:00", 1);
    tzset();
}
--
Paul.

User avatar
carlossilva132
Posts: 2
Joined: Sun Apr 15, 2018 10:28 pm

Re: Getting Local Time without gmt Offset after Deep Sleep

Postby carlossilva132 » Sat Apr 21, 2018 3:22 pm

Paul,
it's works (in Arduino)!

Thank you very much!

doronby
Posts: 1
Joined: Thu Jan 31, 2019 6:09 pm

Re: Getting Local Time without gmt Offset after Deep Sleep

Postby doronby » Thu Jan 31, 2019 6:11 pm

can you please post a complete code that works.
Thanks !

Who is online

Users browsing this forum: Google [Bot] and 113 guests