Advertisement
Guest User

Untitled

a guest
Oct 10th, 2015
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.72 KB | None | 0 0
  1.  
  2.  
  3. #include <JeeLib.h>
  4. //#include <Narcoleptic.h>
  5. #include "Wire.h"
  6.  
  7. ISR(WDT_vect) { Sleepy::watchdogEvent(); }
  8. /// Karta SD ////
  9. #include <SD.h>
  10. #define KARTAPIN 6
  11. File myFile;
  12.  
  13. /// Czujnik wilgotności i temperatury powietrza ////
  14. #include <dht.h>
  15. dht DHT22;
  16. #define DHT22PIN 7
  17.  
  18. /// Czujnik światła ///
  19. #include <BH1750.h>
  20. BH1750 czujnikSwiatla;
  21.  
  22. /// Czujnik wilgotności gleby ///
  23. int sensor_A0 = A0; // podłączenie od A0 na czujniku do A0 na Arduino
  24. int sensor_D0 = 2; // podłączenie od D0 na czujniku do pinu 2 na Arduino
  25. int wartosc_A0;
  26.  
  27. /// sonda temperatury ///
  28. #include <OneWire.h>
  29. #include <DallasTemperature.h>
  30. #define sondaPIN 16
  31. float tmp=0;
  32. OneWire oneWire(sondaPIN);
  33. DallasTemperature sensors(&oneWire);
  34.  
  35.  
  36. /// RTC ///
  37. #define DS3231_I2C_ADDRESS 0x68
  38. byte second, minute, hour, dayOfWeek,dayOfMonth, month, year;
  39.  
  40. char combinedArray[13];
  41. byte lastday=99;
  42. //////////////////////////////////////////////////////////////////////////////////
  43. void setup()
  44. {
  45. //Serial.begin(9600);
  46. Wire.begin();
  47. czujnikSwiatla.begin();
  48. sensors.begin();
  49.  
  50. pinMode(KARTAPIN, OUTPUT);
  51. pinMode(10, INPUT);
  52.  
  53. if (!SD.begin(KARTAPIN))
  54. {
  55. Serial.print("Error SD");
  56. return;
  57. }
  58.  
  59. delay(3000);
  60. }
  61.  
  62. void loop()
  63. {
  64.  
  65. readDS3231time(&second, &minute, &hour,&dayOfWeek, &dayOfMonth, &month, &year);
  66. char msc[3];
  67. char dzien[3];
  68. char rok[5];
  69.  
  70. snprintf(msc, 3, "%d", month); //char
  71. snprintf(dzien, 3, "%d", dayOfMonth);
  72. snprintf(rok, 5, "%d", year);
  73.  
  74.  
  75. combinedArray[0]=dzien[0];
  76. combinedArray[1]=dzien[1];
  77.  
  78. combinedArray[2]='_';
  79.  
  80. if(month<10)
  81. {
  82. combinedArray[3]='0';
  83. combinedArray[4]=msc[0];
  84. }
  85. else
  86. {
  87. combinedArray[3]=msc[0];
  88. combinedArray[4]=msc[1];
  89. }
  90.  
  91. combinedArray[5]='_';
  92. combinedArray[6]=rok[0];
  93. combinedArray[7]=rok[1];
  94. combinedArray[8]='.';
  95. combinedArray[9]='c';
  96. combinedArray[10]='s';
  97. combinedArray[11]='v';
  98.  
  99. myFile = SD.open(combinedArray,FILE_WRITE);
  100. if(dayOfWeek!=lastday)
  101. myFile.println("data;godzina;wilgotnosc powietrza [%];temperatura powietrza [C];wilgotnosc gleby [%];wartosc A0;temperatura gleby [C];natezenie swiatla [lux]");
  102. lastday=dayOfWeek;
  103. data();
  104. delay(10);
  105. czujDHT22();
  106. czujWILGOT();
  107. sondaTEM();
  108. czujSWIATLA();
  109. myFile.close();
  110. for(int i=0;i<30;i++){Sleepy::loseSomeTime(60000);}
  111.  
  112. }
  113.  
  114.  
  115. //////////////////////////////// RTC////////////////////////////////////////
  116. String readDS3231time(byte *second,
  117. byte *minute,
  118. byte *hour,
  119. byte *dayOfWeek,
  120. byte *dayOfMonth,
  121. byte *month,
  122. byte *year)
  123. {
  124. Wire.beginTransmission(DS3231_I2C_ADDRESS);
  125. Wire.write(0); // set DS3231 register pointer to 00h
  126. Wire.endTransmission();
  127. Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  128. // request seven bytes of data from DS3231 starting from register 00h
  129. *second = bcdToDec(Wire.read() & 0x7f);
  130. *minute = bcdToDec(Wire.read());
  131. *hour = bcdToDec(Wire.read() & 0x3f);
  132. *dayOfWeek = bcdToDec(Wire.read());
  133. *dayOfMonth = bcdToDec(Wire.read());
  134. *month = bcdToDec(Wire.read());
  135. *year = bcdToDec(Wire.read());
  136. }
  137.  
  138.  
  139. void data()
  140. {
  141. myFile.print(year, DEC);
  142. myFile.print("/");
  143. if(month<10) myFile.print("0");
  144. myFile.print(month, DEC);
  145. myFile.print("/");
  146. myFile.print(dayOfMonth, DEC);
  147. myFile.print(" ");
  148. myFile.print(";");
  149.  
  150. myFile.print(hour, DEC);
  151. myFile.print(":");
  152.  
  153. myFile.print(minute, DEC);
  154. myFile.print(":");
  155.  
  156. myFile.print(second, DEC);
  157. myFile.print(" ");
  158.  
  159. myFile.print(";");
  160. }
  161.  
  162. byte decToBcd(byte val)
  163. {
  164. return( (val/10*16) + (val%10) );
  165. }
  166. byte bcdToDec(byte val)
  167. {
  168. return( (val/16*10) + (val%16) );
  169. }
  170.  
  171. void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek , byte
  172. dayOfMonth, byte month, byte year)
  173. {
  174. // sets time and date data to DS3231
  175. Wire.beginTransmission(DS3231_I2C_ADDRESS);
  176. Wire.write(0); // set next input to start at the seconds register
  177. Wire.write(decToBcd(second)); // set seconds
  178. Wire.write(decToBcd(minute)); // set minutes
  179. Wire.write(decToBcd(hour)); // set hours
  180. Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
  181. Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
  182. Wire.write(decToBcd(month)); // set month
  183. Wire.write(decToBcd(year)); // set year (0 to 99)
  184. Wire.endTransmission();
  185. }
  186.  
  187. ////////////////////////// Czujnik wilgotności i temperatury powietrza //////////////////////////////////
  188. void czujDHT22()
  189. {
  190. int chk = DHT22.read(DHT22PIN); //sprawdzenie stanu sensora, a następnie wyświetlenie komunikatu na monitorze szeregowym
  191. switch (chk)
  192. {
  193. case DHTLIB_OK:
  194. break;
  195. case DHTLIB_ERROR_CHECKSUM:
  196. myFile.println(" ");
  197. myFile.println("Błąd sumy kontrolnej");
  198. myFile.println(" ");
  199. break;
  200. case DHTLIB_ERROR_TIMEOUT:
  201. myFile.println(" ");
  202. myFile.println("Koniec czasu oczekiwania - brak odpowiedzi");
  203. myFile.println(" ");
  204. break;
  205. default:
  206. myFile.println(" ");
  207. myFile.println("Nieznany błąd");
  208. myFile.println(" ");
  209. break;
  210. }
  211. myFile.print((float)DHT22.humidity, 2);
  212. myFile.print(";");
  213.  
  214. myFile.print((float)DHT22.temperature, 2);
  215. myFile.print(";");
  216. }
  217.  
  218. ////////////////////////////// Czujnik wilgotności gleby /////////////////////////
  219. void czujWILGOT()
  220. {
  221. wartosc_A0 = analogRead(sensor_A0); // pobranie wartości z A0
  222. int sensorConstrainl=constrain(wartosc_A0,300 ,1022);
  223. int soill = map(sensorConstrainl,300 ,1022,100,0);
  224. myFile.print(soill);
  225. myFile.print(";");
  226.  
  227. myFile.print(wartosc_A0);
  228. myFile.print(";");
  229. }
  230.  
  231. /////////////////////////////// Sonda temeratury /////////////////////////
  232.  
  233. void sondaTEM()
  234. {
  235. tmp =(sensors.getTempCByIndex(0));
  236. myFile.print(tmp);
  237. sensors.requestTemperatures(); // Send the command to get temperature
  238.  
  239. myFile.print(";");
  240. }
  241.  
  242. ///////////////////////////////// Czujnik natezenia swiatla ///////////////////////////////
  243. void czujSWIATLA()
  244. {
  245. uint16_t lux = czujnikSwiatla.readLightLevel(); //odczytanie wartości z czujnika
  246. myFile.println(lux);
  247.  
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement