Updating time with nodemcu and NTPClient

Greetings!

I am building an LED clock with a nodemcu via arduino uno. I've made it work, mostly. My problem is updating the timezone. It doesn't seem to matter what offset I use, the timezone refuses to change. I am UTC-5, but no matter what offset I change, the time remains at UTC+1. Any help would be super appreciated!

/*
    This sketch demonstrates how to scan WiFi networks.
    The API is almost the same as with the WiFi Shield library,
    the most obvious difference being the different file you need to include:
*/
#include "ESP8266WiFi.h"
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <time.h>
#include <ESP8266WebServer.h>

#define MAX_CHARS 30

const long utcOffsetInSeconds = -18000;

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "us.pool.ntp.org", -18000, 60000); //7200 seconds of utc offset = UTC+2 (this has to be changed according to summer/winter time)

ESP8266WebServer server(80);

void handleRoot();
void handleLed();
void handleTemp();
void handleNotFound();

char outputString[MAX_CHARS] = "";

time_t rawtime;
struct tm * ti;
uint8_t month;
uint8_t day;
uint8_t week_day;

bool blink = true;
uint8_t mode = 0;
uint8_t color_preset_f = 0xFF;
uint32_t raw_foreground = 0xFFFFFF;
uint8_t color_preset_b = 1;
uint32_t raw_background = 0;
uint8_t brightness = 255;

unsigned long milli_time;
unsigned long previous_trigger;
bool difference_minute;

bool config_posted = true;

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

  // Set WiFi to station mode and disconnect from an AP if it was previously connected
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(100);

  int n;
  do {
    n = WiFi.scanNetworks();
  } while(n < 1);

  for(int i = 0; i < n; i++) {
    if(WiFi.SSID(i) == "wifiid1") {
      WiFi.begin("wifiid1", "wifiid1pass");
      break;
    } 
  }

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

  timeClient.begin();

  server.on("/", HTTP_GET, handleRoot);
  server.on("/change", HTTP_POST, handlePost);
  server.onNotFound(handleNotFound);

  server.begin();

  previous_trigger = millis();
  milli_time = 0; //trigger instantly
}

void loop() {
  timeClient.update();
  server.handleClient();

  difference_minute = (millis() - previous_trigger) > milli_time;
  
  if(Serial.available()) {
    char inChar;
    while(inChar != '\n')
      if(Serial.available())
        inChar = (char)Serial.read();
    if(WiFi.status() != WL_CONNECTED) {
      while(WiFi.status() != WL_CONNECTED) {
        delay(50);
      }
    }
    if(difference_minute) {
      while(!timeClient.update()) ;

      previous_trigger = millis();
      milli_time = (60 - timeClient.getSeconds()) * 1000;
      difference_minute = false;
    }
  
    rawtime = timeClient.getEpochTime();
    ti = localtime (&rawtime);
  
    month = ti->tm_mon + 1;
    day = ti->tm_mday;
    week_day = timeClient.getDay();
  
    if((month == 3)  && ((day-week_day) >= 25) ||
       (month > 3)   && (month < 10) ||
       (month == 10) && ((day-week_day) <= 24))
      timeClient.setTimeOffset(7200);
    else
      timeClient.setTimeOffset(3600);

    if(strlen(outputString)!=0) {
      Serial.print(outputString);
      memset(outputString, '\0', strlen(outputString));
    }

    Serial.print("t");
    Serial.print(timeClient.getHours());
    Serial.print(" ");
    Serial.println(timeClient.getMinutes());
    config_posted = true;
  }
}....

Please delete. I figured it out. Also, I don't know how to delete forum posts. Thanks!

the esp8266 has NTP functions in SDK. you don't need the NTP library

1 Like