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
}
}
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
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.
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.