NTPClient now working with static asynwebpage made using ESPAsyncTCP

Hello,
I am currently working on a project which requires me to pass a time using webpage to NodeMCU and perform a task when the set time is reached. also NodeMCU is hosted on a static ip using WiFi.config(staticIP, gateway, subnet); due to this ntpClient is not sending entire data it send seconds accurately but hour and minute are always 0.
I have tried it without static ip and it works fine but for some reason is NTPClient breaks When server is on static ip
Her is the sample Code

#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <NTPClient.h>
#include <WiFiUdp.h>

const char* ssid = "**";
const char* password = "***";

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org"); 

// Set the static IP address
IPAddress staticIP(192, *, *, *);
IPAddress gateway(192, *, *, *);
IPAddress subnet(255, *, *, *);

const char* PARAM_INPUT = "buttonID";
const char* PARAM_HOUR = "hour";  
const char* PARAM_MIN = "minute";

int Ihour = 0,Imin = 0;

AsyncWebServer server(80);

void setup() {
  Serial.begin(115200);

  // Set the static IP configuration
  WiFi.config(staticIP, gateway, subnet);

  timeClient.begin();

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }

  Serial.println(WiFi.localIP());

  // Handle requests for the root path
  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
    // Your code here if needed for the root path

    // Set CORS headers
    request->send(200, "text/plain", "Hello from NodeMCU!");
  });

  // Handle requests for /index.html
  server.on("/index.html", HTTP_GET, [](AsyncWebServerRequest *request) {
    // Your code here if needed for index.html

    // Set CORS headers
    request->send(200, "text/plain", "Hello from index.html!");
  });

  // Handle the time request
  server.on("/time", HTTP_GET, [](AsyncWebServerRequest *request) {
    String Shour;
    String Smin;

    if (request->hasParam(PARAM_HOUR) && request->hasParam(PARAM_MIN)) {
      Shour = request->getParam(PARAM_HOUR)->value();
      Smin = request->getParam(PARAM_MIN)->value();

      // Process the time message as needed
      Ihour = Shour.toInt();
      Imin = Smin.toInt();

      Serial.print("hour - ");
      Serial.println(Ihour);
      Serial.print("minute - ");
      Serial.println(Imin);
      
      // Send a response
      request->send(200, "text/plain", "Time received successfully");
    } else {
//      timeMessage = "No time sent";
      request->send(400, "text/plain", "Bad Request");
    }
  });
  
  server.begin();
}

void loop() {
  timeClient.update();
  Serial.println(timeClient.getHours());
  Serial.println(Ihour);
  Serial.println(timeClient.getMinutes());
  Serial.println(Imin);
  delay(500);
  if (!T1TimerStarted && timeClient.getHours() == Ihour && timeClient.getMinutes() == Imin && timeClient.getSeconds() < 5) {
    //Work to perform
  }


}

Also Static Ip is necessary

remove the external NTP library.

NTP on the ESP comes with the ESP Core - no need for an external NTP library.

See my page:
https://werner.rothschopf.net/202011_arduino_esp8266_ntp_en.htm

Can you give a brief about its working
i don't understand what to do how do i integrate this to my code?

I suggest you read the article I have provided.
Have you tried the example?
Does it work? If not - describe what's happening on your side if you need help.
Have you adopted it to your time zone?
Does it still work? If not - describe what's happening on your side if you need help.

so i ran the exact code provided in article in MC, It ran fine what i want to know is how do i integrate it to my project do i just put all the code into my project (removing unwanted things like year month and week) or anything else?
Or should i only add the snippets mention below entire code
Also thank you for the help

start following

remove the old libraries

delete/comment lines which will not compile any more.
--> clean up your code.

if you have a NTP less code that compiles,
Follow the description.
add the time.h
make the two defines for the NTP pool and your time zone
add the configTime in setup
add the tm structure
and finally use the tm.tm_hour and tm.tm_min where needed.

here is the code that i prepared following your instruction
CODE

#ifndef STASSID   
#define STASSID "**"                            
#define STAPSK  "***"                      
#endif

#define MY_NTP_SERVER "pool.ntp.org"           //how do i select my NTP pool
#define MY_TZ "IST-5:30"   

#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <ESP8266WiFi.h>            
#include <time.h>  

IPAddress staticIP(192, 168, 1, 20);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);

const char* PARAM_INPUT = "buttonID";
const char* PARAM_HOUR = "hour";  
const char* PARAM_MIN = "minute";

int Ihour = 0,Imin = 0;

AsyncWebServer server(80);

time_t now;                         
tm tm;

void setup() {
  Serial.begin(115200);
  Serial.println("\nNTP TZ DST - bare minimum");

  configTime(MY_TZ, MY_NTP_SERVER); 

  WiFi.config(staticIP, gateway, subnet);

  WiFi.persistent(false);
  WiFi.mode(WIFI_STA);
  WiFi.begin(STASSID, STAPSK);
  while (WiFi.status() != WL_CONNECTED) {
    delay(200);
    Serial.print ( "." );
  }
  
  Serial.println(WiFi.localIP());
  
  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {

    request->send(200, "text/plain", "Hello from NodeMCU!");
  });

  server.on("/index.html", HTTP_GET, [](AsyncWebServerRequest *request) {

    request->send(200, "text/plain", "Hello from index.html!");
  });

  server.on("/time", HTTP_GET, [](AsyncWebServerRequest *request) {
    String Shour;
    String Smin;

    if (request->hasParam(PARAM_HOUR) && request->hasParam(PARAM_MIN)) {
      Shour = request->getParam(PARAM_HOUR)->value();
      Smin = request->getParam(PARAM_MIN)->value();

      Ihour = Shour.toInt();
      Imin = Smin.toInt();

      Serial.print("hour - ");
      Serial.println(Ihour);
      Serial.print("minute - ");
      Serial.println(Imin);

      request->send(200, "text/plain", "Time received successfully");
    } else {

      request->send(400, "text/plain", "Bad Request");
    }
  });

  DefaultHeaders::Instance().addHeader("Access-Control-Allow-Origin", "*");
  
  server.begin();
  
}

void loop() {
  time(&now);                       
  localtime_r(&now, &tm);           
  Serial.print("\thour:");
  Serial.print(tm.tm_hour);         
  Serial.print("\tmin:");
  Serial.print(tm.tm_min);          
  Serial.print("\tsec:");
  Serial.print(tm.tm_sec);                  
  Serial.println();
  delay(1000); 
}

but the serial output is as follows

09:46:29.998 -> 	hour:5	min:30	sec:4
09:46:31.028 -> 	hour:5	min:30	sec:5
09:46:32.006 -> 	hour:5	min:30	sec:6
09:46:32.988 -> 	hour:5	min:30	sec:7
09:46:34.019 -> 	hour:5	min:30	sec:8

this code is able to respond to client request but the time not syncing correctly
but after removing async library it works fine
what should i do?

let it run for a minute. then the time sync will occur.

By default, the first NTP request begins after 60 seconds. That can be changed. The original IDE example shows how to implement the function sntp_startup_delay_MS_rfc_not_less_than_60000.

Even after an hour it is still not working

#ifndef STASSID
#define STASSID "**"                            
#define STAPSK  "***"                      
#endif

#define MY_NTP_SERVER "pool.ntp.org"           
#define MY_TZ "IST-5:30"   

#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <ESP8266WiFi.h>            
#include <time.h>  
//
//IPAddress staticIP(192, 168, *, *);
//IPAddress gateway(192, 168, *, *);
//IPAddress subnet(255, 255, *, *);

const char* PARAM_INPUT = "buttonID";
const char* PARAM_HOUR = "hour";  
const char* PARAM_MIN = "minute";

int Ihour = 0,Imin = 0;

AsyncWebServer server(80);

time_t now;                         
tm tm;

void setup() {
  Serial.begin(115200);
  Serial.println("\nNTP TZ DST - bare minimum");

  configTime(MY_TZ, MY_NTP_SERVER); 

//  WiFi.config(staticIP, gateway, subnet);

  WiFi.persistent(false);
  WiFi.mode(WIFI_STA);
  WiFi.begin(STASSID, STAPSK);
  while (WiFi.status() != WL_CONNECTED) {
    delay(200);
    Serial.print ( "." );
  }
  
  Serial.println(WiFi.localIP());
  
  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {

    request->send(200, "text/plain", "Hello from NodeMCU!");
  });

  server.on("/index.html", HTTP_GET, [](AsyncWebServerRequest *request) {

    request->send(200, "text/plain", "Hello from index.html!");
  });

  server.on("/time", HTTP_GET, [](AsyncWebServerRequest *request) {
    String Shour;
    String Smin;

    if (request->hasParam(PARAM_HOUR) && request->hasParam(PARAM_MIN)) {
      Shour = request->getParam(PARAM_HOUR)->value();
      Smin = request->getParam(PARAM_MIN)->value();

      Ihour = Shour.toInt();
      Imin = Smin.toInt();

      Serial.print("hour - ");
      Serial.println(Ihour);
      Serial.print("minute - ");
      Serial.println(Imin);

      request->send(200, "text/plain", "Time received successfully");
    } else {

      request->send(400, "text/plain", "Bad Request");
    }
  });

  DefaultHeaders::Instance().addHeader("Access-Control-Allow-Origin", "*");
  
  server.begin();
  
}

void loop() {
  time(&now);                       
  localtime_r(&now, &tm);           
  Serial.print("\thour:");
  Serial.print(tm.tm_hour);         
  Serial.print("\tmin:");
  Serial.print(tm.tm_min);          
  Serial.print("\tsec:");
  Serial.print(tm.tm_sec);                  
  Serial.println();
  delay(1000); 
}

above code is working fine so I think it is happening due to static ip how do i get around it?

thats not correct:

see here:

I just tried your static ip code with:

  // WiFi.config(ip, dns, gateway, subnet);
  WiFi.config(staticIP, gateway, gateway, subnet);

and it got ntp time after a few seconds:

12:22:54.038 -> ................172.18.67.20
12:22:57.805 -> 	hour:5	min:30	sec:3
12:22:58.807 -> 	hour:16	min:52	sec:57
12:22:59.811 -> 	hour:16	min:52	sec:58
12:23:00.777 -> 	hour:16	min:52	sec:59

if your router doesn't provide a DNS, set a new ip / DNS

Thank you so much it worked finally, I was working on it for couple of days

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