Guys i am currently working on project which requires to host esp32 on static ip and it needs to perform task when set time is reached to get time i am using ntpclient "pool.ntp.org" but it dose not return correct time
#include <Wire.h>
#include <WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
const char* ssid = "ssid";
const char* password = "password";
IPAddress staticIP(192, 168, x, x);
IPAddress gateway(192, 168, x, x);
IPAddress subnet(255, 255, x, x);
AsyncWebServer server(80);
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP,"pool.ntp.org");
int t1start;
int t2start;
int t3start;
int t4start;
int t1duration = 2; // 10s
int t2duration = 10;
int t3duration = 10; // 10s
int receivedHour;
int receivedMinute;
bool executed1 = false;
bool executed2 = false;
bool executed3 = false;
bool executed4 = false;
bool executed5 = false;
void setup() {
Serial.begin(115200);
delay(3000);
WiFi.config(staticIP, gateway, subnet, subnet);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println(WiFi.localIP());
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "text/plain", "Hello from ESP32!");
});
server.on("/index.html", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "text/plain", "Hello from index.html!");
});
server.on("/time", HTTP_GET, [](AsyncWebServerRequest *request) {
if (request->hasParam("hour") && request->hasParam("minute")) {
String hour = request->getParam("hour")->value();
String minute = request->getParam("minute")->value();
// Convert hour and minute to integers if needed
receivedHour = hour.toInt();
receivedMinute = minute.toInt();
// Process received hour and minute here
Serial.print("Received Hour: ");
Serial.println(receivedHour);
Serial.print("Received Minute: ");
Serial.println(receivedMinute);
request->send(200, "text/plain", "Time received successfully");
} else {
request->send(400, "text/plain", "Invalid request");
}
});
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Origin", "*");
server.begin();
timeClient.begin();
timeClient.setTimeOffset(19800); // Indian Standard Time (GMT +5:30)
}
int convert(int hour, int min, int sec)
{
return hour * 3600 + min * 60 + sec; // Calculate total seconds
}
void loop() {
timeClient.update();
delay(200);
int currentHour = timeClient.getHours();
int currentMinute = timeClient.getMinutes();
int currentSecond = timeClient.getSeconds();
Serial.print(currentHour); // Print current hour
Serial.print(":"); // Separator
Serial.println(currentMinute); // Print current minute
int currentTime = convert(currentHour, currentMinute, currentSecond);
Serial.println(currentTime); // Print current time in seconds
if (currentHour == receivedHour && currentMinute == receivedMinute && currentMinute <= 5 && !executed1) {
startT1();
startR1();
t1start = currentTime;
executed1 = true;
}
if ((currentTime - t1start >= t1duration) && executed1 && !executed2) {
stopT1();
stopR1();
startT2();
t2start = currentTime;
executed2 = true;
}
}
I raised similar topic for esp8266 here is the link for that topic