Thanks to all! Finally I can write timestamp and data on file.
I have just 1 question.
I attached too an OLED display because I want read in real time Humidity and Temperature but data need to be stored on file ONLY using a delay of 10minutes.
How can I separate real time monitor from storing data on file??
void loop() {
//float h = dht.readHumidity(); // Leggo il valore di umidità
//float t = dht.readTemperature(); // Leggo il valore di temperatura
u8g.firstPage();
do {
draw();
} while ( u8g.nextPage() );
delay(600000);
myFile = SD.open(filename, FILE_WRITE);
myFile.print(timeStamp());
myFile.print(" ");
myFile.print(dht.readHumidity());
myFile.print(" ");
myFile.println(dht.readTemperature());
// close the file:
myFile.close();
Serial.println(" done.");
//delay(600000); // wait 10minutes
}
This is the final code
#include "U8glib.h"
#include "DHT.h"
#include <Wire.h>
#include <SPI.h>
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE | U8G_I2C_OPT_DEV_0); // I2C / TWI
DHT dht(2,DHT22); //Definisco il pin al quale è collegato il sensore e il tipo
float h;
float t;
void draw(void) {
//u8g.setFont(u8g_font_unifont);
//u8g.setFont(u8g_font_freedoomr10r);
//u8g.setFont(u8g_font_gdr12);
u8g.setFont(u8g_font_fur14);
u8g.setPrintPos(0, 15);
u8g.print("H:");
// u8g.setPrintPos(30, 10);
u8g.print(" ");
u8g.print(h);
//u8g.setPrintPos(80, 10);
u8g.print(" %");
u8g.setPrintPos(0,50);
u8g.print("T:");
//u8g.setPrintPos(30, 40);
u8g.print(" ");
u8g.print(t);
//u8g.setPrintPos(80, 40);
u8g.print(" ");
u8g.write(0xB0); //write degree characher
u8g.print("C");
}
void setup(void) {
// assign default color value
if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
u8g.setColorIndex(255); // white
}
else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
u8g.setColorIndex(3); // max intensity
}
else if ( u8g.getMode() == U8G_MODE_BW ) {
u8g.setColorIndex(1); // pixel on
}
else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
u8g.setHiColorByRGB(255, 255, 255);
}
}
void loop(void) {
h = dht.readHumidity();
t = dht.readTemperature();
// picture loop
u8g.firstPage();
do {
draw();
} while ( u8g.nextPage() );
// rebuild the picture after some delay
delay(2000);
}