#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DebouncedLDR.h>
#include <ESP32Firebase.h>
#include <RTClib.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
int entry = 0;
RTC_DS3231 rtc;
const int temperaturePin = 35; // Digital pin connected to the temperature sensor
DebouncedLDR::Reading max_reading = 1023; // Maximum possible reading from the LDR
DebouncedLDR::Level max_level = 100; // Maximum possible level from the LDR
DebouncedLDR::Reading noise = 10; // Noise level to ignore
DebouncedLDR ldr(max_reading, max_level, noise);
#define _SSID "Wokwi-GUEST"
#define _PASSWORD ""
#define REFERENCE_URL "https://soil-moisture-b6b6e-default-rtdb.firebaseio.com/"
Firebase firebase(REFERENCE_URL);
void setup() {
Wire.begin(23, 22);
Serial.begin(9600);
lcd.init();
lcd.backlight();
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(1000);
Serial.println();
Serial.println();
Serial.print("Connecting to: ");
Serial.println(_SSID);
WiFi.begin(_SSID, _PASSWORD);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print("-");
}
Serial.println("");
Serial.println("WiFi Connected");
Serial.print("IP Address: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
rtc.begin();
}
void loop() {
int currentTime = rtcfetch();
DebouncedLDR::Reading current_reading = analogRead(32); // Read the current LDR value
ldr.update(current_reading); // Update the state of the debounced LDR
// Implement your own method to convert the LDR reading to lux
float lux = convertToLux(current_reading);
int16_t soilMoisture = analogRead(34); // Read soil moisture sensor
int16_t temperatureValue = analogRead(temperaturePin); // Read temperature sensor from digital pin
// Convert temperature sensor value to temperature in Celsius
float temperatureCelsius = temperatureValue; // Assuming a linear relationship between analog reading and temperature
String moistureStatus = soilMoisture < 2165 ? "WET" : soilMoisture > 3135 ? "DRY" : "OK";
// Determine temperature status based on the reading
String temperatureStatus;
if (temperatureCelsius < 1877) {
temperatureStatus = "HIGH";
} else if (temperatureCelsius > 2213) {
temperatureStatus = "LOW";
} else {
temperatureStatus = "OK";
}
lcd.clear();
lcd.print("Soil: ");
lcd.print(moistureStatus);
lcd.setCursor(0, 1);
lcd.print("Temp: ");
lcd.print(lux);
String path = "/sensors/entry" + String(entry);
firebase.setFloat(path + "/temperature", temperatureValue);
Serial.println("Temperature data exported to Firebase");
firebase.setFloat(path + "/Humidity", soilMoisture);
Serial.println("Humidity data exported to Firebase");
firebase.setFloat(path + "/Light", lux);
Serial.println("Light data exported to Firebase");
firebase.setInt(path + "/timestamp", currentTime);
Serial.println("Date created data exported to Firebase");
Serial.println(currentTime);
entry++;
delay(500);
}
float convertToLux(int raw) {
float voltage = raw * (3.3 / 1023.0); // Convert the raw analog reading (0 to 1023) to a voltage (0 to 5V)
// This is a typical conversion for an LDR, but it may vary depending on your specific sensor
float lux = 12518931 * pow(voltage, -1.405); // Convert voltage to lux
return lux/10/304;
}
int rtcfetch(){
if (rtc.isrunning()) {
// Get current time
DateTime now = rtc.now();
// Convert to Unix epoch timestamp
uint32_t timestamp = now.unixtime();
return timestamp;
} else {
Serial.println("RTC is not running!");
return 0;
}
}