#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
#define ServoPin 19
#define lightSensorPin 34
DHT dht(DHT_PIN, DHT22);
LiquidCrystal_I2C lcd1(0x27, 20, 4);
LiquidCrystal_I2C lcd2(0x26, 16, 2);
Servo myservo;
// Sensor Soil Moisture FC28 dengan potentiometer
#include "FC28.h"
#define SensorFC28_Pin 34
float soil_moisture;
FC28Sensor mySensor;
const float gama = 0.7;
const float rl10 = 50;
int lightThreshold = 500;
void setup() {
Serial.begin(9600);
Serial.flush();
pinMode(ldrPin, INPUT);
pinMode(DHT_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(relayPin, OUTPUT);
// Inisialisasi Sensor FC28
mySensor.initFC28Sensor(115200, SensorFC28_Pin);
// Inisialisasi RTC
Wire.begin();
if (!rtc.begin()) {
Serial.println("RTC Tidak Terhubung");
lcd1.setCursor(0, 0);
lcd1.print("RTC Tidak Konek");
while (1);
}
// Inisialisasi Komponen
Serial.println("Smart Farming System");
dht.begin();
lcd1.init();
lcd1.backlight();
lcd2.init();
lcd2.backlight();
lcd1.setCursor(0, 0);
lcd1.print("IOT LABORA");
lcd1.setCursor(0, 1);
lcd1.print("Monitoring Sistem");
delay(2000);
lcd1.clear();
// Inisialisasi Servo
myservo.attach(ServoPin);
myservo.write(0); // Posisi awal servo motor
}
void loop() {
// RTC untuk menampilkan waktu
DateTime now = rtc.now();
int tahun = now.year() % 100;
Serial.println("Tanggal: " + String(now.day()) + "/" + String(now.month()) + "/" + String(tahun));
Serial.println("Waktu: " + String(now.hour()) + ":" + String(now.minute()) + ":" + String(now.second()));
// Data Sensor DHT22
float hum = dht.readHumidity();
float temp = dht.readTemperature();
Serial.println("Suhu: " + String(temp, 2) + " °C");
Serial.println("Kelembaban: " + String(hum, 1) + " %");
// Data Sensor LDR
int ldrValue = analogRead(ldrPin);
ldrValue = map(ldrValue, 4095, 0, 1024, 0);
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("Kecerahan: " + String(lux, 3) + " Lux");
String luxmsg = lux < 807 ? "Kurang" : lux > 808 && lux < 1614 ? "Medium" : "Tinggi";
// Data Sensor Soil Moisture FC28
soil_moisture = mySensor.getSoilMoisture();
String soilStatus = soil_moisture < 33.33 ? "Basah" : soil_moisture > 66.66 ? "Kering" : "Normal";
Serial.println("Soil Moisture: " + String(soil_moisture, 2) + " %");
// Kontrol Relay untuk Penyiraman
if (soil_moisture > 66.66) {
digitalWrite(relayPin, HIGH);
myservo.write(180);
} else {
digitalWrite(relayPin, LOW);
myservo.write(0);
}
// Output ke LCD1
lcd1.setCursor(0, 0);
lcd1.print("Tanah: ");
lcd1.print(soilStatus);
lcd1.setCursor(0, 1);
lcd1.print("Kecerahan: ");
lcd1.print(luxmsg);
lcd1.setCursor(0, 2);
lcd1.print("Suhu: ");
lcd1.print(temp);
lcd1.print(" C");
lcd1.setCursor(0, 3);
lcd1.print("Kelembaban: ");
lcd1.print(hum);
lcd1.print(" %");
// Data Sensor Cahaya & Servo
int lightValue = analogRead(lightSensorPin);
lcd2.setCursor(0, 0);
lcd2.print("Cahaya: ");
lcd2.print(lightValue);
lcd2.setCursor(0, 1);
lcd2.print("Status: ");
if (lightValue < lightThreshold) {
lcd2.print("Gelap");
myservo.write(180); // Penyiraman jika cahaya rendah
delay(3000);
myservo.write(0);
} else {
lcd2.print("Terang");
}
Serial.println("--------------");
delay(2000);
lcd1.clear();
lcd2.clear();
}