Advertisement
RuiViana

Untitled

Jun 8th, 2015
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <SD.h>
  3. #include <Wire.h>
  4. #include <RTClib.h>
  5. #include <dht.h>
  6. #include <OneWire.h>
  7. #include <DallasTemperature.h>
  8.  
  9. //Programa : Sensor de umidade e temperatura DHT11/Cartão SD/Dois sensores de temperatura DS18B20
  10. //Autor : Gustavo Okano Alves Pinto
  11.  
  12. #define ONE_WIRE_BUS 10
  13. #define dht_dpin A1 //Pino DATA do Sensor ligado na porta Analogica A1
  14.  
  15.  
  16. RTC_DS1307 RTC;
  17. dht DHT; //Inicializa o sensor
  18. const int chipSelect = 53;
  19. OneWire oneWire(ONE_WIRE_BUS);
  20. DallasTemperature sensors(&oneWire);
  21. DeviceAddress vermelho_1_Thermometer = { 0x28, 0xFF, 0xB8, 0x67, 0x56, 0x14, 0x03, 0xB0 };
  22. DeviceAddress azul_1_Thermometer = { 0x28, 0xFF, 0x5B, 0x45, 0x64, 0x14, 0x04, 0x41 };
  23.  
  24. float tempC; // modifiquei aqui
  25. float tempA; // modifiquei aqui
  26. float tempV; // modifiquei aqui
  27. int Dia;
  28. int Mes;
  29. int Ano;
  30. int Hora;
  31. int Min;
  32. int Seg;
  33.  
  34. void setup(void)
  35. {
  36. Serial.begin(9600);
  37. Wire.begin();//Inicializacao do protocolo wire
  38. RTC.begin();//Inicializacao do modulo RTC
  39. delay (1000);
  40. //Verifica se o modulo esta funcionando ou nao
  41. if (!RTC.isrunning()) {
  42. Serial.println("RTC is NOT running!");
  43. //Ajusta a data/hora do Clock com a data/hora em que o codigo foi compilado, basta descomentar a linha
  44. RTC.adjust(DateTime(2015,6,8,19,11,55));
  45. }
  46.  
  47. Serial.print("Initializing SD card...");
  48.  
  49. // see if the card is present and can be initialized:
  50. if (!SD.begin(chipSelect)) {
  51. Serial.println("Card failed, or not present");
  52. // don't do anything more:
  53. return;
  54. }
  55.  
  56.  
  57. sensors.begin();
  58. sensors.setResolution(vermelho_1_Thermometer, 10);
  59. sensors.setResolution(azul_1_Thermometer, 10);
  60. }
  61.  
  62. void printTemperature(DeviceAddress deviceAddress)
  63. {
  64. // float tempC = sensors.getTempC(deviceAddress); // modifiquei aqui
  65. tempC = sensors.getTempC(deviceAddress); // modifiquei aqui
  66. if (tempC == -127.00)
  67. {
  68. Serial.print("Erro ao ler temperatura !");
  69. }
  70. else
  71. {
  72. Serial.print(" C: ");
  73. Serial.print(tempC);
  74. Serial.print(" F: ");
  75. Serial.print(DallasTemperature::toFahrenheit(tempC));
  76. }
  77. }
  78.  
  79. void loop(void)
  80. {
  81.  
  82. // make a string for assembling the data to log:
  83. String dataString = "";
  84.  
  85. // read one sensor and append to the string:
  86. for (int analogPin = 0; analogPin < 3; analogPin++) {
  87. int sensor = analogRead(analogPin);
  88. dataString += String(sensor);
  89. if (analogPin < 2) {
  90. dataString += ",";
  91. }
  92. }
  93.  
  94. // Get data
  95. DateTime now = RTC.now();//Recuperando a data e hora atual
  96. Dia = (now.day(), DEC);//Salvando o dia
  97. Mes = (now.month(), DEC);//Salvando o mes
  98. Ano = (now.year(), DEC);//Salvando o ano
  99. Hora = (now.hour(), DEC);//Salvando a hora
  100. Min = (now.minute(), DEC);//Salvando os minutos
  101. Seg = (now.second(), DEC);//Salvando os segundos
  102.  
  103. // open the file. note that only one file can be open at a time,
  104. // so you have to close this one before opening another.
  105. File dataFile = SD.open("datalog.csv", FILE_WRITE);
  106.  
  107. // if the file is available, write to it:
  108. if (dataFile) {
  109.  
  110. dataFile.println("Temperature,Umidade");
  111.  
  112. dataFile.println(DHT.temperature);
  113.  
  114. dataFile.println(DHT.humidity);
  115.  
  116. dataFile.println("TemperatureSensor1, TemperatureSensor2");
  117.  
  118. dataFile.println(tempV);
  119.  
  120. dataFile.println(tempA);
  121.  
  122. dataFile.println(Dia);
  123. dataFile.println(Mes);
  124. dataFile.println(Ano);
  125. dataFile.println(Hora);
  126. dataFile.println(Min);
  127. dataFile.println(Seg);
  128.  
  129. dataFile.close();
  130. // print to the serial port too:
  131. Serial.println();
  132. }
  133. // if the file isn't open, pop up an error:
  134. else {
  135. Serial.println("error opening datalog.csv");
  136. }
  137.  
  138.  
  139.  
  140. {
  141. DateTime now = RTC.now();//Recuperando a data e hora atual
  142. Serial.print(now.day(), DEC);//Imprimindo o dia
  143. Serial.print('/');
  144. Serial.print(now.month(), DEC);//Recuperando o mes
  145. Serial.print('/');
  146. Serial.print(now.year(), DEC);//Recuperando o ano
  147. Serial.print(' ');
  148. Serial.print(now.hour(), DEC);//Recuperando a hora
  149. Serial.print(':');
  150. Serial.print(now.minute(), DEC);//Reci[erando os minutos
  151. Serial.print(':');
  152. Serial.print(now.second(), DEC);//Recuperando os segundos
  153. Serial.println();
  154.  
  155.  
  156.  
  157. DHT.read11(dht_dpin); //Lê as informações do sensor
  158. Serial.print("SENSOR DHT11:\n\r");
  159. Serial.print("Umidade = ");
  160. Serial.print(DHT.humidity);
  161. Serial.print(" % ");
  162. Serial.print("Temperatura = ");
  163. Serial.print(DHT.temperature);
  164. Serial.println(" Celsius ");
  165.  
  166. {
  167. delay(3000);
  168. Serial.print("SENSOR DS18B20:\n\r");
  169. sensors.requestTemperatures();
  170. Serial.print("Temperatura Sensor 1: ");
  171. printTemperature(vermelho_1_Thermometer);
  172. tempV = tempC; // modifiquei aqui
  173. Serial.print("\n\r");
  174. Serial.print("Temperatura Sensor 2: ");
  175. printTemperature(azul_1_Thermometer);
  176. tempA = tempC; // modifiquei aqui
  177. Serial.print("\n\r");
  178.  
  179. delay(1000);//Aguarda 1 seg antes de acessar as informações do sensor
  180. }
  181.  
  182.  
  183. //Não diminuir o valor abaixo. O ideal é a leitura a cada 2 segundos
  184. delay(4000);
  185. }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement