My goal is: Only go to loop() after I synced with any NTP server.
Why it's important: Sometimes the servers may take longer time to answer, or the wifi connection isn't fast enough or any other anormal event that prevents the RTC to be synced with quickness
Why I'm posting this: I don't know the commands I have available for each package included in my code. There is probably just one command that does it. I want your help.
Current code, simplified, below (I deleted a lot of application lines and added some comments):
OBS: I'm brazilian. Don't mind some names and phrases in the code
#include <EEPROM.h>
#include <time.h>
//https://sourceware.org/newlib/libc.html#ctime
#include <ESP8266WiFi.h>
#include <Wire.h>
#include <ESP32Time.h>
#include <math.h>
const float interval = 15.0;
unsigned long previousMillis = 0;
ESP32Time rtc(3600);
const long gmtOffset_sec = -4; //estou usando como UTC no caso estamos em UTC-3. nao sei por que tem que ter mais um
const int daylightOffset_sec = 0;
const char* ssid = "**********";
const char* pass = "**************";
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED){
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Conectado a: ");
Serial.println(ssid);
configTime(gmtOffset_sec*60*60, daylightOffset_sec, "br.pool.ntp.org", "time.nist.gov","2.br.pool.ntp.org");
Serial.println("Favor nao desconectar a internet");
delay(7000);
Serial.println("Pode desconectar");
}
void loop() {
static boolean rede = true;
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
//this code is good to prevent using delay() which stops your whole proccess.
//It's very useful if you want different things to work with different frequencies
//for performance and for control of how fast the loop() is running
if(WiFi.status() != WL_CONNECTED){
if(rede == true){
rede = false;
//code that runs once when your wifi is disconnected while execution
}
if((millis()%7000)<100){
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
//this is needed because each try to reconnect must have a time to proccess.
//It's not optimized, I know, but it's working for now
}
}
}
if(WiFi.status() == WL_CONNECTED && rede == false){
rede = true;
//code that runs once when you reconnect
}
}
What I want: something that grants me the date is synced, overwritting the 3 lines:
Serial.println("Favor nao desconectar a internet");
delay(7000);
Serial.println("Pode desconectar");
to something that should look like:
while(lastSyncEvent==NULL){
//and idk, maybe some Serial.print to show that we are waiting for
//any of the NTP servers from configTime() answer?
}
I'd appreciate a lot of your help. I already looked for answers on this forum and from other references but nothing useful.
You can test it by checking the getLocalTime() result.
See the example:
void initTime(String timezone){
struct tm timeinfo;
Serial.println("Setting up time");
configTime(0, 0, "pool.ntp.org"); // First connect to NTP server, with 0 TZ offset
if(!getLocalTime(&timeinfo)){
Serial.println(" Failed to obtain time");
return;
}
Serial.println(" Got the time from NTP");
// Now we can set the real timezone
setTimezone(timezone);
}
Old way, that doesn't do DS automatically
timezone and DST can be done with a location string. configTime("<-03>3", "br.pool.ntp.org", "time.nist.gov","2.br.pool.ntp.org"); // for Sao Paulo
coredecls.h can be used to wait, or see if NTP has synched.
Thank you for telling about the location strings in the command configTime(), it really helped.
The solution wasn't the one you suggested, because I was importing another rtc library and it was getting a little complex with your idea.. I solved my problem with this code:
#include <ESP8266WiFi.h>
#include <ESP32Time.h>
const char* ssid = "**********";
const char* pass = "*****************";
ESP32Time rtc(3600);
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED){
delay(500);
Serial.print(".");
}
configTime("<-3>3", "br.pool.ntp.org", "time.nist.gov","2.br.pool.ntp.org");
struct tm timeinfo; //I don't know what this command does.
//I removed it and the rtc time wasn't being updated to the current date
while(rtc.getLocalEpoch()<50000){
//this locks the proccess while I don't update the date
//50000 is an arbitrary number. It just needs to be lower than the current epoch
//https://www.epochconverter.com
}
}
loop(){
}