0% found this document useful (0 votes)
19 views3 pages

Code 7

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 3

#include <Wire.

h>
#include <RTClib.h>
#include <Time.h>
#include <DHT.h>
#include <SPI.h>
#include <SD.h>
#include <Adafruit_I2CDevice.h>
#include <Adafruit_Sensor.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);

#define DHTPIN 9
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

float humidity; //Stores humidity value


float temperature; //Stores temperature value

const int chipSelect = 10; //10 is default by shield, but normally on Pin 4
int interval = 5; //Log to SD Card every 5 seconds

long timer;
String timestring;
String mvalue;

RTC_DS3231 rtc;
char daysofTheWeek[7] [12] = {"Sunday" , "Monday" , "Tuesday" , "Wednesday" ,
"Thursday" , "Friday" , "Saturday"};

void setup () {
Serial.begin(9600);
delay(3000);
Serial.println("Initializing SD card...");
lcd.init();
lcd.backlight();
lcd.begin(16, 2);
dht.begin();
Wire.begin();

#ifndef ESP8266
while (!Serial) ;
#endif

// Initialize the RTC


rtc.begin();

// Initialize the SD card


if (!SD.begin(chipSelect)) {
Serial.println("SD Card error");
return;
}
Serial.println("card initialized");
if (! rtc.begin()) {
Serial.println("No RTC found");
} else {
Serial.println("RTC clock found");
}

if (! rtc.isrunning()) {
Serial.println("RTC is not configured");
}

rtc.adjust (DateTime(2023, 5, 6 , 20, 18, 0));

if(rtc.lostPower()){
Serial.println("RTC Lost Power, default time will be used.");
}

void loop () {
temperature = dht.readTemperature();
humidity = dht.readHumidity();

if ((timer + interval * 1000) < millis()) {


timer = millis();
get_logvalue(); //Get your value
get_time(); //Get time from RTC
write_data(); //Write value and Time to SD
}
}

{
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print(" C");
lcd.setCursor(0, 1);
lcd.print("Hum: ");
lcd.print(humidity);
lcd.print(" %");

if (temperature <37.1 ) {
digitalWrite(relayPin, LOW);
digitalWrite(greenLED, LOW);
digitalWrite(redLED, HIGH);
}

else if (temperature > 37.7) {


digitalWrite(relayPin, HIGH);
digitalWrite(greenLED, LOW);
digitalWrite(redLED, HIGH);
}

else {
digitalWrite(greenLED, HIGH);
digitalWrite(redLED, LOW);
}
}
void get_logvalue() {
mvalue = "My Value"; //mvalue is your log parameter eg. Temperature
}

void get_time(){ //Read Time from RTC


DateTime now = rtc.now();
timestring = now.day();
timestring += "-";
timestring += now.month();
timestring += "-";
timestring += now.year();
timestring += " ";
timestring += now.hour();
timestring += ":";
timestring += now.minute();
timestring += ":";
timestring += now.second();
Serial.println(timestring);
Serial.print(" Temperature: ");
Serial.print(temperature);
Serial.print(" Humidity: ");
Serial.print(humidity);
Serial.println();

void write_data() { //Write to SD card


String dataString = mvalue + "," + timestring;
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
Serial.println(dataString);
}
else {
Serial.println("error writing datalog.txt");
}

delay (600000);
}

You might also like