Advertisement
Guest User

Untitled

a guest
Jun 1st, 2014
974
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.17 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.  
  7. // Released to the public domain
  8. // no warranty whatsoever!
  9.  
  10. dht DHT;
  11.  
  12. #define DHT22_PIN 7
  13.  
  14. #define MOISTURESENSOR1 0 // plant 7
  15. #define MOISTURESENSOR2 2 // plant 1
  16. #define MOISTURESENSOR3 3 // plant 6
  17.  
  18. // SD Shield pin, make sure not to use the chosen one for other sensors!
  19. // change this to match your SD shield or module;
  20. // Arduino Ethernet shield: pin 4
  21. // Adafruit SD shields and modules: pin 10
  22. // Sparkfun SD shield: pin 8
  23. const int chipSelect = 10;  
  24.  
  25. int photocellPin = 1;
  26.  
  27. // delay between averaging measurements (NOT the delay between individual measurements!)
  28. #define MEASUREDELAY 100
  29. // number of averaging measurements
  30. #define MEASURECOUNT 5
  31.  
  32.  
  33.  
  34. Sd2Card card;
  35. SdVolume volume;
  36. SdFile root;
  37.  
  38. RTC_DS1307 RTC;
  39.  
  40. void setup()
  41. {
  42.   Serial.begin(9600);
  43.  
  44.  
  45.     Wire.begin();
  46.     RTC.begin();
  47.    if (! RTC.isrunning()) {
  48.     Serial.println("RTC is NOT running!");
  49.     // following line sets the RTC to the date & time this sketch was compiled
  50.     // uncomment it & upload to set the time, date and start run the RTC!
  51. //    RTC.adjust(DateTime(__DATE__, __TIME__));
  52.   }
  53.  
  54.     Serial.print("Initializing SD card...");
  55.   // make sure that the default chip select pin is set to
  56.   // output, even if you don't use it:
  57.   pinMode(10, OUTPUT);
  58.  
  59.  
  60.  
  61.    
  62.   if (!SD.begin(10)) {
  63.     Serial.println("initialization failed!");
  64.     return;
  65.   }
  66.   Serial.println("initialization done.");
  67.  
  68.  
  69.    Serial.print("DHT LIBRARY VERSION: ");
  70.   Serial.println(DHT_LIB_VERSION);
  71.   Serial.println();
  72.  
  73.  
  74. }
  75.  
  76. void loop()
  77. {
  78.  
  79.   DateTime now = RTC.now();
  80.  
  81. //for log
  82.     char dateTimeString[20];
  83. sprintf (dateTimeString, "%.2d/%.2d/%.2d-%.2d:%.2d:%.2d", now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second());
  84.    String datetime(dateTimeString);
  85.  
  86. //for filename
  87.     char dateTimeStringFilename[20];
  88. sprintf (dateTimeStringFilename, "%.2d%.2d%.2d", now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second());
  89.    String dateTimeStringFilenameString(dateTimeStringFilename);
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.   // read moisture1 from moisutre sensor
  98. float moisture = getAverageAnalog(MOISTURESENSOR1,MEASURECOUNT,MEASUREDELAY); // single top left connection, plant 7 (front plant)
  99. float moisture2 = getAverageAnalog(MOISTURESENSOR2,MEASURECOUNT,MEASUREDELAY); // lower left connection, plant 1 (tub)
  100. float moisture3 = getAverageAnalog(MOISTURESENSOR3,MEASURECOUNT,MEASUREDELAY); // lower right connection, plant 6 (back-right plant)
  101.  
  102. // get brightness from photocell, seems super inacurate
  103. float photocellReading = getAverageAnalog(photocellPin,100,10);
  104.  
  105.  
  106.   // read temperature and air humidity from DHT sensor
  107.   int chk = DHT.read22(DHT22_PIN);
  108.   switch (chk)
  109.   {
  110.     case DHTLIB_OK:  
  111.          
  112.         break;
  113.     case DHTLIB_ERROR_CHECKSUM:
  114.         Serial.print("Checksum error,\t");
  115.         break;
  116.     case DHTLIB_ERROR_TIMEOUT:
  117.         Serial.print("Time out error,\t");
  118.         break;
  119.     default:
  120.         Serial.print("Unknown error,\t");
  121.         break;
  122.   }
  123.  
  124.  
  125.  
  126. // humidity sensor seems to be very inprecise, use measurecount * 2 !!!!!!!! (still inprecise, but a little better)
  127.   float humidity=0;
  128.   for (int i=0;i<MEASURECOUNT;i++)
  129.   {
  130.    // read and add
  131.     humidity+=DHT.humidity;
  132.     delay(MEASUREDELAY);
  133.   }
  134.   humidity = humidity/(MEASURECOUNT);
  135.  
  136.  
  137.  
  138.   float temperature = 0;
  139.   for (int i=0;i<MEASURECOUNT;i++)
  140.   {
  141.    // read and add
  142.     temperature+=DHT.temperature;
  143.     delay(MEASUREDELAY);
  144.   }
  145.   temperature=temperature/MEASURECOUNT;
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153. String dataString = datetime+"\t"+humidity+"\t"+temperature+"\t"+moisture+"\t"+moisture2+"\t"+moisture3+"\t"+photocellReading;
  154.  
  155.  
  156. // debug to USB
  157. Serial.println(dataString);
  158.  
  159.  
  160. // apple numbers need .txt extension to open tab separated files correctly. .csv only for comma separated, eh
  161. String filename = dateTimeStringFilenameString+".txt";
  162. char * buffer = new char[filename.length()];
  163. strcpy(buffer,filename.c_str());
  164.  
  165.   File dataFile = SD.open(buffer, FILE_WRITE);
  166.  
  167.   // if the file is available, write to it:
  168.   if (dataFile) {
  169.     dataFile.println(dataString);
  170.     dataFile.close();
  171.   }  
  172.   // if the file isn't open, pop up an error:
  173.   else {
  174.     Serial.println("error opening "+filename);
  175.   }
  176.  
  177.  
  178.  
  179.  
  180.  
  181.   // new char[] uses malloc  
  182.   free(buffer);
  183.  
  184.   delay(60000); // 900000 = 15 minutes delay 60000=1min
  185. }
  186.  
  187.  
  188. float getAverageAnalog(int pin, int iterations, int delaySeconds)
  189. {
  190.     float value=0;
  191.  
  192.   for (int i=0;i<iterations;i++)
  193.   {
  194.    // read and add
  195.     value+=analogRead(pin);
  196.     delay(delaySeconds);
  197.   }
  198.  
  199.   return value/iterations;
  200. }
  201.  
  202.  
  203.  
  204. //Rounds down (via intermediary integer conversion truncation)
  205. String doubleToString(double input,int decimalPlaces){
  206. if(decimalPlaces!=0){
  207. String string = String((int)(input*pow(10,decimalPlaces)));
  208. if(abs(input)<1){
  209. if(input>0)
  210. string = "0"+string;
  211. else if(input<0)
  212. string = string.substring(0,1)+"0"+string.substring(1);
  213. }
  214. return string.substring(0,string.length()-decimalPlaces)+"."+string.substring(string.length()-decimalPlaces);
  215. }
  216. else {
  217. return String((int)input);
  218. }
  219. }
  220.  
  221.  
  222. //
  223. // END OF FILE
  224. //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement