Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <JeeLib.h>
- //#include <Narcoleptic.h>
- #include "Wire.h"
- ISR(WDT_vect) { Sleepy::watchdogEvent(); }
- /// Karta SD ////
- #include <SD.h>
- #define KARTAPIN 6
- File myFile;
- /// Czujnik wilgotności i temperatury powietrza ////
- #include <dht.h>
- dht DHT22;
- #define DHT22PIN 7
- /// Czujnik światła ///
- #include <BH1750.h>
- BH1750 czujnikSwiatla;
- /// Czujnik wilgotności gleby ///
- int sensor_A0 = A0; // podłączenie od A0 na czujniku do A0 na Arduino
- int sensor_D0 = 2; // podłączenie od D0 na czujniku do pinu 2 na Arduino
- int wartosc_A0;
- /// sonda temperatury ///
- #include <OneWire.h>
- #include <DallasTemperature.h>
- #define sondaPIN 16
- float tmp=0;
- OneWire oneWire(sondaPIN);
- DallasTemperature sensors(&oneWire);
- /// RTC ///
- #define DS3231_I2C_ADDRESS 0x68
- byte second, minute, hour, dayOfWeek,dayOfMonth, month, year;
- char combinedArray[13];
- byte lastday=99;
- //////////////////////////////////////////////////////////////////////////////////
- void setup()
- {
- //Serial.begin(9600);
- Wire.begin();
- czujnikSwiatla.begin();
- sensors.begin();
- pinMode(KARTAPIN, OUTPUT);
- pinMode(10, INPUT);
- if (!SD.begin(KARTAPIN))
- {
- Serial.print("Error SD");
- return;
- }
- delay(3000);
- }
- void loop()
- {
- readDS3231time(&second, &minute, &hour,&dayOfWeek, &dayOfMonth, &month, &year);
- char msc[3];
- char dzien[3];
- char rok[5];
- snprintf(msc, 3, "%d", month); //char
- snprintf(dzien, 3, "%d", dayOfMonth);
- snprintf(rok, 5, "%d", year);
- combinedArray[0]=dzien[0];
- combinedArray[1]=dzien[1];
- combinedArray[2]='_';
- if(month<10)
- {
- combinedArray[3]='0';
- combinedArray[4]=msc[0];
- }
- else
- {
- combinedArray[3]=msc[0];
- combinedArray[4]=msc[1];
- }
- combinedArray[5]='_';
- combinedArray[6]=rok[0];
- combinedArray[7]=rok[1];
- combinedArray[8]='.';
- combinedArray[9]='c';
- combinedArray[10]='s';
- combinedArray[11]='v';
- myFile = SD.open(combinedArray,FILE_WRITE);
- if(dayOfWeek!=lastday)
- myFile.println("data;godzina;wilgotnosc powietrza [%];temperatura powietrza [C];wilgotnosc gleby [%];wartosc A0;temperatura gleby [C];natezenie swiatla [lux]");
- lastday=dayOfWeek;
- data();
- delay(10);
- czujDHT22();
- czujWILGOT();
- sondaTEM();
- czujSWIATLA();
- myFile.close();
- for(int i=0;i<30;i++){Sleepy::loseSomeTime(60000);}
- }
- //////////////////////////////// RTC////////////////////////////////////////
- String readDS3231time(byte *second,
- byte *minute,
- byte *hour,
- byte *dayOfWeek,
- byte *dayOfMonth,
- byte *month,
- byte *year)
- {
- Wire.beginTransmission(DS3231_I2C_ADDRESS);
- Wire.write(0); // set DS3231 register pointer to 00h
- Wire.endTransmission();
- Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
- // request seven bytes of data from DS3231 starting from register 00h
- *second = bcdToDec(Wire.read() & 0x7f);
- *minute = bcdToDec(Wire.read());
- *hour = bcdToDec(Wire.read() & 0x3f);
- *dayOfWeek = bcdToDec(Wire.read());
- *dayOfMonth = bcdToDec(Wire.read());
- *month = bcdToDec(Wire.read());
- *year = bcdToDec(Wire.read());
- }
- void data()
- {
- myFile.print(year, DEC);
- myFile.print("/");
- if(month<10) myFile.print("0");
- myFile.print(month, DEC);
- myFile.print("/");
- myFile.print(dayOfMonth, DEC);
- myFile.print(" ");
- myFile.print(";");
- myFile.print(hour, DEC);
- myFile.print(":");
- myFile.print(minute, DEC);
- myFile.print(":");
- myFile.print(second, DEC);
- myFile.print(" ");
- myFile.print(";");
- }
- byte decToBcd(byte val)
- {
- return( (val/10*16) + (val%10) );
- }
- byte bcdToDec(byte val)
- {
- return( (val/16*10) + (val%16) );
- }
- void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek , byte
- dayOfMonth, byte month, byte year)
- {
- // sets time and date data to DS3231
- Wire.beginTransmission(DS3231_I2C_ADDRESS);
- Wire.write(0); // set next input to start at the seconds register
- Wire.write(decToBcd(second)); // set seconds
- Wire.write(decToBcd(minute)); // set minutes
- Wire.write(decToBcd(hour)); // set hours
- Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
- Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
- Wire.write(decToBcd(month)); // set month
- Wire.write(decToBcd(year)); // set year (0 to 99)
- Wire.endTransmission();
- }
- ////////////////////////// Czujnik wilgotności i temperatury powietrza //////////////////////////////////
- void czujDHT22()
- {
- int chk = DHT22.read(DHT22PIN); //sprawdzenie stanu sensora, a następnie wyświetlenie komunikatu na monitorze szeregowym
- switch (chk)
- {
- case DHTLIB_OK:
- break;
- case DHTLIB_ERROR_CHECKSUM:
- myFile.println(" ");
- myFile.println("Błąd sumy kontrolnej");
- myFile.println(" ");
- break;
- case DHTLIB_ERROR_TIMEOUT:
- myFile.println(" ");
- myFile.println("Koniec czasu oczekiwania - brak odpowiedzi");
- myFile.println(" ");
- break;
- default:
- myFile.println(" ");
- myFile.println("Nieznany błąd");
- myFile.println(" ");
- break;
- }
- myFile.print((float)DHT22.humidity, 2);
- myFile.print(";");
- myFile.print((float)DHT22.temperature, 2);
- myFile.print(";");
- }
- ////////////////////////////// Czujnik wilgotności gleby /////////////////////////
- void czujWILGOT()
- {
- wartosc_A0 = analogRead(sensor_A0); // pobranie wartości z A0
- int sensorConstrainl=constrain(wartosc_A0,300 ,1022);
- int soill = map(sensorConstrainl,300 ,1022,100,0);
- myFile.print(soill);
- myFile.print(";");
- myFile.print(wartosc_A0);
- myFile.print(";");
- }
- /////////////////////////////// Sonda temeratury /////////////////////////
- void sondaTEM()
- {
- tmp =(sensors.getTempCByIndex(0));
- myFile.print(tmp);
- sensors.requestTemperatures(); // Send the command to get temperature
- myFile.print(";");
- }
- ///////////////////////////////// Czujnik natezenia swiatla ///////////////////////////////
- void czujSWIATLA()
- {
- uint16_t lux = czujnikSwiatla.readLightLevel(); //odczytanie wartości z czujnika
- myFile.println(lux);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement