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();
}