Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <SD.h>
- #include <Wire.h>
- #include "RTClib.h"
- #include <dht.h>
- // Released to the public domain
- // no warranty whatsoever!
- dht DHT;
- #define DHT22_PIN 7
- #define MOISTURESENSOR1 0 // plant 7
- #define MOISTURESENSOR2 2 // plant 1
- #define MOISTURESENSOR3 3 // plant 6
- // SD Shield pin, make sure not to use the chosen one for other sensors!
- // change this to match your SD shield or module;
- // Arduino Ethernet shield: pin 4
- // Adafruit SD shields and modules: pin 10
- // Sparkfun SD shield: pin 8
- const int chipSelect = 10;
- int photocellPin = 1;
- // delay between averaging measurements (NOT the delay between individual measurements!)
- #define MEASUREDELAY 100
- // number of averaging measurements
- #define MEASURECOUNT 5
- Sd2Card card;
- SdVolume volume;
- SdFile root;
- RTC_DS1307 RTC;
- void setup()
- {
- Serial.begin(9600);
- Wire.begin();
- RTC.begin();
- if (! RTC.isrunning()) {
- Serial.println("RTC is NOT running!");
- // following line sets the RTC to the date & time this sketch was compiled
- // uncomment it & upload to set the time, date and start run the RTC!
- // RTC.adjust(DateTime(__DATE__, __TIME__));
- }
- Serial.print("Initializing SD card...");
- // make sure that the default chip select pin is set to
- // output, even if you don't use it:
- pinMode(10, OUTPUT);
- if (!SD.begin(10)) {
- Serial.println("initialization failed!");
- return;
- }
- Serial.println("initialization done.");
- Serial.print("DHT LIBRARY VERSION: ");
- Serial.println(DHT_LIB_VERSION);
- Serial.println();
- }
- void loop()
- {
- DateTime now = RTC.now();
- //for log
- char dateTimeString[20];
- sprintf (dateTimeString, "%.2d/%.2d/%.2d-%.2d:%.2d:%.2d", now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second());
- String datetime(dateTimeString);
- //for filename
- char dateTimeStringFilename[20];
- sprintf (dateTimeStringFilename, "%.2d%.2d%.2d", now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second());
- String dateTimeStringFilenameString(dateTimeStringFilename);
- // read moisture1 from moisutre sensor
- float moisture = getAverageAnalog(MOISTURESENSOR1,MEASURECOUNT,MEASUREDELAY); // single top left connection, plant 7 (front plant)
- float moisture2 = getAverageAnalog(MOISTURESENSOR2,MEASURECOUNT,MEASUREDELAY); // lower left connection, plant 1 (tub)
- float moisture3 = getAverageAnalog(MOISTURESENSOR3,MEASURECOUNT,MEASUREDELAY); // lower right connection, plant 6 (back-right plant)
- // get brightness from photocell, seems super inacurate
- float photocellReading = getAverageAnalog(photocellPin,100,10);
- // read temperature and air humidity from DHT sensor
- int chk = DHT.read22(DHT22_PIN);
- switch (chk)
- {
- case DHTLIB_OK:
- break;
- case DHTLIB_ERROR_CHECKSUM:
- Serial.print("Checksum error,\t");
- break;
- case DHTLIB_ERROR_TIMEOUT:
- Serial.print("Time out error,\t");
- break;
- default:
- Serial.print("Unknown error,\t");
- break;
- }
- // humidity sensor seems to be very inprecise, use measurecount * 2 !!!!!!!! (still inprecise, but a little better)
- float humidity=0;
- for (int i=0;i<MEASURECOUNT;i++)
- {
- // read and add
- humidity+=DHT.humidity;
- delay(MEASUREDELAY);
- }
- humidity = humidity/(MEASURECOUNT);
- float temperature = 0;
- for (int i=0;i<MEASURECOUNT;i++)
- {
- // read and add
- temperature+=DHT.temperature;
- delay(MEASUREDELAY);
- }
- temperature=temperature/MEASURECOUNT;
- String dataString = datetime+"\t"+humidity+"\t"+temperature+"\t"+moisture+"\t"+moisture2+"\t"+moisture3+"\t"+photocellReading;
- // debug to USB
- Serial.println(dataString);
- // apple numbers need .txt extension to open tab separated files correctly. .csv only for comma separated, eh
- String filename = dateTimeStringFilenameString+".txt";
- char * buffer = new char[filename.length()];
- strcpy(buffer,filename.c_str());
- File dataFile = SD.open(buffer, FILE_WRITE);
- // if the file is available, write to it:
- if (dataFile) {
- dataFile.println(dataString);
- dataFile.close();
- }
- // if the file isn't open, pop up an error:
- else {
- Serial.println("error opening "+filename);
- }
- // new char[] uses malloc
- free(buffer);
- delay(60000); // 900000 = 15 minutes delay 60000=1min
- }
- float getAverageAnalog(int pin, int iterations, int delaySeconds)
- {
- float value=0;
- for (int i=0;i<iterations;i++)
- {
- // read and add
- value+=analogRead(pin);
- delay(delaySeconds);
- }
- return value/iterations;
- }
- //Rounds down (via intermediary integer conversion truncation)
- String doubleToString(double input,int decimalPlaces){
- if(decimalPlaces!=0){
- String string = String((int)(input*pow(10,decimalPlaces)));
- if(abs(input)<1){
- if(input>0)
- string = "0"+string;
- else if(input<0)
- string = string.substring(0,1)+"0"+string.substring(1);
- }
- return string.substring(0,string.length()-decimalPlaces)+"."+string.substring(string.length()-decimalPlaces);
- }
- else {
- return String((int)input);
- }
- }
- //
- // END OF FILE
- //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement