#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <RTClib.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#define TFT_CS 15
#define TFT_DC 2
#define TFT_RST -1
#define SD_CS 5 // Chip select pin for the microSD card module
#define DHTPIN 4 // Pin which is connected to the DHT22
#define DHTTYPE DHT22 // DHT 22 (AM2302)
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
RTC_DS1307 rtc;
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
Serial.println("Initializing ILI9341 display, RTC module, SD card, and DHT22 sensor...");
// Initialize the ILI9341 display
tft.begin();
tft.setRotation(0); // Rotate the display 180 degrees
tft.fillScreen(ILI9341_BLACK);
// Initialize I2C for RTC
Wire.begin(21, 22); // SDA on GPIO 21, SCL on GPIO 22
// Initialize the RTC
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (!rtc.isrunning()) {
Serial.println("RTC is NOT running, let's set the time!");
// Set the time to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
// Initialize the SD card
if (!SD.begin(SD_CS)) {
Serial.println("Initialization failed!");
tft.setCursor(0, 0);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(2);
tft.println("SD init failed!");
while (1);
}
Serial.println("SD card initialized.");
// Create or open the CSV file and write the header
File dataFile = SD.open("/datalog.csv", FILE_WRITE);
if (dataFile) {
if (dataFile.size() == 0) { // Check if file is empty to write the header
dataFile.println("Date,Time,Temperature,Humidity"); // Write the CSV header
}
dataFile.close();
} else {
Serial.println("Error opening datalog.csv");
}
// Initialize the DHT22 sensor
dht.begin();
// Initial display of time and date
displayTime();
}
void loop() {
displayTime();
delay(1000); // Update the time every second
}
void displayTime() {
DateTime now = rtc.now();
// Read temperature and humidity from DHT22
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Check if any reads failed and exit early (to try again).
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Display the current time
tft.setCursor(0, 0);
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK); // Draw white text on black background to overwrite previous time
tft.setTextSize(2);
tft.printf("Time: %02d:%02d:%02d", now.hour(), now.minute(), now.second());
// Display the current date
tft.setCursor(0, 20);
tft.setTextColor(ILI9341_YELLOW, ILI9341_BLACK); // Draw yellow text on black background to overwrite previous date
tft.setTextSize(2);
tft.printf("Date: %02d/%02d/%d", now.day(), now.month(), now.year());
// Display the temperature
tft.setCursor(0, 40);
tft.setTextColor(ILI9341_CYAN, ILI9341_BLACK);
tft.setTextSize(2);
tft.printf("Temp: %.1fC", temperature);
// Display the humidity
tft.setCursor(0, 60);
tft.setTextColor(ILI9341_GREEN, ILI9341_BLACK);
tft.setTextSize(2);
tft.printf("Humidity: %.1f%%", humidity);
// Log the time, date, temperature, and humidity to the CSV file
logToCSV(now, temperature, humidity);
}
void logToCSV(DateTime now, float temperature, float humidity) {
File dataFile = SD.open("/datalog.csv", FILE_WRITE);
if (dataFile) {
dataFile.printf("%02d/%02d/%d,%02d:%02d:%02d,%.1f,%.1f\n",
now.day(), now.month(), now.year(),
now.hour(), now.minute(), now.second(),
temperature, humidity);
dataFile.close();
Serial.println("Data logged to CSV.");
} else {
Serial.println("Error opening datalog.csv for writing.");
}
}