ESP8266 - Writing on SD has timestamp w/o timelib

Hi,
I came across a strange effect: If I connect my esp8266 to WiFi, next SD write operation gives an actual timestamp without any setTime operations in my code.
How does it come? I could not find any documentation about it.
Does this mean, that I have access to actual time information just by connecting to WiFi and without implementing any NTP stuff?

Uli


#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <SPI.h>
#include <SD.h>

char dataBlock[100];

// WiFi connection
const char* ssid = "SSID";
const char* password = "PASS";
void WiFiStart();

    
/////////////////////
// the setup routine
/////////////////////
void setup() 
{
  // start serial
  Serial.begin(115200);
  delay(1000);
  //Serial.printf("Size %d\n",sizeof(struct_alles));
  Serial.println("Timestamp - Test");
  Serial.println("Build: " __TIME__ "  " __DATE__ );
  Serial.println(__FILE__);
 
  Serial.print("Initializing SD-Card...");
  if(!SD.begin(16))
    Serial.println("ERROR !!");
  else
    Serial.println("OK.");
   
  write_FS("After_SD_Begin");
 
   // inital connect
  WiFi.mode(WIFI_STA);
  WiFi.hostname("Timestamp");
  WiFiStart();
  write_FS("After_WiFiStart");    
}

void write_FS(String FileName)
   {
   
   Serial.println();
 
   File mySDFile = SD.open(FileName,FILE_WRITE);
   if (!mySDFile) 
      Serial.println(F("ERROR opening file on SD-card for writing!"));
   else  
     {
      mySDFile.seek(0);
      mySDFile.write(dataBlock,sizeof(dataBlock));
      Serial.println(F("File written on SD-Card."));
      mySDFile.close();
     }    
   }

///////////////////
// (re-)start WiFi
///////////////////
void WiFiStart()
{

  // Connect to WiFi network
  Serial.printf("Connecting to %s\n",ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(500);
    Serial.print(".");
  }
  Serial.printf("\nWiFi connected\n");
  // Print the IP address
  Serial.println(WiFi.localIP());
}


void  loop()
{     
}
  

Network Time Protocol (NTP), the same way your PC keeps your system time synced to real-time.

I guess it's NTP, but I never read that this is implemented somewhere hidden in the WiFi library.
I had some projects with need for actual time and I always had to use WiFiUdp.h and include the NTP code.
Does WiFi update the system time like setting it with setTime() ?

I think it has to do with this line in SDFS.h:
sdfat::FsDateTime::setCallback(dateTimeCB);

FS.h and SDFS.h are included in SD.h

it seems that SDFS.h uses the ESP core function to get time.
Basically NTP is part of the ESP core already and for usual you don't need any self written UDP requests for NTP on the ESP.

Example: NTP for the ESP8266 including day light saving time (DST) without 3rd party library

Thanks for the explanation. I did not expect the platform to start external communication without explicit order to do so.

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