Chat freely about anything...

User avatar
By Sirquil
#75131 Found this reference on setting TZ: http://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html

Here are some example TZ values, including the appropriate Daylight Saving Time and its dates of applicability. In North American Eastern Standard Time (EST) and Eastern Daylight Time (EDT), the normal offset from UTC is 5 hours; since this is west of the prime meridian, the sign is positive. Summer time begins on March’s second Sunday at 2:00am, and ends on November’s first Sunday at 2:00am.

EST+5EDT,M3.2.0/2,M11.1.0/2

Any examples of using tm_wday? I do not understand Ansi C; I am self taught on C++ for the Arduino. Structures are above my current level of C++ knowledge.

William
User avatar
By schufti
#75133 Hi,

I use it like this:
Code: Select all  time_t tnow;
  struct tm *ti;

  tnow = time(nullptr) + 1;
  ti = localtime(&tnow);
  iWoTag = ti->tm_wday;
  iJahr = ti->tm_year + 1900;
  iMonat = ti->tm_mon + 1;
  iTag = ti->tm_mday ;
  iStunde  = ti->tm_hour;
  iMinute  = ti->tm_min;
  iSekunde = ti->tm_sec;


I usually add 1 second to be ahead rather then late since the time one gets has allready passed.
Be aware that month starts with zero (thus +1), sunday=0.
User avatar
By Sirquil
#75146 Thank you; it is working now!

Code: Select all/////////////////////////////////////////////
//
//
//    ESP8266_Fast_Time.ino
//       from example code provided by shufti
//
////////////////////////////////////////////


#include <ESP8266WiFi.h>
#include <sys/time.h>                   // struct timeval
#include <time.h>                       // time() ctime()         1)

const char* ssid     = "SSID";
const char* pass = "PassPhase";

// #define RTC_TEST 1510592825 // = Monday 13 November 2017 17:07:05 UTC
#define RTC_TEST 1499893466 // = Wed 12 July 2017 21:04:26 UTC

int lc = 0;
timeval tv = { RTC_TEST, 0 };



void setup() {
  Serial.begin(115200);
  configTime(0, 0, "time.nist.gov");      // 2)
  settimeofday(&tv, nullptr);
 
/*

   Found this reference on setting TZ: http://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html

   Here are some example TZ values, including the appropriate Daylight Saving Time and its dates of applicability. In North American Eastern Standard Time (EST) and Eastern Daylight Time (EDT), the normal offset from UTC is 5 hours; since this is west of the prime meridian, the sign is positive. Summer time begins on March’s second Sunday at 2:00am, and ends on November’s first Sunday at 2:00am.

   EST+5EDT,M3.2.0/2,M11.1.0/2

*/
 
  setenv("TZ", "EST+5EDT,M3.2.0/2,M11.1.0/2", 3);   // this sets TZ to Brussels/Paris/Vienna     // 2)
  tzset();     // 2)
   Serial.println();
   Serial.print("MAC: ");
   Serial.println(WiFi.macAddress());
   
   // We start by connecting to a WiFi network
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, pass);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
 
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  // don't wait, observe time changing when ntp timestamp is received
}

void loop() {
 
  char strftime_buf[64];
  time_t tnow = time(nullptr);
  //  formated localtime
  strftime(strftime_buf, sizeof(strftime_buf), "%c", localtime(&tnow));
  Serial.println(strftime_buf);
 
  struct tm *ti;

  tnow = time(nullptr) + 1;
  ti = localtime(&tnow);
  int  DOW = ti->tm_wday;
  int  YEAR = ti->tm_year + 1900;
  int  MONTH = ti->tm_mon + 1;
  int  DATE = ti->tm_mday;
  int  HOUR  = ti->tm_hour;
  int  MINUTE  = ti->tm_min;
  int  SECOND = ti->tm_sec;
 
  Serial.println(DOW);
 
  Serial.println(HOUR);
  Serial.println(MINUTE);
  Serial.println(SECOND);


  lc++;
  delay(1000);
}



http://tinyurl.com/Observations-weather Project web site.


William
Last edited by Sirquil on Sun Apr 08, 2018 7:54 am, edited 1 time in total.
User avatar
By Sirquil
#75211 This code works; however, it does not produce expected outcome in my project:
Code: Select all//////////////////////////////////////////////////////////////////
//
//           Fast_Time.ino  v2  04/08/2018  by Siruil --of ESP8266.com
//                from eamples posted by schufti  --of ESP8266.com
//
///////////////////////////////////////////////////////////////////


#include <ESP8266WiFi.h>
#include <sys/time.h>                   // struct timeval
#include <time.h>                       // time() ctime()         1)

#define ssid "ssid"
#define wpwd "passphase"

// #define RTC_TEST 1510592825 // = Monday 13 November 2017 17:07:05 UTC
#define RTC_TEST 1499893466 // = Wed 12 July 2017 21:04:26 UTC

int DOW, MONTH, DATE, YEAR, HOUR, MINUTE, SECOND;

//int lc = 0;
timeval tv = { RTC_TEST, 0 };

time_t tnow = time(nullptr);

void setup() {
  Serial.begin(115200);
  configTime(0, 0, "time.nist.gov");      // 2)
  settimeofday(&tv, nullptr);
  setenv("TZ", "EST+5EDT,M3.2.0/2,M11.1.0/2", 3);   // this sets TZ to Brussels/Paris/Vienna     // 2)
 
  /*

   Found this reference on setting TZ: http://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html

   Here are some example TZ values, including the appropriate Daylight Saving Time and its dates of applicability. In North American Eastern Standard Time (EST) and Eastern Daylight Time (EDT), the normal offset from UTC is 5 hours; since this is west of the prime meridian, the sign is positive. Summer time begins on March’s second Sunday at 2:00am, and ends on November’s first Sunday at 2:00am.

   EST+5EDT,M3.2.0/2,M11.1.0/2

*/
  tzset();     // 2)
  WiFi.persistent(false);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, wpwd);

  // don't wait, observe time changing when ntp timestamp is received
}

void loop() {
 
  char strftime_buf[64];
  struct tm *ti;

  time_t tnow = time(nullptr);
 
  tnow = time(nullptr) + 1;
  //Serial.println(millis());
  // simple localtime
  //Serial.print(ctime(&tnow));
  //  formated localtime
  strftime(strftime_buf, sizeof(strftime_buf), "%c", localtime(&tnow));
  Serial.print(strftime_buf);
  Serial.println("  void loop");
  //  formated gmtime
  strftime(strftime_buf, sizeof(strftime_buf), "%c", gmtime(&tnow));
  //Serial.print(strftime_buf);
 
  delay(10000);

  getDateTime();
 
   delay(1000);
}

void getDateTime()
{
 
  char strftime_buf[64];
  struct tm *ti;

  time_t tnow = time(nullptr);
 
  tnow = time(nullptr) + 1;
  //Serial.println(millis());
  // simple localtime
  //Serial.print(ctime(&tnow));
  //  formated localtime
  strftime(strftime_buf, sizeof(strftime_buf), "%c", localtime(&tnow));
  Serial.print(strftime_buf);
  Serial.println("  void getDateTime");
  //  formated gmtime
  strftime(strftime_buf, sizeof(strftime_buf), "%c", gmtime(&tnow));
  //Serial.print(strftime_buf);
  Serial.println("");

  tnow = time(nullptr) + 1;
  ti = localtime(&tnow);
  DOW = ti->tm_wday;
  YEAR = ti->tm_year + 1900;
  MONTH = ti->tm_mon + 1;
  DATE = ti->tm_mday;
  HOUR  = ti->tm_hour;
  MINUTE  = ti->tm_min;
  SECOND = ti->tm_sec;
 
  Serial.println("");
  Serial.print(DOW);
  Serial.println("   Day of week");
 
  Serial.print(HOUR);
  Serial.println("  Hours");
  Serial.print(MINUTE);
  Serial.println("  Minutes");
  Serial.print(SECOND);
  Serial.println("  Seconds");
  Serial.println("");

                                                                       
}


This is my void loop for my project:
Code: Select allvoid loop()
{

     //error = 0;
   
     getDateTime();

     Serial.println(strftime_buf);

     if(started == 1)
     {

          // Open a "log.txt" for appended writing
          File log = SPIFFS.open("/SERVER.TXT", "a");



          if (!log)
          {
               Serial.println("file open failed");
          }

          log.print("Started Server:  ");
          log.println(lastUpdate) + " EST";
          log.close();

     }

     started = 0;   //only time started = 1 is when Server starts in setup

     //Serial.println("Returned to loop");

     wdt_reset();

     //Serial.println(dayofWeek);  //Check to see which dayofWeek starts is Saturday.  Saturday is 6 dayofWeek on DS3231.

     //Collect  "LOG.TXT" Data for one day; do it early (before 00:00:00) so day of week still equals 6.
     if (((HOUR) == 23 )  &&
               ((MINUTE) == 57) &&
               ((SECOND) == 0))
     {
          newDay();
     }

     //Write Data at 15 minute interval

     if ((((MINUTE) == 0)||
               ((MINUTE) == 15)||
               ((MINUTE) == 30)||
               ((MINUTE) == 45))
               && ((SECOND) == 0))
     {

      webInterface();
      logtoSD();   //Output to SPIFFS  --Log to SPIFFS on 15 minute interval.
      delay(1);  //Be sure there is enough SPIFFS write time
      speak();
           
     }
     else
     {
          delay(500);

          // Disable listen function prior to writing data to log file
          if (!((((MINUTE) == 14)||
                    ((MINUTE) == 29)||
                    ((MINUTE) == 44)||
                    ((MINUTE) == 59))
                    && ((SECOND) > 50)))
          {
               listen();
          }
     }
}


getDateTime function does not give current date and time.

Attaching "Fast_Time_Web_Interface.ino." --complete working sketch; except for time related routines.

http://tinyurl.com/Observations-weather Project web page

William
Attachments
Open-Source project files.
(12.95 KiB) Downloaded 241 times