#include <BluetoothSerial.h>
#include <DHT.h>
#include <Wire.h>
#include <RTClib.h>
#include <LiquidCrystal_I2C.h>
#include <FastLED.h>
#define DHTPIN 4 // DHT22 data pin
#define DHTTYPE DHT22
#define RTC_SDA 21 // RTC data pin
#define RTC_SCL 22 // RTC clock pin
#define PHOTO_PIN 34 // Photoresistor pin
#define BUTTON_PIN 32
#define LED_PIN 14
#define NUM_LEDS 18
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
BluetoothSerial SerialBT;
DHT dht(DHTPIN, DHTTYPE);
RTC_DS3231 rtc;
LiquidCrystal_I2C lcd(0x27, 20, 4); // Set the LCD address to 0x27 for a 20x4 display
bool isFahrenheit = false; // Flag to track temperature unit
unsigned long previousMillis = 0;
const long interval = 5000; // Interval to gather data (5 seconds)
const int numDataPoints = 30; // Number of data points to chart
float temperatureData[numDataPoints];
float humidityData[numDataPoints];
int lightData[numDataPoints];
int currentIndex = 0;
void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32_BT"); // Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
// Initialize DHT sensor
dht.begin();
// Initialize RTC
Wire.begin();
rtc.begin();
// Initialize LCD display
lcd.init();
lcd.backlight();
// Initialize LED
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
// Boot-up sequence (blue color)
fill_solid(leds, NUM_LEDS, CRGB(0, 0, 255)); // Blue color
FastLED.show();
// Initialize button
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop() {
// Gather sensor data
if (millis() - previousMillis >= interval) {
previousMillis = millis();
// Read sensor data
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Read time from RTC
DateTime now = rtc.now();
int lightLevel = analogRead(PHOTO_PIN);
// Store data
temperatureData[currentIndex] = temperature;
humidityData[currentIndex] = humidity;
lightData[currentIndex] = lightLevel;
currentIndex++;
if (currentIndex >= numDataPoints) {
currentIndex = 0;
}
}
// Check if the button is pressed
if (digitalRead(BUTTON_PIN) == LOW) {
// Button pressed, toggle temperature unit
isFahrenheit = !isFahrenheit;
delay(200); // Debounce delay
}
// Display sensor data on LCD
displaySensorData();
// Check for low light level
if (analogRead(PHOTO_PIN) < 100) {
// Low light detected, turn LEDs on white
fill_solid(leds, NUM_LEDS, CRGB::White);
} else {
// Normal light, turn LEDs off
fill_solid(leds, NUM_LEDS, CRGB::Black);
}
FastLED.show();
// If Bluetooth is connected, send data over Bluetooth
if (SerialBT.available()) {
sendBluetoothData();
} else {
// After 20 seconds without Bluetooth connection, display data on screen
if (millis() - previousMillis >= 20000) {
displayDataOnScreen();
}
}
}
// Display sensor data function
void displaySensorData() {
lcd.clear();
lcd.setCursor(0, 0);
// Display time and date
DateTime now = rtc.now();
lcd.print(now.month(), DEC);
lcd.print('/');
lcd.print(now.day(), DEC);
lcd.print('/');
lcd.print(now.year(), DEC);
lcd.print(" ");
lcd.print(now.hour(), DEC);
lcd.print(':');
lcd.print(now.minute(), DEC);
lcd.print(':');
lcd.print(now.second(), DEC);
lcd.println();
// Display temperature
lcd.print("Temperature: ");
if (isFahrenheit) {
lcd.print(dht.readTemperature(true)); // Convert to Fahrenheit
lcd.println(" F");
} else {
lcd.print(dht.readTemperature());
lcd.println(" C");
}
// Display humidity
lcd.print("Humidity: ");
lcd.print(dht.readHumidity());
lcd.println(" %");
// Display light level
lcd.print("Light Level: ");
lcd.println(analogRead(PHOTO_PIN));
// Chart sensor data (if needed)
}
// Send data over Bluetooth
void sendBluetoothData() {
SerialBT.print("Time: ");
SerialBT.print(rtc.now().timestamp());
SerialBT.print(", Temperature: ");
SerialBT.print(isFahrenheit ? dht.readTemperature(true) : dht.readTemperature());
SerialBT.print(" ");
SerialBT.print(isFahrenheit ? "F" : "C");
SerialBT.print(", Humidity: ");
SerialBT.print(dht.readHumidity());
SerialBT.print(" %");
SerialBT.print(", Light Level: ");
SerialBT.println(analogRead(PHOTO_PIN));
}
// Display data on screen function
void displayDataOnScreen() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temperature: ");
lcd.print(isFahrenheit ? dht.readTemperature(true) : dht.readTemperature());
lcd.println(isFahrenheit ? " F" : " C");
lcd.print("Humidity: ");
lcd.print(dht.readHumidity());
lcd.println(" %");
lcd.print("Light Level: ");
lcd.println(analogRead(PHOTO_PIN));
}