1#include <SPI.h>
2#include <SD.h>
3#include <DHT.h>
4#include <RTClib.h>
5
6
7#define DHTPIN 2
8
9
10#define DHTTYPE DHT11
11
12
13
14
15DHT dht(DHTPIN, DHTTYPE);
16
17
18
19
20
21const int chipSelect = 4;
22
23
24File myFile;
25
26
27RTC_DS1307 rtc;
28
29void setup() {
30
31 dht.begin();
32
33
34 Serial.begin(9600);
35
36
37 while(!Serial);
38 if(! rtc.begin()) {
39 Serial.println("Couldn't find RTC");
40 while (1);
41 }
42 else {
43
44 rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
45 }
46 if(! rtc.isrunning()) {
47 Serial.println("RTC is NOT running!");
48 }
49
50
51 Serial.print("Initializing SD card...");
52
53 if(!SD.begin(chipSelect)) {
54 Serial.println("initialization failed!");
55 return;
56 }
57 Serial.println("initialization done.");
58
59
60 myFile=SD.open("DATA.txt", FILE_WRITE);
61
62
63 if (myFile) {
64 Serial.println("File opened ok");
65
66 myFile.println("Date,Time,Temperature ºC");
67 }
68 myFile.close();
69}
70
71void loggingTime() {
72 DateTime now = rtc.now();
73 myFile = SD.open("DATA.txt", FILE_WRITE);
74 if (myFile) {
75 myFile.print(now.year(), DEC);
76 myFile.print('/');
77 myFile.print(now.month(), DEC);
78 myFile.print('/');
79 myFile.print(now.day(), DEC);
80 myFile.print(',');
81 myFile.print(now.hour(), DEC);
82 myFile.print(':');
83 myFile.print(now.minute(), DEC);
84 myFile.print(':');
85 myFile.print(now.second(), DEC);
86 myFile.print(",");
87 }
88 Serial.print(now.year(), DEC);
89 Serial.print('/');
90 Serial.print(now.month(), DEC);
91 Serial.print('/');
92 Serial.println(now.day(), DEC);
93 Serial.print(now.hour(), DEC);
94 Serial.print(':');
95 Serial.print(now.minute(), DEC);
96 Serial.print(':');
97 Serial.println(now.second(), DEC);
98 myFile.close();
99 delay(1000);
100}
101
102void loggingTemperature() {
103
104
105
106 float t = dht.readTemperature();
107
108
109
110
111 if (isnan(t) ) {
112 Serial.println("Failed to read from DHT sensor!");
113 return;
114 }
115
116
117 Serial.print("Temperature: ");
118 Serial.print(t);
119 Serial.println(" *C");
120
121
122
123 myFile = SD.open("DATA.txt", FILE_WRITE);
124 if (myFile) {
125 Serial.println("open with success");
126 myFile.print(t);
127 myFile.println(",");
128 }
129 myFile.close();
130}
131
132void loop() {
133 loggingTime();
134 loggingTemperature();
135 delay(5000);
136}
vikramaditya1
2 years ago
hello i am using this code to record temp data but it is not recording in 24 hour format .after 12 am/pm it is displaying last recorded time like 00.45.23 26 *C 00.45.23 26.5 *C 00.45.23 27*C 00.45.23 25*C what should i change in the code so that i get time in 12 hour /24 hour format.