// Final Project EdSpert Bootcamp IoT
// Judul : Smart IoT Farming Automation and Monitoring System
// Kelompok : 5
// Referensi kebutuhan cahaya tanaman :
// https://sustainablecampus.unimelb.edu.au/__data/assets/pdf_file/0005/2839190/Indoor-plant-workshop-Light-and-Moisture-Requirements.pdf
#include <WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
#include "LiquidCrystal_I2C.h"
#include "DHT.h"
#include <ESP32Servo.h>
#include <RTClib.h>
RTC_DS1307 rtc;
#define relayPin 18
#define ldrPin 35
#define DHT_PIN 15
#define DHTTYPE DHT22
#define LED_PIN 33
DHT dht(DHT_PIN, DHT22);
LiquidCrystal_I2C lcd(0x27, 20, 4);
LiquidCrystal_I2C lcd2(0x27, 20, 4);
// Representation of the FC28 sensor using a potentiometer
#include "FC28.h"
#define SensorFC28_Pin 34
float soil_moisture;
FC28Sensor mySensor;
const float gama = 0.7;
const float rl10 = 50;
void setup() {
Serial.begin(9600);
Serial.flush();
pinMode(ldrPin, INPUT);
pinMode(DHT_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(relayPin, OUTPUT);
mySensor.initFC28Sensor(115200, SensorFC28_Pin);
Wire.begin();
if (!rtc.begin()) {
Serial.println("RTC Not Connected");
lcd.setCursor(0, 0);
lcd.print("RTC Unable to Connect");
while (1);
}
Serial.println("Smart Farming System");
dht.begin();
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("IOT ");
lcd.setCursor(0, 1);
lcd.print("Project ");
lcd.setCursor(0, 2);
lcd.print("Smart Plant");
lcd.setCursor(0, 3);
lcd.print("Monitoring System");
delay(2000);
lcd.clear();
}
void loop() {
// RTC serial print to display time
DateTime now = rtc.now();
int year = now.year() % 100;
Serial.println("Date : " + String() + now.day() + "/" + now.month() + "/" + year);
Serial.println("Time : " + String() + now.hour() + ":" + now.minute() + ":" + now.second());
// DHT22 sensor data
float hum = dht.readHumidity();
float temp = dht.readTemperature();
Serial.println("Temperature : " + String(temp, 2) + " °C");
Serial.println("Humidity : " + String(hum, 1) + " %");
// LDR sensor data
int ldrValue = analogRead(ldrPin);
ldrValue = map(ldrValue, 4095, 0, 1024, 0); // Convert LDR sensor reading from Arduino ADC value to ESP32 ADC value
float ldrVoltage = ldrValue / 1024.0 * 5;
float ldrResistance = 2000 * ldrVoltage / (1 - ldrVoltage / 5);
float lux = pow(rl10 * 1e3 * pow(10, gama) / ldrResistance, (1 / gama));
Serial.println("Brightness : " + String(lux, 3) + " Lux");
String luxmsg = lux < 807 ? "Low" : (lux > 808 && lux < 1614 ? "Medium" : "High");
// FC28 Soil Moisture Sensor data
soil_moisture = mySensor.getSoilMoisture();
String msg = soil_moisture < 33.33 ? "Wet" : (soil_moisture > 66.66 ? "Dry" : "Normal");
Serial.println("Soil Moisture Value : " + String(soil_moisture, 2) + " %");
// Relay to control water supply to plants based on soil moisture condition
if (soil_moisture > 66.66) {
digitalWrite(relayPin, HIGH);
} else {
digitalWrite(relayPin, LOW);
}
// LCD output for monitoring plant conditions
// Soil Condition
lcd.setCursor(0, 0);
lcd.print("Soil :");
lcd.setCursor(13, 0);
lcd.print(msg);
// Brightness Condition and LED On/Off
if (lux < 807) {
digitalWrite(LED_PIN, HIGH);
lcd.setCursor(0, 1);
lcd.print("Brightness : ");
lcd.setCursor(13, 1);
lcd.print(luxmsg);
} else if (lux > 808 && lux < 1614) {
digitalWrite(LED_PIN, LOW);
lcd.setCursor(0, 1);
lcd.print("Brightness : ");
lcd.setCursor(13, 1);
lcd.print(luxmsg);
} else {
digitalWrite(LED_PIN, LOW);
lcd.setCursor(0, 1);
lcd.print("Brightness : ");
lcd.setCursor(13, 1);
lcd.print(luxmsg);
}
// Temperature
lcd.setCursor(0, 2);
lcd.print("Temperature :");
lcd.setCursor(13, 2);
lcd.print(temp);
lcd.setCursor(19, 2);
lcd.print("C");
// Humidity
lcd.setCursor(0, 3);
lcd.print("Humidity :");
lcd.setCursor(13, 3);
lcd.print(hum);
lcd.setCursor(19, 3);
lcd.print("%");
Serial.println("--------------");
delay(2000);
lcd.clear();
}