Advertisement
J0N4TH4NGR4NDM0NT

Code temporaire pour enregistrement des températures

Mar 27th, 2023
975
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. en/*
  2.   SD card read/write
  3.  
  4.   This example shows how to read and write data to and from an SD card file
  5.   The circuit:
  6.    SD card attached to SPI bus as follows:
  7.  ** MOSI - pin 11
  8.  ** MISO - pin 12
  9.  ** CLK - pin 13
  10.  ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)
  11.  
  12.   created   Nov 2010
  13.   by David A. Mellis
  14.   modified 9 Apr 2012
  15.   by Tom Igoe
  16.  
  17.   This example code is in the public domain.
  18.  
  19. */
  20.  
  21. #include <SPI.h>
  22. #include <SD.h>
  23. #include <Wire.h>
  24. #include <RTC.h>
  25. static DS1307 RTC;
  26. #include "DHT.h"
  27. #define dht_int A2
  28. #define dht_ext A1
  29. #define DHTTYPE DHT11
  30. DHT dht1(dht_int, DHTTYPE);
  31. DHT dht2(dht_ext, DHTTYPE);
  32. int resetPin = A0;
  33.  
  34.  
  35. File myFile;
  36.  
  37. void setup() {
  38.   // Open serial communications and wait for port to open:
  39.   Serial.begin(115200);
  40.   while (!Serial) {
  41.   ;
  42.   }
  43.   pinMode(resetPin, OUTPUT);  
  44.   RTC.begin();
  45.   Wire.begin();
  46.   dht1.begin();
  47.   dht2.begin();
  48.   delay(1000);
  49.   Serial.print(".");
  50.   delay(0500);
  51.   Serial.print(".");
  52.   delay(0500);
  53.   Serial.print(".");
  54.   delay(5000);
  55.  
  56.   Serial.print("Initializing SD card...");
  57.   delay(3000);
  58.   if (!SD.begin(4)) {
  59.     Serial.println("initialization failed!");
  60.     delay(5000);
  61.     digitalWrite(resetPin, HIGH);
  62.     while (1);
  63.   }
  64.   Serial.println("initialization done.");
  65.  
  66.   // open the file. note that only one file can be open at a time,
  67.   // so you have to close this one before opening another.
  68.   myFile = SD.open("test.txt", FILE_WRITE);
  69.  
  70.   //if the file opened okay, write to it:
  71.   if (myFile) {
  72.     Serial.print("Writing to test.txt...");
  73.     myFile.println("testing 1, 2, 3.");
  74.   //  close the file:
  75.     myFile.close();
  76.     Serial.println("done.");
  77.    
  78.   }
  79.   /*else {
  80.     // if the file didn't open, print an error:
  81.     Serial.println("error opening test.txt");
  82.   }
  83.  
  84.   // re-open the file for reading:
  85.   myFile = SD.open("test.txt");
  86.   if (myFile) {
  87.     Serial.println("test.txt:");
  88.  
  89.     // read from the file until there's nothing else in it:
  90.     while (myFile.available()) {
  91.       Serial.write(myFile.read());
  92.     }
  93.     // close the file:
  94.     myFile.close();
  95.   } else {
  96.     // if the file didn't open, print an error:
  97.     Serial.println("error opening test.txt");
  98.   }*/
  99. }
  100.  
  101. void loop() {
  102.  
  103.  myFile = SD.open("test.txt", FILE_WRITE);
  104.  
  105.  
  106.  
  107.   float h1 = dht1.readHumidity();
  108.   float t1 = dht1.readTemperature();
  109.   float f1 = dht1.readTemperature(true);
  110.   float h2 = dht2.readHumidity();
  111.   float t2 = dht2.readTemperature();
  112.   float f2 = dht2.readTemperature(true);
  113.  
  114.   if (RTC.getDay()<10){
  115.   myFile.print(0);
  116.   }
  117.   myFile.print(RTC.getDay());
  118.   myFile.print("-");
  119.   if (RTC.getMonth()<10){
  120.   myFile.print(0);
  121.   }
  122.   myFile.print(RTC.getMonth());
  123.   myFile.print("-");
  124.   myFile.print(RTC.getYear());
  125.   myFile.print(",");
  126.   myFile.print(RTC.getHours());
  127.   myFile.print(":");
  128.   if (RTC.getMinutes()<10){
  129.   myFile.print(0);
  130.   }
  131.   myFile.print(RTC.getMinutes());
  132.   myFile.print(":");
  133.   if (RTC.getSeconds()<10){
  134.   myFile.print(0);
  135.   }
  136.   myFile.print(RTC.getSeconds());
  137.   myFile.print(",");
  138.   myFile.print("T_INT:,");
  139.   myFile.print(t1);
  140.   myFile.print(",degré_C & ");
  141.   myFile.print(" HUMIDITÉ:,");
  142.   myFile.print(h1);
  143.   myFile.print(",%  ");
  144.   myFile.print("T_EXT:,");
  145.   myFile.print(t2);
  146.   myFile.print(",degré C & ");
  147.   myFile.print("HUMIDITÉ :, ");
  148.   myFile.print(h2);
  149.   myFile.println();
  150.   myFile.close();
  151.  
  152.   delay(10000);
  153.  
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement