ESP32 and simple RTC function strange behavior

Dear community.
I have two ESP32 which talk over LoRa data connection. A-ESP32 has a WiFi / Internet connection connection, B-ESP32 has no.
I need to have simple time function also in the B-ESP32.
With the first communication block setup() I transmitt one time only the data for the hour and minute in two int variables.

#include <Arduino.h>
#include <ESP32Time.h>
ESP32Time rtc;
 // _________rtc set  variables
int rtc_rec_min = 0;
int rtc_rec_hr = 0;
int LKH_hr;
...

void setup()
{
Serial.begin(9600);
rtc_rec_hr = (id_trans_t - id_trans*10000) / 100;                             // isolation Hour
rtc_rec_min = (id_trans_t - id_trans*10000) - (rtc_rec_hr *100);  // isolation minute
rtc.setTime(00, (rtc_rec_min), (rtc_rec_hr), 00, 00, 00);               // setting the RTC
}

void loop()
{
Serial.print ("Time :" ); Serial.print(rtc.getHour(true)); Serial.println (rtc.getMinute());

 if ((rtc.getHour(true)) == LKH_hr) 
{
do something
}

The reaction is now... the time is being set correctly (i.e. 17:15) , BUT after 6 seconds the time changed to "5:15" And this is the (true) time, not the US type display (means am/pm).

I have no clue, why this happens after 6-7 seconds and the begin is ok. WiFi is not activated in the B-ESP32.
Do I need to consider the timezone? But why is than the ESP32 changing after this waiting period and it cannot get any info about the time zone.

I'm completely irritated and would be grateful for any hint.
Thanks
Willythecat

it remembers the last WiFi connections and uses it to get time from Internet.
the zone is not set so the time is UTC.

You need to understand how ESP32Time works. It probably isn't what you want. If you need accurate time, either use NTP or a DS3231 type RTC.

Internet time on an ESP32 is easy. It comes with the core.
This is basically all you need, with the WiFi and printing part removed.
The unsigned long variable "now" holds epoch, and the tm.tm struct holds the year/month/day/ hour/min/sec/wday/DST variables.

time_t now; // epoch
tm tm; // time struct

void setup() {
  configTzTime("location", "pool"); // where on the planet
}

void loop() {
  time(&now); // update epoch
  localtime_r(&now, &tm); // convert to local time
}

This is the basic code with WiFi and printing the time once a second.
Get YOUR location from the weblink, and choose an NTP pool close to you.

#include <WiFi.h> // ESP32
unsigned long prevTime;
time_t now; // epoch
tm tm; // time struct

void setup() {
  Serial.begin(115200);
  WiFi.begin("SSID", "PW");
  configTzTime("NZST-12NZDT,M9.5.0,M4.1.0/3", "nz.pool.ntp.org"); // https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv
}

void loop() {
  time(&now);
  localtime_r(&now, &tm);
  if (now != prevTime) { // if time has changed
    prevTime = now; // remember
    printf("\n%02u:%02u:%02u", tm.tm_hour, tm.tm_min, tm.tm_sec);
  }
}

I guess you could send "now" (unsigned long) periodically (every hour?) to the next ESP, and fill the time struct with localtime there.
Leo..

Read this (click).

1 Like

Thanks a lot for you swift answers. Perhaps I wasn't clear enought:

B-ESP32 has no WiFi conection, WiFi drivers are not included and it is normally in a environemnt without WiFi reception (off course not in the moment during the development).
If the B-EPS32 switches to UTC -- assuming the actual timezone is UTC +/- 12 -- I ask myself, what is reason to switch the time-zone without having info about actual timezone.
@ sonofyc, of course I would like to understand the rtc function deeper, what is the source to read about?

I need only a non-precise timing function for the remaining function in the B-ESP32.
THe function is: When both ESP32 powered up, both ESP32 initializes theier LoRa modules, than A-ESP32 start the WiFi, reads the internet time and set it's own RTC. After this function it calls the B-ESP32 via LoRa and transmitts the actual Hour and Minute to the B-ESP32 - this is all in the setup part. After this function the B-ESP32 must use its own RTC to continue.
The loop function is only designed to interchange via LoRa some 10 digit numbers which triggers subroutines in both ESP32.
Hope this explains it better.
Best regards
Willythecat

I think you don't understand that ESP32 boards keep perfect time between occasional NTP updates. I don't see the need for any RTC module.
Leo..

WEll, if the ESP32 has no power, my assumption is than it will start again from scratch, there is no "preferences" calls and saving as well. The B-ESP32 has no NTP connection.

And tell me, that I have no understanding is not very useful I know that I'm lacking in understadning, otherwise i wouldn't have posted here. But you can enlighten me to give me at least point to the source of knowledge.

And tell me, that you don't see the need of an RTC is also quite "ambigious". The entire code is about 2.500 lines (I extracted only the parts which are using "rtc." calls.; both ESP23 work autonomously and I need a timing function in B-ESP32. BTW. I have the same combination with a kind-of D-LAN adapters and it worked always fine.

Of course, esp32 doesn't have any battery backed up RTC.

after B-ESP32 has received the current time fro A-ESP32 via LoRa you would set the internal clock of the B-ESP32 and you will have an updated time on the B-ESP32 during runtime.

I would transmit the UTC from A to B, let B run in UTC and use the timezone with the build in functions of time.h

I just discovered the root cause, but this raised even more questions! I'm now completely confused.
I read also temperatures from two DS18B20 and read also the number of devices found into the variable "noDev".

#include <DallasTemperature.h>
#include <OneWire.h>
#include <Arduino.h>
#include <ESP32Time.h>

#define pin_temp_sensor 25        // ext. devices

ESP32Time rtc;
OneWire oneWire(pin_temp_sensor);
DallasTemperature sensors(&oneWire);

 // _________rtc set  variables
int rtc_rec_min = 0;
int rtc_rec_hr = 0;
int LKH_hr;
int noDev;                                                                // Number of temperature devices found
...

void setup()
{
Serial.begin(9600);
rtc_rec_hr = (id_trans_t - id_trans*10000) / 100;                             // isolation Hour
rtc_rec_min = (id_trans_t - id_trans*10000) - (rtc_rec_hr *100);  // isolation minute
rtc.setTime(00, (rtc_rec_min), (rtc_rec_hr), 00, 00, 00);               // setting the RTC
}

void loop()
{
Serial.print ("Time :" ); Serial.print(rtc.getHour(true)); Serial.println (rtc.getMinute());
  sensors.begin();
  noDev = (sensors.getDS18Count()-1);                  // initial definition of no of external sensors


 if ((rtc.getHour(true)) == LKH_hr) 
{
do something
}

NOW, in the past I read with the statement

  noDev = (sensors.getDS18Count()-1);                 

due to some internal calculations I included this "-1".
When the -1 is there, the time is correct, when I remove the -1 the time is offest by 11 hrs.
What has this variable to do with the RTC? I'm completely confused now.
The RTC has a number of GPIO pins, including the Pin 25, but I have no clue, why this pin is now making trouble.
The Pin 25 is just a Deep-Wake-Up pin and I guess it must be defined earlier, which I did not.

And I discovered it's only depended on the variable "noDev" . If I keep -1 and do nothing, the time is ok.
If I add 1 later

noDev = noDev +1;

the error is again there, means, the time is again false.

I changed the variable name without success; it's still the same effect.

Even I used a helping variable, it's creating the error:

  no_Dev = (sensors.getDS18Count()-1); // initial definition of no of external sensors
  no_ExtDev = no_Dev;
  if (no_ExtDev == 2)

is ok, but

  no_Dev = (sensors.getDS18Count()-1); // initial definition of no of external sensors
  no_ExtDev = no_Dev +1;
  if (no_ExtDev == 2)

is creating agin the offset. I'm helpess here.

I also found: The RTC time changes, after the first read of the no_Devices. initially the time is ok. This was the delay of the 6 seconds I wrote in my first post; because I delayed the reading for 6 seconds.

 no_Dev = (sensors.getDS18Count()); // initial definition of no of external sensors

Yes, but getting internet time takes about 4 seconds on a good internet connection.
Is that a problem?

I understood that. That's why I suggested to send the UTC variable "now" to the second one. Either when NTP is updated on ESP-A (defaults to 1 hour intervals) or when ESP-B request it.

Splitting it into local time, hours, minutes, seconds, etc is already part of the ESP core. Can be done locally on ESP-A and ESP-B. No need to use a library for that.

The ESP core also has a callback option, to let you know when NTP has been updated. So you can send that UTC variable to the second one at the right moment, straight after an update.
Leo..

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.