(SOLVED) Question with the NTPClient.h library in esp8266

Hi, I would like to use an esp8266 to automate tasks at home, but my problem is that I don't quite understand the time format, according to what I can see on the library's wiki, the function timeClient.getFormattedTime(); returns the time in hh:mm:ss format, whereas the getEpochTime(); returns the seconds since January 1, 1970, but what I want to do is, for example, a relay turns on at 9:00 and turns off at 11:00

have a look a this which gets the epoch time and prints local time

/*
  Rui Santos
  Complete project details at https://RandomNerdTutorials.com/epoch-unix-time-esp32-arduino/
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files.
  
  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.
*/

#include <WiFi.h>
#include "time.h"

// Replace with your network credentials
const char* ssid = " xxxxx";
const char* password = "xxxxxxxxxx";

// NTP server to request epoch time
const char* ntpServer = "pool.ntp.org";

// Variable to save current epoch time
unsigned long epochTime; 

// Function that gets current epoch time
unsigned long getTime() {
  time_t now;
  struct tm timeinfo;
  if (!getLocalTime(&timeinfo)) {
    //Serial.println("Failed to obtain time");
    return(0);
  }
  time(&now);
  return now;
}

// Initialize WiFi
void initWiFi() {
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.print("Connecting to WiFi ..");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print('.');
    delay(1000);
  }
  Serial.println(WiFi.localIP());
}

void setup() {
  Serial.begin(115200);
  initWiFi();
  configTime(0, 0, ntpServer);
}

void loop() {
  epochTime = getTime();
  Serial.print("Epoch Time: ");
  Serial.println(epochTime);
   time_t curtime;
   struct tm *loc_time;
 
   //Getting current time of system
   curtime = time (NULL);
 
   // Converting current time to local time
   loc_time = localtime (&curtime);
   char text[100]={0};
    // Displaying date and time in standard format
   snprintf(text, 100, "%s", asctime (loc_time));
   Serial.print("time ");
   Serial.println(text);
  delay(1000);
}

serial monitor displays

Epoch Time: 1672653175
time Mon Jan  2 09:52:55 2023

Epoch Time: 1672653176
time Mon Jan  2 09:52:56 2023
int hour = (EpochTime % SecondsPerDay) / SecondsPerHour;
  if (hour >= 9 && hour < 11)
    digitalWrite(RelayPin, RelayOn);
  else
    digitalWrite(RelayPin, RelayOff);

ok, looks promising, i'll try it and comment

Well, I've tried it and in the terminal, for some reason it prints the last characters of the Wi-Fi password, I know, it's very LOL

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

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

int time_h;
int time_m;
int time_s;

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);

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

  WiFi.begin(ssid, password);

  while ( WiFi.status() != WL_CONNECTED ) {
    delay ( 500 );
    Serial.print ( "." );
  }

  timeClient.begin();
}

void loop() {
  timeClient.update();
  //Serial.println(timeClient.getFormattedTime());
  delay(1000);

  time_h = ((timeClient.getEpochTime() % 86400) / 3600);
  //time_m = (getEpochTime() % 86400) / 60;
  //time_s = (getEpochTime() % 86400) / 1;

  Serial.println("time_h: " + time_h);
  //Serial.println("time_m: " + time_m);
  //Serial.println("time_s: " + time_s);
}
/*
int hour = (EpochTime % SecondsPerDay) / SecondsPerHour;
  if (hour >= 9 && hour < 11)
    digitalWrite(RelayPin, RelayOn);
  else
    digitalWrite(RelayPin, RelayOff);
*/

The '+' trick only works on String objects. Try:

  Serial.print("time_h: ")
  Serial.println(time_h);

perfect, I've corrected it and it seems to work, I'm going to leave it for 24 hours and tomorrow I'll comment if everything worked well, this is the code for now:

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

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

long time_x;
int time_h;
int time_m;
int time_s;

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);

void setup(){
  Serial.begin(115200);
  WiFi.begin(ssid, password);

  while ( WiFi.status() != WL_CONNECTED ) {
    delay ( 500 );
    Serial.print ( "." );
  }
  timeClient.begin();
  pinMode(16, OUTPUT);
}

void loop() {
  timeClient.update();
  time_x = timeClient.getEpochTime();
  time_h = ((time_x % 86400) / 3600) +1;
  time_m = ((time_x % 3600) / 60);
  time_s = (time_x % 60);
   
  Serial.println("time_h: " + String(time_h));
  Serial.println("time_m: " + String(time_m));
  Serial.println("time_s: " + String(time_s));

  if (time_h >= 9 && time_h < 21) digitalWrite(16, true); else digitalWrite(16, false);
  delay(1000);
}

For those who read this in the future, I am using a WeMos D1 mini clone, which activates and deactivates a relay, LED, etc. at certain times. In case someone wants to use it in their project, here I leave the documentation, etc.

Libraries to use:
1.- ESP8266WIFI.h ==> integrated into the official espressif library from this url:
http://arduino.esp8266.com/stable/package_esp8266com_index.json

2.- NTPClient.h ==> installed from the library manager without the need for a url

3.- WiFiUdp.h ==> integrated into the espressif library of esp8266

why have you ignored the post of @horace ?

on a ESP8266 you don't need an external library to get SNTP. That's already in the core of the ESP8266.

See also
https://werner.rothschopf.net/202011_arduino_esp8266_ntp_en.htm

1 Like

Hello, @noiasca, I have not ignored it, it's just that I have found more information and current codes for that library, it also allows me to have compatibility with ethernet boards for other arduinos

It works perfectly, I have migrated it to an esp32 and great, thanks to all :slight_smile:

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