hello,
i am using an ESP8266 LOLIN 2x15pins module t read GPS data and write the data on SDcard
i used another sctech to initialise the Sdcard in FAT16
Erasiong/formatting OK , i put on it a litle txt file …
SDCARD coulde be read on a PC WIN10 ..OK
I get the GPS data ..OK
I write data GPS on SCRACR ..OK
But the date is corrupted .
[Quote]
.
Directory : D:\_ESP8266MOD_Lolin_2x15_Jaune\_ESP8266_Lolin_Jaune_NTP_GPS_AM2302_SDCARD_2025-06
MODULE : ESP8266_Jaune_LOLIN_NodeMCU_V3
BOARD : Lolin VMOS D1 R1
ESP8266 Lolin Jaune 2x15pins
_ESP8266_Lolin_Jaune_NTP_GPS_AM2302_SDCARD_2025-06.ino 12-06-2025
NTP Server: europe.pool.ntp.org
ESP getCoreVersion : 3.1.2
ESP getSdkVersion : 2.2.2-dev(38a443e)
BP1 input <–D2 (GPIO 4)
AM2302-DHT11 –>D1 (GPIO 5)
DHT11 Temp : 26.40 Humid : 59.60
Attente connection STAtion Wifi
…………
TU : 1749728111,jeudi, 12/06/2025 , 11:35:11,
mois=-10
jour=-159
heure=11
minute=35
Option 1
<LF>Initializing GPS
<LF>Initializing SD card…
Wiring is correct and a card is present.
Card type: SDHC
Cluster size: 32768
Blocks x Cluster: 64
Blocks size: 512
Total Blocks: 942
Total Cluster: 60342
Volume type is: FAT16
Volume size (Kb): 1977286
Volume size (Mb): 1930
Volume size (Gb): 1.88
Card size: 1977.29 Mb
Total bytes: 1977286656
Used bytes: 163840
System Volume Information/
IndexerVolumeGuid76CREATION: 2025-06-12 10:56:38LAST WRITE: 2025-06-12 10:56:40<LF>
test2025.txt 74 CREATION: 2098-01-01 00:00:22 LAST WRITE: 2098-01-01 00:00:22<LF>
gpslog.txt 2552 CREATION: 2098-01-01 00:00:30 LAST WRITE: 2025-06-12 11:32:12<LF>
SDcard2G.txt 59 CREATION: 2025-06-12 10:58:32 LAST WRITE: 2025-06-12 10:58:34<LF>
Writing to test.txt…done.
test2025.txt:
testing 1, 2, 3.
testing 1, 2, 3.
testing 1, 2, 3.
LAT: 45.824429,LON: 5.197163,ALT :192.90,DOP: 13.54,SAT: 4 *
LAT: 45.824428,LON: 5.197164,ALT :192.90,DOP: 13.54,SAT: 4 *
LAT: 45.824429,LON: 5.197160,ALT :192.90,DOP: 13.54,SAT: 4 *
[/Quote]
after, i can’t read anymore my SDCARD on my PC Computer
i think ,it is due to the bad Date
2098-01-01 00:00:22
even i get the correct date with NTP server …
but i don’t know which date is used by SDfat …
Start your code here
//_ESP8266_HW364A_Jaune #1
#include <Arduino.h>
#include <NTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
//#include <U8g2lib.h> // version 2.23.15 installed
#include <Wire.h>
#include <SD.h>
#include <SPI.h>
#include <SoftwareSerial.h>
//#include <Adafruit_GFX.h>
//#include <Adafruit_SSD1306.h>
#include <TinyGPS++.h>
#define option1
#include <DHTesp.h>
//#include <Adafruit_Sensor.h>
// adresse I2C = 0x78
// voir nodemcu_esp8266_pinout.jpg
//#define SCL 12 // D6 GPIO12
//#define SDA 14 // D5 GPIO14
// SD card test for esp8266
// SD card attached to SPI bus as follows:
// CS GPIO 15 …D8
// MOSI GPIO 13 ….D7
// SCK GPIO 14 ….D5
// MISO GPIO 12 ….D6
const int LedOnBoard = 2; //D4= GPIO2 bleue sur module
#define BP1 4 // D2 =GPIO4
#define DHTTYPE DHT22 // DHT type (DHT11, DHT22)
int DHTPIN = 5 ; // D1 =GPIO 5 <– Broche du capteur AM2302
#define VERSION “12-06-2025”
#define DIRECTORY “D:\\_ESP8266MOD_Lolin_2x15_Jaune\\_ESP8266_Lolin_Jaune_NTP_GPS_AM2302_SDCARD_2025-06”
#define MODULE “ESP8266_Jaune_LOLIN_NodeMCU_V3”
#define BOARD “Lolin VMOS D1 R1”
#define SDCARD “Sandisk SD 2Gb “
const char* ssid = “Freebox-5CF3C9”;
const char* password = “mxxxxxxxxxxxxxxxxxm”;
#include <time.h>
int i;
int j;
int k;
unsigned long epoch = 0;
int nujour = 0; //numero jour de la semaine avec 0 pour dimanche
String jour = “mon jour”; // dimanche, lundi, etc.
String heure = “mon heure ..”;
char buffer[80]; // Stockage de la date complete
float temperatureC ;
byte addr[8];
#define HIVER ‘1’
#define ETE ‘0’
int jd;
int m1;
int mn;
int hh;
int CH=HIVER;
int Once=1;
byte Etat_BP;
unsigned long lastTime = 0;
unsigned long timerDelay = 4500; //4sec
double lat ;
double lng ;
double alt ;
int sat ;
double dop;
IPAddress local_IP(192, 168, 1, 11);
IPAddress gateway(192, 168, 1, 254);
IPAddress subnet(255, 255, 255, 0);
IPAddress primaryDNS(212,27,40,241); //optional
IPAddress secondaryDNS(212,27,40,240); //optional
//Creation objet WIFI UDP
WiFiUDP ntpUDP;
//Creation objet client NTP avec les parametres suivants :
// – pool de serveurs NTP
// – en option le décalage horaire en secondes, ici 3600 pour GMT+1, pour GMT+8 mettre 28800, etc.
// – en option l intervalle de mise à jour en millisecondes par défaut à 60 secondes
NTPClient timeClient(ntpUDP, “europe.pool.ntp.org”, 7200, 60000); // hiver
//U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0,SCL,SDA, U8X8_PIN_NONE);
// GPS en SoftwareSerial
SoftwareSerial gpsSerial(0, 2); // RX, TX → GPS TX sur D3 = 18-GPIO0)
TinyGPSPlus gps;
char Tempchar [10];
char Humidchar[10];
float t = 0.0 ; // AM2302 temper
float h = 0.0 ; // AM2302 Humid
DHTesp dht; // AM2302 DHT12
File myFile;
File logFile;
void printDirectory(File dir, int numTabs)
{
struct tm * tmstruct;
while (true)
{
File entry = dir.openNextFile();
if (! entry) {
// no more files
break;
}
for (uint8_t i = 0; i < numTabs; i++)
{
Serial.print(‘\t’);
}
Serial.print(entry.name());
if (entry.isDirectory()) {
Serial.println(“/”);
printDirectory(entry, numTabs + 1);
}
else
{
// files have sizes, directories do not
Serial.print(“\t”);
Serial.print(entry.size(), DEC);
time_t cr = entry.getCreationTime();
time_t lw = entry.getLastWrite();
tmstruct = localtime(&cr);
Serial.printf(“\tCREATION: %d-%02d-%02d %02d:%02d:%02d”, (tmstruct->tm_year)+1900 , (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec);
tmstruct = localtime(&lw);
Serial.printf(“\tLAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n”, (tmstruct->tm_year)+1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec);
}
Serial.print(“\r\n”);
entry.close();
}
}
void Affiche_Date_Heure_NTP()
{
// Recup heure puis affichage
timeClient.update();
epoch = timeClient.getEpochTime(); // Heure Unix
nujour = timeClient.getDay(); // jour de la semaine
heure = timeClient.getFormattedTime(); // heure
// Calcul de la date en convertissant le temps UNIX epoch
time_t timestamp = epoch;
struct tm * pTime = localtime( & timestamp );
strftime( buffer,80, ” %d/%m/%Y “, pTime );
switch (nujour) { // on determine le jour
case 0:
jour = “dimanche”;
break;
case 1:
jour = “lundi”;
break;
case 2:
jour = “mardi”;
break;
case 3:
jour = “mercredi”;
break;
case 4:
jour = “jeudi”;
break;
case 5:
jour = “vendredi”;
break;
case 6:
jour = “samedi”;
break;
}
// Envoi des donnees recuperees sur la liaison serie
if (j%60==0)
{
Serial.print(“\r\nTU : “); //Temps UNIX
Serial.print(epoch);
Serial.print(“,”);
}
// Serial.print(” J=”);
// Serial.print(j); Serial.print(“,”);
Serial.print(jour);
Serial.print(“,”);
// strftime( buffer,80, ” %d/%m/%Y “, pTime );
Serial.print(buffer); Serial.print(” “);
Serial.print(timeClient.getFormattedTime());
Serial.print(“,”);
if( Once==1)
{
jd=(buffer[0]-48) *10 ;
jd=jd+(buffer[1]-48);
m1=(buffer[3]-48) *10;
m1= m1+(buffer[4]-48);
hh=(heure[0]-48)*10;
hh= hh+ (heure[1]-48);
mn=(heure[3]-48)*10;
mn= mn+ (heure[4]-48);
Serial.println(” “);
Serial.print(“mois=”); Serial.print(m1,DEC);Serial.println(” “);
Serial.print(“jour=”); Serial.print(jd,DEC);Serial.println(” “);
Serial.print(“heure=”);Serial.print(hh,DEC);Serial.println(” “);
Serial.print(“minute=”); Serial.print(mn,DEC);Serial.println(” “);
Serial.println(” “);
Once=0;
}
}
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(115200);
delay(500);
while (!Serial)
{
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println();
delay(1000);
Serial.print(” Directory : “);
Serial.println(DIRECTORY);
Serial.print(” MODULE : “);
Serial.println(MODULE);
Serial.print(” BOARD : “);
Serial.println(BOARD);
Serial.println(F(” ESP8266 Lolin Jaune 2x15pins\r\n”));
Serial.print(” _ESP8266_Lolin_Jaune_NTP_GPS_AM2302_SDCARD_2025-06.ino “);
Serial.println(VERSION);
Serial.println(” NTP Server: europe.pool.ntp.org”);
Serial.print(“\r\n ESP getCoreVersion : “);
Serial.println(ESP.getCoreVersion());
Serial.print(” ESP getSdkVersion : “);
Serial.println(ESP.getSdkVersion() );
pinMode(BP1,INPUT_PULLUP);
Serial.println(” BP1 input <–D2 (GPIO 4)”);
Serial.println(” AM2302-DHT11 –>D1 (GPIO 5)”);
dht.setup(DHTPIN,DHTesp::DHT22);
delay(1000);
t = dht.getTemperature();
if (isnan(t)) snprintf(Tempchar,6,”%s\t”,” ???”); else snprintf(Tempchar,6,”%3.2f\t”,t);
//——————-
h=dht.getHumidity();
if (isnan(h)) snprintf(Humidchar,6,”%s\t”,” ???”); else snprintf(Humidchar,6,”%2.2f\t”,h);
Serial.print(F(” DHT11 Temp : “));
Serial.print(Tempchar);
Serial.print(F(” Humid : “));
Serial.println(Humidchar);
Serial.println(” Attente connection STAtion Wifi\r\n”);
WiFi.begin(ssid, password);
while ( WiFi.status() != WL_CONNECTED ) {
delay ( 500 );
Serial.print ( “.” );
}
Serial.println ( “.” );
// Demarrage client NTP
timeClient.begin();
Affiche_Date_Heure_NTP();
j=0;
#ifdef option1
Serial.println(” Option 1″);
#else
Serial.println(” Option 2″);
#endif
Serial.println(“\nInitializing GPS “);
gpsSerial.begin(9600);
Serial.println(“\nInitializing SD card…”);
delay(2000);
// we’ll use the initialization code from the utility libraries
// since we’re just testing if the card is working!
if (!SD.begin(15)) // SS= Selec Scard=D8
{
Serial.println(“PB init SDCARD ,veralization failed. Things to check:”);
Serial.println(“* is a card inserted?”);
Serial.println(“* is your wiring correct?”);
Serial.println(“* did you change the chipSelect pin to match your shield or module?”);
while (1);
} else
{
Serial.println(“Wiring is correct and a card is present.”);
}
Serial.println();
// print the type of card
Serial.println();
Serial.print(“Card type: “);
switch (SD.type()) {
case 0:
Serial.println(“SD1”);
break;
case 1:
Serial.println(“SD2”);
break;
case 2:
Serial.println(“SDHC”);
break;
default:
Serial.println(“Unknown”);
}
Serial.print(“Cluster size: “);
Serial.println(SD.clusterSize());
Serial.print(“Blocks x Cluster: “);
Serial.println(SD.blocksPerCluster());
Serial.print(“Blocks size: “);
Serial.println(SD.blockSize());
Serial.print(“Total Blocks: “);
Serial.println(SD.totalBlocks());
Serial.println();
Serial.print(“Total Cluster: “);
Serial.println(SD.totalClusters());
Serial.println();
// print the type and size of the first FAT-type volume
uint32_t volumesize;
Serial.print(“Volume type is: FAT”);
Serial.println(SD.fatType(), DEC);
volumesize = SD.totalClusters();
volumesize= volumesize * SD.clusterSize();
volumesize = volumesize / 1000;
Serial.print(“Volume size (Kb): “);
Serial.println(volumesize);
Serial.print(“Volume size (Mb): “);
volumesize /= 1024;
Serial.println(volumesize);
Serial.print(“Volume size (Gb): “);
Serial.println((float)volumesize / 1024.0);
Serial.print(“Card size: “);
Serial.print((float)SD.size()/1000000);
Serial.println(” Mb”);
FSInfo fs_info;
SDFS.info(fs_info);
Serial.print(“Total bytes: “);
Serial.println(fs_info.totalBytes);
Serial.print(“Used bytes: “);
Serial.println(fs_info.usedBytes);
File dir = SD.open(“/”);
printDirectory(dir, 0);
// ——————————————-
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile.close();
myFile = SD.open(“test2025.txt”, FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print(“Writing to test.txt…”);
myFile.println(“testing 1, 2, 3.”);
// close the file:
myFile.close();
Serial.println(“done.”);
} else {
// if the file didn’t open, print an error:
Serial.println(“error opening test2025.txt”);
}
//————
// re-open the file for reading:
myFile = SD.open(“test2025.txt”);
if (myFile) {
Serial.println(“test2025.txt:”);
// read from the file until there’s nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn’t open, print an error:
Serial.println(“error opening test2025.txt”);
}
}
void loop(void)
{
j++;
if (j ==500)
{
t = dht.getTemperature();
if (isnan(t)) snprintf(Tempchar,6,”%s\t”,” ???”); else snprintf(Tempchar,6,”%3.2f\t”,t);
//——————-
h=dht.getHumidity();
if (isnan(h)) snprintf(Humidchar,6,”%s\t”,” ???”); else snprintf(Humidchar,6,”%2.2f\t”,h);
Serial.print(F(” DHT11 Temp : “));
Serial.print(Tempchar);
Serial.print(F(“, Humid : “));
Serial.print(Humidchar);
if (digitalRead(BP1)==HIGH) Serial.println(“,BP1 ON”); else Serial.println(“,BP1 OFF”);
}
//Affiche_Date_Heure_NTP();
while (gpsSerial.available())
{
gps.encode(gpsSerial.read());
}
if (gps.location.isUpdated())
{
#ifdef option1
// Affiche_Date_Heure_NTP();
lat = gps.location.lat();
lng = gps.location.lng();
alt = gps.altitude.meters();
dop= gps.hdop.value() / 100.0;
sat = gps.satellites.value();
//Serial.print(“\r\nTU : “); //Temps UNIX
// Serial.print(epoch);
Serial.print(“LAT: “);
Serial.print(gps.location.lat(), 6);
Serial.print(“,LON: “);
Serial.print(gps.location.lng(), 6);
// Serial.print(“SPEED (km/h) = “);
// Serial.println(gps.speed.kmph());
Serial.print(“,ALT :”);
Serial.print(gps.altitude.meters());
Serial.print(“,DOP: “);
Serial.print(gps.hdop.value() / 100.0);
Serial.print(“,SAT: “);
Serial.print(gps.satellites.value());
// Serial.print(“Time in UTC: “);
// Serial.println(String(gps.date.year()) + “/” + String(gps.date.month()) + “/”
// + String(gps.date.day()) + “,” + String(gps.time.hour()) + “:” + String(gps.time.minute())
// + “:” + String(gps.time.second()));
if (digitalRead(BP1)==HIGH )
{
Serial.println(” *”);
// Écriture SD
logFile.close();
logFile = SD.open(“gpslog.txt”, FILE_WRITE);
//if (!SD.begin(15))
if (logFile)
{
// logFile.printf(“j: %5d,Lat: %.6f,Lng: %.6f,Alt: %.1f m,Dop: %.2f,Sat: %d,Tmp: %s,Hum: %s”,
// j, lat, lng, alt, dop, sat, Tempchar,Humidchar);
logFile.printf(“j: %5d,Lat: %.6f,Lng: %.6f,Alt: %.1f m,Dop: %.2f,Sat: %d,Tmp: %.2f,Hum: %.2f\n”,
j, lat, lng, alt, dop, sat, t,h);
// logFile.printf(“j:%5d,Lat: %.6f,Lng: %.6f,Alt: %.1f,Dop: %3f,Sat: %d\n”,
// j,lat, lng, alt,dop,sat);
logFile.close();
}
}
else
Serial.println(“”);
#endif
#ifdef option2
Serial.print(“LAT: “);
Serial.println(gps.location.lat(), 6);
Serial.print(“,LON: “);
Serial.println(gps.location.lng(), 6);
// Serial.print(“SPEED (km/h) = “);
// Serial.println(gps.speed.kmph());
Serial.print(“,ALT :”);
Serial.println(gps.altitude.meters());
Serial.print(“,DOP: “);
Serial.println(gps.hdop.value() / 100.0);
Serial.print(“,SAT: “);
Serial.println(gps.satellites.value());
// Serial.print(“Time in UTC: “);
// Serial.println(String(gps.date.year()) + “/” + String(gps.date.month()) + “/”
// + String(gps.date.day()) + “,” + String(gps.time.hour()) + “:” + String(gps.time.minute())
// + “:” + String(gps.time.second()));
Serial.println(“”);
#endif
}
else
{
if (j>1000)
{
j=0;
Serial.printf(“\r\n”);
Affiche_Date_Heure_NTP();
Serial.print(“\r\n”);
// re-open the file for reading:
myFile = SD.open(“gpslog.txt”);
if (myFile) {
Serial.println(“gpslog.txt :”);
// read from the file until there’s nothing else in it:
while (myFile.available())
{
Serial.write(myFile.read());
}
Serial.println(” “);
// close the file:
myFile.close();
} else {
// if the file didn’t open, print an error:
Serial.println(“error opening gpslog.txt”);
}
}
}
delay(100);
}
Hi.
The issue is likely that the ESP8266 system time (used by the SD library for file timestamps) is not synchronized with the NTP time retrieved by your NTPClient.
You have to synchronize the system time with the time you get from the NTP Client.
For that, you can use the functions from the Time.h library.
- https://github.com/PaulStoffregen/Time
You can install it via the Arduino IDE library Manager.
Then, you can use the setTime() function to set the system time.
Check the examples provided by the library:
I hope this helps.
Regards,
Sara
Hello,
Thank’s for your support,
but , it does’nt solved the problem
With the help of chatgpt ,i get the fix..
NTP setup to get the correct date and time ….OK
with shift 1H00 (Paris) + shit summer time 1H
const long gmtOffset_sec = 3600;
const int daylightOffset_sec = 3600;
shift 1H00 + summer timer
setTime(…etc…); ..OK
then
Init SDCard …..OK
the main change in my code was to add this :
SD.dateTimeCallback(dateTime);
checking:
System Volume Information/
IndexerVolumeGuid76CREATION: 2025-06-12 10:56:38LAST WRITE: 2025-06-12 10:56:40<LF>
WPSettings.dat12CREATION: 2025-06-12 18:46:46LAST WRITE: 2025-06-12 18:46:48<LF>
test2025.txt 1206 CREATION: 2098-01-01 00:00:22 LAST WRITE: 2025-06-14 14:19:08<LF>
SDcard2G.txt 59 CREATION: 2025-06-12 10:58:32 LAST WRITE: 2025-06-12 10:58:34<LF>
GPS/
GPSN7001.log1157CREATION: 2025-06-14 14:20:28LAST WRITE: 2025-06-14 14:21:16<LF>
LastWrite on GPS date and time are now correct!
Probleme solved