Bonjour,
j'essaie de compiler 2 exemples de programmes sans succès
Ecriture sur carte SD
Lecture de la date via une synchronisation NTP
Dans mon programme ci joint, je n'ai aucune information écrite sur la carte SD...
Je ne vois pas d'ou pourrait venir le souci...
Votre avis ?
MERCI !
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <Time.h>
#include <LiquidCrystal.h>
#include <SD.h>
LiquidCrystal lcd(2, 3, 5, 6, 7, 8);
const int brocheSDCardSelect=4; // broche utilisée pour sélectionner la SD card
int capteur1 = 0; float U_temperature; float temperature;
int capteur2 = 1; float U_lumiere; float lumiere;
int capteur3 = 2; float hygrometrie; float U_hygrometrie;
int test; // Variable utilisée pour tester valeur renvoyée par fonctions SD Card
File file; // objet file
File root; // objet root pour le répertoire racine
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip( 192, 168, xxx, xxx );
IPAddress gateway( 192, 168, xxx, xxx );
IPAddress subnet( 255, 255, 255, 0 );
IPAddress timeServer(132, 163, 4, 101);
const int timeZone = 1;
EthernetUDP Udp;
unsigned int localPort = 8888; // local port to listen for UDP packets
void setup() {
Serial.begin(9600);
pinMode(10, OUTPUT); // met la broche 10 (SS) en sortie (nécessaire avec module ethernet)
digitalWrite(10, HIGH); // mais désactive le circuit intégré W5100 du module ethernet!
Serial.println("Initialisation de la SD card...");
test=SD.begin(brocheSDCardSelect);
if (test!=true) {
Serial.println("Echec initialisation!");
}
else {
Serial.println("Initialisation reussie !");
root = SD.open("/");
Serial.println("Repertoire racine ouvert !");
}
test=SD.remove("data.txt");
if (test) Serial.println("Fichier efface");
file = SD.open("data.txt", FILE_WRITE);
if (!file) {
Serial.println ("Erreur ouverture fichier !");
}
else {
Serial.println ("Fichier pret pour ecriture !");
}
file.println("temps;temperature;lumiere;hygrometrie");
file.close();
delay(1000);
while (!Serial) ; // Needed for Leonardo only
Serial.println ("2 !");
delay(250);
Serial.println("TimeNTP Example");
if (Ethernet.begin(mac) == 0) {
// no point in carrying on, so do nothing forevermore:
while (1) {
Serial.println("Failed to configure Ethernet using DHCP");
delay(10000);
}
}
Serial.print("IP number assigned by DHCP is ");
Serial.println(Ethernet.localIP());
Udp.begin(localPort);
Serial.println("waiting for sync");
setSyncProvider(getNtpTime);
}
time_t prevDisplay = 0; // when the digital clock was displayed
void loop(){
U_temperature = analogRead(capteur1);
temperature=1/((1/298.15)+(log ((((5* U_temperature)/1024)/((5-((5* U_temperature)/1024))/10000))/10000)/3977))-273.15;
U_lumiere = analogRead(capteur2);
lumiere=5*U_lumiere/1024;
U_hygrometrie = analogRead(capteur3);
hygrometrie=31.687*((5* U_hygrometrie)/1024) - 24.323 ;
prevDisplay = now();
digitalClockDisplay();
file = SD.open("data.txt", FILE_WRITE); // ouvre le fichier en écriture puis écriture des données
file.print(temperature), file.print(';');
file.print(lumiere), file.print(';');
file.print(hygrometrie);
file.println();
file.close(); // ferme le fichier
// lcd.setCursor(0, 1); lcd.print("Temp : "); lcd.print(temperature); lcd.print(" oC");
// lcd.setCursor(0, 2); lcd.print("Lumiere : "); lcd.print(lumiere); lcd.print(" V");
// lcd.setCursor(0, 3); lcd.print("Hygro : "); lcd.print(hygrometrie); lcd.print(" HR%");
// delay(2000);
// lcd.clear();
delay(1000); // 1 secondes entre chaque mesure
}
void digitalClockDisplay(){
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.print(day());
Serial.print(" ");
Serial.print(month());
Serial.print(" ");
Serial.print(year());
Serial.println();
}
void printDigits(int digits){
// utility for digital clock display: prints preceding colon and leading 0
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
/*-------- NTP code ----------*/
const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets
time_t getNtpTime()
{
while (Udp.parsePacket() > 0) ; // discard any previously received packets
Serial.println("Transmit NTP Request");
sendNTPpacket(timeServer);
uint32_t beginWait = millis();
while (millis() - beginWait < 1500) {
int size = Udp.parsePacket();
if (size >= NTP_PACKET_SIZE) {
Serial.println("Receive NTP Response");
Udp.read(packetBuffer, NTP_PACKET_SIZE); // read packet into the buffer
unsigned long secsSince1900;
// convert four bytes starting at location 40 to a long integer
secsSince1900 = (unsigned long)packetBuffer[40] << 24;
secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
secsSince1900 |= (unsigned long)packetBuffer[43];
return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
}
}
Serial.println("No NTP Response :-(");
return 0; // return 0 if unable to get the time
}
// send an NTP request to the time server at the given address
void 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();
}