How to replace sketch with HTTP server with NTP

This (attached) is a sketch of a clock + temperature sensor DHT22 using an esp8266 that gets the data (Time and Date) from a local HTTP server but, I would like to use an NTP server (because I live in another country - Brazil) to get the data but, I don’t have enough knowledge for it, so could someone kindly, by kindness, instruct me what should I change in the sketch to do this?

The code was taken from here: ESP8266 IoT room thermometer - part 1 - Machina Speculatrix

Sorry, English is not my native language!

My early thanks,

Daniel

IoT_room_thermometer.ino (15.7 KB)

Daniel:

Adapting your ESP8266 program to use NTP time is almost trivial now that the ESP8266 core has been updated with some of the ESP32 code.

Here is a program I threw together for my own reference a few months ago. You will have to do some research to find out how to modify the setenv(…) function parameters for your time zone.

// Minimal code to implement NTP update of timekeeping on an ESP8266
// This code uses EspressIF functions introduced with the ESP32.  They work on 
//  recent versions of the ESP8266 core as well.
// Date and time information is output on the serial monitor.
// Time is derived from an NTP time server

// Program information
char programName[] = "NTP_Minimal_Updated.ino";
char versionNumber[] = "v07";
char programDate[] = "2019-05-04";
char programAuthor[] = "Donald Weiman";

// Version information
//  v01 - cobbled together from various example programs
//  v02 - clean up code and comments
//  v03 - implement settimeofday() callback (indicating NTP update) 
//  v04 - save time when NTP update occurred
//  v05 - move display of time out of loop()
//  v06 - specify interval between time displays (remove delay() from loop())
//  v07 - remove vestigial code from previous versions

// Required libraries
#include <ESP8266WiFi.h>
#include <time.h>
#include <coredecls.h>                  //   required for settimeofday_cb()

const char* ssid = "...";               // put your info here
const char* pass = "...";

// Timing parameters
int data_update_minutes = 1;            // measurement interval in minutes
time_t present_timestamp;
time_t previous_timestamp = 0;

// ....................................................................................  
void setup()
{
  // make sure the ESP8266 WiFi functions are enabled
  WiFi.mode(WIFI_STA);                  // use only the WiFi 'station' mode
  
  Serial.begin(115200);

  // display sign-on message - useful to determine exactly which version is running
  Serial.println();
  Serial.println();   
  Serial.print(programName);
  Serial.print(" ");
  Serial.print(versionNumber);
  Serial.print("   ");
  Serial.print(programDate);
  Serial.print("   ");
  Serial.print(programAuthor); 
  Serial.println(); 

  // minimal code to log onto a local WiFI network
  WiFi.begin(ssid, pass);               // send credentials
  while (WiFi.status() != WL_CONNECTED) // wait for connection
  {
    delay(500);
    Serial.print(".");
  } 
  
  // implement NTP update of timekeeping (with automatic hourly updates)
  configTime(0, 0, "0.pool.ntp.org");

  // info to convert UNIX time to local time (including automatic DST update)
  setenv("TZ", "EST+5EDT,M3.2.0/2:00:00,M11.1.0/2:00:00", 1); 
  
   // register a callback (execute whenever an NTP update has occurred)
  settimeofday_cb(time_is_set);
 
  Serial.println("\nSetup done");  
 
}

// ....................................................................................  
void loop()
{
  present_timestamp = time(nullptr);

  if (present_timestamp >= previous_timestamp + (60 * data_update_minutes)) 
  {
    previous_timestamp = present_timestamp;
    Serial.println();  
    displayDateAndTime(); 
  }    
    
}

// ....................................................................................  
void time_is_set (void)
// callback routine - arrive here whenever a successful NTP update has occurred
{
  struct tm *tmp ;                      // NOTE: structure tm is defined in time.h  
  char UPDATE_TIME[50];                 // buffer for use by strftime()
    
  // display time when NTP update occurred
  time_t tnow = time(nullptr);          // get UNIX timestamp 
  tmp = localtime(&tnow);               // convert to local time and break down
  strftime(UPDATE_TIME, sizeof(UPDATE_TIME), "%T", tmp);  // extract just the 'time' portion
    
  Serial.print("\n-------- NTP update at ");
  Serial.print(UPDATE_TIME);
  Serial.println(" --------");
}


// ....................................................................................
void displayDateAndTime()
{
  Serial.print(present_timestamp);      // display system time as UNIX timestamp
  
  // use ctime() to convert the system (UNIX) time to a local date and time in readable form
  // NOTE: 'ctime' produces a output in a specific format that looks 
  //        like --> Fri Mar 22 12:11:51 2019  -there is also a newline (\n) appended
  Serial.print("  ");
  Serial.print(ctime(&present_timestamp));  // convert timestamp and display
  
  struct tm *tmp ;                          // NOTE: structure tm is defined in time.h
  char FORMATTED_TIME[50];                  //   filled by strftime()
    
  // convert the system (UNIX) time to a local date and time in a configurable format 
  tmp = localtime(&present_timestamp);      // break down the timestamp     
      
  // use strftime() to display the date and time in some other formats
  // https://www.geeksforgeeks.org/strftime-function-in-c/ 

  // the following gives output in this form -->  03/22/19 - 12:11 pm EDT
    //   where %x --> writes localized date representation
    //         %I --> writes hour as a decimal number, 12 hour clock (range [01,12]) 
    //         %M --> writes minute as a decimal number (range [00,59]) 
    //         %P --> writes localized a.m. or p.m. (locale dependent)
    //         %Z --> time zone abbreviation name
  strftime(FORMATTED_TIME, sizeof(FORMATTED_TIME), "%x - %I:%M%P %Z", tmp);  
  Serial.println(FORMATTED_TIME);
 
  // the following gives output in this form -->  2019-03-22 12:11:51
    //         %F --> equivalent to "%Y-%m-%d" (the ISO 8601 date format)     
    //         %T --> equivalent to "%H:%M:%S" (the ISO 8601 time format) 
  strftime(FORMATTED_TIME, sizeof(FORMATTED_TIME), "%F %T", tmp);
  Serial.println(FORMATTED_TIME);
    
  Serial.println();
}

EDIT: Note that this program uses time.h not Time.h (lower case).

Don

Hi Don
Thanks for the sample program; I will try, although I have no concrete idea how to adapt this to my sketch! But thanks for the help

Dan