Chat freely about anything...

User avatar
By schufti
#75232 Hi,
I stripped your code to show only the relevant parts for the time functionality. Unfortunately I don't get your intention behind setting esp in APmode AND configuring softAPmode. Then you set the IP etc, later print it over serial as if it were variable and indistinct. But I cant find where you you log on to your WiFi to be able to reach the internet...
My modified example just logs on to your WiFi and the esp will be reachable under its given (hardcoded) IP.
If it was your intention to have the esp span its own WiFi to be reachable for everyone, it would need some more modification.

Code: Select all#include <sys/time.h>                   // struct timeval
#include <time.h>                       // time() ctime()       
#include <ESP8266WiFi.h>   //Part of ESP8266 Board Manager install

#define NTP0 "us.pool.ntp.org"
#define NTP1 "time.nist.gov"
/*

  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.
*/
#define TZ "EST+5EDT,M3.2.0/2,M11.1.0/2"


// Replace with your network details
const char* ssid = "yourSSID";
const char* password = "yourPASSPHRASE";

//setting the addresses
IPAddress staticIP(10, 0, 0, 174);
IPAddress gateway(10, 0, 0, 1);
IPAddress subnet(255, 255, 255, 0);

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

int lc = 0;
time_t tnow;
char strftime_buf[64];


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

  if (Serial.available())
  {

    Serial.println("Starting...");
    Serial.print("NTP_Observations.ino");
    Serial.print("\n");
  }
// set up TimeZone in local environment
  setenv("TZ", TZ, 3);
  tzset();

// Connecting to local WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.mode(WIFI_STA);                 
  WiFi.config(staticIP, gateway, subnet);
  WiFi.begin(ssid,password);
  delay(10);
  while (WiFi.status() != WL_CONNECTED)
  {
    Serial.print(".");
    delay(100);
  }
  configTime(0, 0, NTP0, NTP1);
  Serial.println("\n WiFi connected");
  Serial.print("MAC: ");
  Serial.println(WiFi.macAddress());
  Serial.print("Server IP:  ");
  Serial.println(WiFi.localIP());
  Serial.println("\n");
  Serial.print("wait for first valid timestamp ");

  while (time(nullptr) < 100000ul)
  {
    Serial.print(".");
    delay(10);
  }
  Serial.println(" time synced");
// set up SoftAP
  // WiFi.softAP("DATA","*logger#");
  // WiFi.softAPConfig(local_ip, gateway, subnet);
}

void loop()
{
  getDateTime();
  Serial.println(strftime_buf);

  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("\n");
  delay(1000);
}

void getDateTime()
{
  struct tm *ti;

  tnow = time(nullptr) + 1;
  strftime(strftime_buf, sizeof(strftime_buf), "%c", localtime(&tnow));
  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;
}
User avatar
By Sirquil
#75241
Weather.JPG
Weather web page servered up from RobotDyn WiFi D! R2 board with a BME280 sensor
I apologize for the mess on networking; late night/early morning cutting and pasting. I will be more careful. Outstanding presentation of this method of getting date, time, and time stamp! I captured a "Putty" log file of a session with "Fast_Time_Web_Interface.ino." Also, did a screen capture of the "Weather web page.

Attaching project code with revised time routines working. Thank you schufti!

Best regards,
William
Attachments
Revised project code 04/09/2018 @ 18:42
(11.36 KiB) Downloaded 227 times
Session showing dat, time stamping of my project
(1.78 KiB) Downloaded 558 times
User avatar
By schufti
#75256 Hi,
just some remarks:

°) in declaration of tnow initialization is missing:
time_t tnow=0;

°) in get Date Time() you convert time 2x, once to string (strftime_buf) that you use nowhere else
so you might want to remove
Code: Select allstrftime(strftime_buf, sizeof(strftime_buf), "%c", localtime(&tnow));
            and its declaration
char strftime_buf[64];

or, even more elegant, modify the format string to "%m/%d/%Y,%H:%M:%S"
(now just "%c", see here https://sourceware.org/newlib/libc.html#strftime for more info)
to produce the equivalent to your timeStamp() function.

°) before starting WiFi with WiFi.begin(); you should define the mode
WiFi.mode(WIFI_STA);
you might start an AP otherwise that you don't expect and might interfere with the rest of your sketch...

°) this
Code: Select all if ((((MINUTE) == 0)||
               ((MINUTE) == 15)||
               ((MINUTE) == 30)||
               ((MINUTE) == 45))
               && ((SECOND) == 0))
     {

can more elegantly be solved using "modulo" operation
Code: Select allif (MINUTE mod 15 == 0
               && SECOND == 0)
{

or even shorter
Code: Select allif (tnow mod 900 == 0) {


°) do you really need spi.h? I could not find any use...

°) if you use Arduino IDE, just hit <ctrl>+T once to get your code reformatted for easier reading

°) get rid of all those wdt_reset()
if that really raises problems, we will help to fix them proper.

I hope that doesn't look to critical but I allways think every little helps ...

best regards,
schufti
User avatar
By Sirquil
#75269 Hi schufti,

Thank you for all the remarks; I have implemented most, I need to review the web link on formatting strftime. I elected to use the longer form than to use "if(tnow mod 900 == 0)." I do appreciate the time and your help!

"Fast_Time_Web_Interface_v3.ino" has everything working; except the webInterface function. Appeared there was some "leakage" in Serial Monitor. Capturing html code in the Serial Monitor, shows "Author of website has deleted website." Will have to contact hosting service to see why I am getting this result echoed back from the website. For now I have commented out the webInterface function in "loop."

William
Attachments
Made the changes to my project, cleaned up the formatting,; attaching project code.
(11.41 KiB) Downloaded 211 times