Advertisement
RuiViana

Untitled

May 27th, 2015
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <SD.h>
  3. #include <dht.h>
  4. #include <OneWire.h>
  5. #include <DallasTemperature.h>
  6.  
  7. //Programa : Sensor de umidade e temperatura DHT11
  8. //Autor : Gustavo Okano Alves Pinto
  9.  
  10. #define ONE_WIRE_BUS 10
  11. #define dht_dpin A1 //Pino DATA do Sensor ligado na porta Analogica A1
  12.  
  13. dht DHT; //Inicializa o sensor
  14. const int chipSelect = 4;
  15. OneWire oneWire(ONE_WIRE_BUS);
  16. DallasTemperature sensors(&oneWire);
  17. DeviceAddress vermelho_1_Thermometer = { 0x28, 0xFF, 0xB8, 0x67, 0x56, 0x14, 0x03, 0xB0 };
  18. DeviceAddress azul_1_Thermometer = { 0x28, 0xFF, 0x5B, 0x45, 0x64, 0x14, 0x04, 0x41 };
  19.  
  20. float tempC; // modifiquei aqui
  21. float tempA; // modifiquei aqui
  22. float tempV; // modifiquei aqui
  23.  
  24. void setup(void)
  25. {
  26. Serial.begin(9600);
  27. while (!Serial) {
  28. // wait for serial port to connect. Needed for Leonardo only
  29. };
  30.  
  31. sensors.begin();
  32. sensors.setResolution(vermelho_1_Thermometer, 10);
  33. sensors.setResolution(azul_1_Thermometer, 10);
  34. }
  35.  
  36. void printTemperature(DeviceAddress deviceAddress)
  37. {
  38. // float tempC = sensors.getTempC(deviceAddress); // modifiquei aqui
  39. tempC = sensors.getTempC(deviceAddress); // modifiquei aqui
  40. if (tempC == -127.00)
  41. {
  42. Serial.print("Erro ao ler temperatura !");
  43. }
  44. else
  45. {
  46. Serial.print("C: ");
  47. Serial.print(tempC);
  48. Serial.print(" F: ");
  49. Serial.print(DallasTemperature::toFahrenheit(tempC));
  50. }
  51. }
  52.  
  53. void loop(void)
  54. {
  55. {
  56. DHT.read11(dht_dpin); //Lê as informações do sensor
  57. Serial.print("Umidade = ");
  58. Serial.print(DHT.humidity);
  59. Serial.print(" % ");
  60. Serial.print("Temperatura = ");
  61. Serial.print(DHT.temperature);
  62. Serial.println(" Celsius ");
  63.  
  64.  
  65. {
  66. delay(3000);
  67. Serial.print("Lendo temperaturas...\n\r");
  68. sensors.requestTemperatures();
  69. Serial.print("Temperatura Sensor 1: ");
  70. printTemperature(vermelho_1_Thermometer);
  71. tempV = tempC; // modifiquei aqui
  72. Serial.print("\n\r");
  73. Serial.print("Temperatura Sensor 2: ");
  74. printTemperature(azul_1_Thermometer);
  75. tempA = tempC; // modifiquei aqui
  76. Serial.print("\n\r");
  77.  
  78. Serial.print("Initializing SD card...");
  79.  
  80. // see if the card is present and can be initialized:
  81. if (!SD.begin(chipSelect)) {
  82. Serial.println("Card failed, or not present");
  83. // don't do anything more:
  84. return;
  85. }
  86. Serial.println("card initialized.");
  87. }
  88.  
  89. delay(1000);//Aguarda 1 seg antes de acessar as informações do sensor
  90. }
  91.  
  92. {
  93. // make a string for assembling the data to log:
  94. String dataString = "";
  95.  
  96. // read one sensor and append to the string:
  97. for (int analogPin = 0; analogPin < 3; analogPin++) {
  98. int sensor = analogRead(analogPin);
  99. dataString += String(sensor);
  100. if (analogPin < 2) {
  101. dataString += ",";
  102. }
  103. }
  104.  
  105.  
  106.  
  107.  
  108.  
  109. // open the file. note that only one file can be open at a time,
  110. // so you have to close this one before opening another.
  111. File dataFile = SD.open("datalog.csv", FILE_WRITE);
  112.  
  113. // if the file is available, write to it:
  114. if (dataFile) {
  115. dataFile.println("Temperatura,Umidade");
  116. dataFile.println(DHT.temperature);
  117. dataFile.println(DHT.humidity);
  118. dataFile.println("TemperaturaSensor1");
  119. dataFile.println("TemperaturaSensor2");
  120. dataFile.println(tempV); // modifiquei aqui
  121. dataFile.println(tempA); // modifiquei aqui
  122. // dataFile.println(vermelho_1_Thermometer); // modifiquei aqui
  123. // dataFile.println(azul_1_Thermometer); // modifiquei aqui
  124.  
  125. dataFile.close();
  126. // print to the serial port too:
  127. Serial.println();
  128. }
  129. // if the file isn't open, pop up an error:
  130. else {
  131. Serial.println("error opening datalog.csv");
  132. }
  133.  
  134.  
  135.  
  136. //Não diminuir o valor abaixo. O ideal é a leitura a cada 2 segundos
  137. delay(4000);
  138. }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement