How can update ds1307 date whit NTP in esp32?

hello
i want update my ds1307 date whit ntp in esp32 automatically 2 times in day
just esp32 connect to the NTP and update date by it self

Hi,
If you access NTP 2 times a day, why use a DS1307?

This task is composed from two:

  • get time from NTP
  • update time in DS1307

The first you can find in the ESP32 Wifi examples. The second, as I think, should be in the DS1307 manual or library.

All you have to do is put them together

i use ds1307 for alarm(for example turn on GPIO3 when time is 20:45)
But No matter how hard I tried, I could not managed to run the alarm program with the NTP server.
Because the time values in the NTP server cannot be used in if commands.
And sometimes the connection between the modem and the ESP32 module is interrupted, which causes ESP32 to fail(stuck on this error: connecting to Wifi) and ESP32 cannot run other programs.

At first, I did the same thing, but because the value was an integer and the value of the NTP server was a string, I could not make both the same, and I tried to convert the values in any way, but every time I encountered a new error.

This is not true. Time from the NTP can also be represented in the form of hours, minutes and seconds, which are easy to use in if conditions. See the library

1 Like

If the OP was to use the library #include <ESP32Time.h> ESP32Time - Arduino Reference the OP may find that using NTP and just setting the internal time clock will do well.

See the ESP32Time examples. Good luck.

Hi,
How about posting your code here so we can see it and better understand your difficulty.
To publish the code, remember to use the appropriate </> tags.

Thank you, I did not know about the existence of such a library.
But there is a problem
Sometimes the connection of the ESP32 board to the modem is interrupted
But ESP32 continues to work in the absence of Wi-Fi, but sometimes it gets stuck in the Wi-Fi connection error (while it was previously connected to Wi-Fi and received the time from NTP) and is no longer able to run other programs, Not until it reconnects to the modem.

How can I solve this problem of ESP32 getting stuck on the error (connecting to Wi-Fi)? It skips updating the time until it is not connected to Wi-Fi (or uses the clock it had previously updated) and When it is connected to Wi-Fi again to update the time

Hello, I also use the temperature sensor
Thanks

#include <NTPClient.h>
#include <WiFiUdp.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h> 
#define DS18B20PIN 32
#include <WiFi.h>

int byte_idx=0;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP,"pool.ntp.org", 12600, 60000);
const int hour = 06;
const int minute = 59;
OneWire oneWire(DS18B20PIN);

DallasTemperature sensor(&oneWire);


#include <LiquidCrystal.h>
int rs=22;
int en=4;
int d4=5;
int d5=18;
int d6=19;
int d7=21;
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);
#include <WiFi.h>
#include "time.h"

const char* ssid       = "GT500";
const char* password   = "liy8434G@)m*DC^s684684dB";
void setup() {
  lcd.begin(16,2);
   sensor.begin();
   sensor.setResolution(11);
 
  //connect to WiFi
  
lcd.printf("Connecting to %s ", ssid);
 WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      lcd.setCursor(0,1);
lcd.print(".");
  }
  lcd.setCursor(0,0);
lcd.print(" CONNECTED");

timeClient.begin();
  
WiFi.begin(ssid, password);
  
//disconnect WiFi as it's no longer needed
  WiFi.disconnect(true);
  WiFi.mode(WIFI_OFF);


pinMode(2,OUTPUT);

}

void loop() {
timeClient.update();
lcd.setCursor(0,1);
lcd.print(timeClient.getFormattedTime());


  sensor.requestTemperatures(); 
  float tempinC = sensor.getTempCByIndex(0);  
lcd.noCursor(); 
lcd.setCursor(byte_idx,1);  
  lcd.setCursor(9,1);
  lcd.print(tempinC);
lcd.setCursor(byte_idx,0);

}

Does your temp sensor work OK?

yes why?

So if the WiFi connection is not available what do you want the program to do?

void connectToWiFi()
{
  int TryCount = 0;
  while ( WiFi.status() != WL_CONNECTED )
  {
    TryCount++;
    WiFi.disconnect();
    WiFi.begin( SSID, PASSWORD );
    vTaskDelay( 4000 );
    if ( TryCount == 10 )
    {
      return; // if no WiFI in 10 tries, give up.
    }
  }
  WiFi.onEvent( WiFiEvent );
}

then all the code that uses WiFi should check for WiFi before using WiFI.

void loop() {
if (WiFi.status() == WL_CONNECTED)
{
timeClient.update(); // only call if WiFI is connected
}
lcd.setCursor(0,1);

this could be done

void loop() {
if (WiFi.status() == WL_CONNECTED)
{
timeClient.update(); // only call if WiFI is connected
} else {
    connectToWiFi();//try connect
}
lcd.setCursor(0,1);

I'm afraid this is a sidetrack and may not be relevant to your problem. You appear to have no time control in your loop, hence my question, but maybe this is handled by the Dallas Temperature library. I don't suppose that is a good idea, but apparently not damaging.

just stop update time by ntp and use own clock(Sometimes it does the same thing and just keeps counting the hours and doesn't connect to the NTP server anymore, but sometimes it doesn't keep counting and it shows the error connecting to Wi-Fi.)
Or somehow it won't do (or just jump from) Wi-Fi related commands until it reconnects

thank you
But I could not verify this code ('WiFiEvent' was not declared in this scope)
How can check the Wi-Fi connection condition with a single command only in the void loop?

Yes, I am using the Dallas Temperature library
I just don't understand what you mean about time control

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