Demande d'infos relevé de température avec sauvegarde sur microSD et visu.

Salut,

Je voulais faire à peu près la même chose que toi, et je pense j'ai réussi.
J’écris dans la carte SD, toutes les 10 secondes, les valeurs d'une sonde, et je rajoute sur chaque ligne, le timestamp unix, retrouvé en NTP.
IL y a aussi j'ai un petit serveur web qui me permet d'aller télécharger le fichier.

Je ne voulais pas dépendre d'un serveur "extérieur", le but de mon projet étant de diminuer ma facture electrique en réglant mes chauffages via l'arduino, c'est pas pour avoir un serveur qui tourne 100% du temps !
J'ai mon Arduino depuis 5 heures.. un peu d'indulgence dans la programmation, j'ai encore du mal avec les String, par exemple !

J'ai pas encore bossé sur les sondes, J'ecris la valeur de A0, mais tu dois pouvoir mettre autre chose

/*
  Syslog + Web Server to download the syslog file

 
 
 created 16 Mars 2012
 by Cédric2, libre de tous droit.
 Premier programme...
 
 */

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <SD.h>


// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1, 2);
char Fichier[] = "logfile5.txt";

// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
EthernetServer server(80);


//Déclaration pour client NTP
unsigned int localPort = 8888;      // local port to listen for UDP/NTP packets
//IPAddress timeServer(192, 43, 244, 18); // time.nist.gov NTP server
IPAddress timeServer(192, 168, 1, 254); // NTP server freebox, donc fonctionne même si adsl down :-)
const int NTP_PACKET_SIZE= 48; // NTP time stamp is in the first 48 bytes of the message
byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets 
// A UDP instance to let us send and receive packets over UDP
EthernetUDP Udp;


long i = 50000;  // petit compteur pour ne pas ecrire trop souvent//

void setup()
{

  // start the Ethernet connection and the server:
  Serial.begin(9600);
  Serial.print("Initializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin 
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output 
  // or the SD library functions will not work. 
  pinMode(10, OUTPUT);

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.begin(9600);
  Udp.begin(localPort);
}

void loop()
{
  //Ecriture sur SD carte toutes les 50000 itérations (Environs toutes les 5s)// a améliorer
  if (i >= 50000)
  {

    Serial.println("Debut ecriture du fichier");
    //je commence par demander à la freebox l'heure
    sendNTPpacket(timeServer); // send an NTP packet to a time server
    // wait to see if a reply is available
    delay(1000);  
    if ( Udp.parsePacket() ) {  
      // We've received a packet, read the data from it
      Udp.read(packetBuffer,NTP_PACKET_SIZE);  // read the packet into the buffer

      //the timestamp starts at byte 40 of the received packet and is four bytes,
      // or two words, long. First, esxtract the two words:

      unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
      unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);  
      // combine the four bytes (two words) into a long integer
      // this is NTP time (seconds since Jan 1 1900):
      unsigned long secsSince1900 = highWord << 16 | lowWord;  

      // now convert NTP time into everyday time:
      Serial.print("Unix time = ");
      // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
      const unsigned long seventyYears = 2208988800UL;     
      // subtract seventy years:
      unsigned long epoch = secsSince1900 - seventyYears;  //Unix time
      // print Unix time:
      Serial.println(epoch);                               
      int sensorValue = analogRead(A0);
      //int  sensorValue = 120;


      File dataFile_write = SD.open(Fichier, FILE_WRITE);
      // if the file is available, write to it:
      String tmp_string = String("<unixtime>"+String(epoch)+"</unixtime>"+"<sensorValue>"+sensorValue+"<sensorValue>");
      if (dataFile_write) {
        dataFile_write.println(tmp_string);
        dataFile_write.close();
        // print to the serial port too:
        Serial.println(tmp_string);
        i=1;   // tout est OK, je re-initialise mon compteur.
      }  
      // if the file isn't open, pop up an error:
      else {
        Serial.println("error opening datalog.txt");
      } 
    }
    else
    {
      Serial.println("Pas recu de réponse NTP");
    }
  }
  else
  {
    i++;
  }
  //partie serveur Web
  EthernetClient client = server.available();
  if (client) {
    Serial.println("client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: plain/text");
          client.println();

          // output the value of each analog input pin
          File
            myFile = SD.open(Fichier);
          if (myFile) {
            Serial.print("Open file ");
            Serial.println(
            Fichier);


            // read from the file until there's nothing else in it:
            while (myFile.available()) {
              char read_file = myFile.read();
              Serial.write(read_file);

              client.write(read_file);


            }
            // close the file:
            myFile.close();
          } 
          else {
            // if the file didn't open, print an error:
            Serial.println("error opening test.txt");
          }
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } 
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
  }
}



// send an NTP request to the time server at the given address 
unsigned long sendNTPpacket(IPAddress& address)
{
  // set all bytes in the buffer to 0
  memset(packetBuffer, 0, NTP_PACKET_SIZE); 
  // Initialize values needed to form NTP request
  // (see URL above for details on the packets)
  packetBuffer[0] = 0b11100011;   // LI, Version, Mode
  packetBuffer[1] = 0;     // Stratum, or type of clock
  packetBuffer[2] = 6;     // Polling Interval
  packetBuffer[3] = 0xEC;  // Peer Clock Precision
  // 8 bytes of zero for Root Delay & Root Dispersion
  packetBuffer[12]  = 49; 
  packetBuffer[13]  = 0x4E;
  packetBuffer[14]  = 49;
  packetBuffer[15]  = 52;

  // all NTP fields have been given values, now
  // you can send a packet requesting a timestamp: 		   
  Udp.beginPacket(address, 123); //NTP requests are to port 123
  Udp.write(packetBuffer,NTP_PACKET_SIZE);
  Udp.endPacket(); 
}