#define BLYNK_TEMPLATE_ID "TMPL6lteeJxCG"
#define BLYNK_TEMPLATE_NAME "DETEKSI KELEMBABAN"
#define BLYNK_AUTH_TOKEN "mrX-qKXbzriq34tlM38QvQAGjyoFrP0Y"
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <LiquidCrystal_I2C.h>//include library code
// ************************************************************
LiquidCrystal_I2C lcd(0x27,16,2);
//************************************************************
// Gantilah dengan SSID dan password WiFi Anda
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
const int analogPin = 35; // Soil moisture sensor attached to analog pin A0
const int relayPin = 4; // 5v relay module attached to digital pin 4
int thresholdValue = 40; // Threshold value set to 40%
bool motorState = false;
int status = WL_IDLE_STATUS;
BLYNK_WRITE(V1) {
motorState = param.asInt();
motorState = !motorState;
if (motorState == LOW) {
Serial.println("-------------------------");
Serial.print("Button Blynk: ");
Serial.println(motorState);
Serial.println("Pompa Nyala...");
Blynk.virtualWrite(V1, HIGH);
Serial.println("-------------------------");
} else {
Serial.println("-------------------------");
Serial.print("Button Blynk: ");
Serial.println(motorState);
Serial.println("Pompa Mati...");
Blynk.virtualWrite(V1, LOW);
Serial.println("-------------------------");
}
}
void setup() {
pinMode(relayPin, OUTPUT); // Sets the relayPin as OUTPUT
pinMode(analogPin, INPUT); // Sets the relayPin as OUTPUT
// digitalWrite(relayPin, HIGH); // Water pump is off by default
Serial.begin(9600); // Initializes the serial communication at 9600 bps
lcd.init ();//Define the LCD as 16 column by 2 rows
//lcd.setBacklightPin(3, POSITIVE);//Switch on the backlight
lcd.backlight();
lcd.setCursor(0, 0); //goto first column (column 0) and second line (line 0)
lcd.print(" Automatic ");//Print at cursor Location
lcd.setCursor(0, 1); //goto first column (column 0) and second line (line 1)
lcd.print(" Garden System ");//Print at cursor Location
delay(2000); //sets delay for 2 seconds
lcd.clear();//clears the LCD screen
lcd.setCursor(0, 0); //goto first column (column 0) and second line (line 0)
lcd.print("Analyze Water...");//Print at cursor Location
delay(3000); //sets delay for 2 seconds
lcd.clear();//clears the LCD screen
lcd.setCursor(0, 0); //goto first column (column 0) and second line (line 0)
lcd.print("I Need Water!!!");//Print at cursor Location
delay(3000); //sets delay for 2 seconds
lcd.clear();//clears the LCD screen
// Memulai komunikasi serial
// delay(2000); ? // Sets delay for 2 seconds
// Serial.begin(9600);
// while (!Serial) {
// ; // Tunggu hingga port serial terhubung
// }
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
Serial.println("Terhubung ke jaringan WiFi");
printWiFiStatus();
}
void loop() {
lcd.clear();//clears the LCD screen
Blynk.run();
int analogValue = analogRead(analogPin);
int moisturedata = map(analogValue, 0, 4095, 100, 0); // Maps sensor value to percentage
Serial.print("Moisture: ");
Serial.print(moisturedata);
Serial.println("%");
lcd.setCursor(0, 0);
lcd.print("Pump");
if (moisturedata < thresholdValue) {
Serial.print(" - Time to water your plant");
Serial.println(" - Water pump is on");
digitalWrite(relayPin, LOW); // Turn on the water
lcd.setCursor(12, 1);
lcd.print("OFF");
lcd.setCursor(0, 1);
lcd.print("Plant:");
lcd.setCursor(6, 1);
lcd.print("OK");
Blynk.virtualWrite(V1, HIGH);
delay(1000);
} else {
Serial.print(" - The plant doesn't need watering");
Serial.println(" - Water pump is off");
digitalWrite(relayPin, HIGH); // Turn off the water pump
lcd.setCursor(12, 1);
lcd.print("ON");
lcd.setCursor(0, 1);
lcd.print("Plant:");
lcd.setCursor(6, 1);
lcd.print("DRY");
Blynk.virtualWrite(V1, LOW);
delay(1000);
}
Blynk.virtualWrite(V0, moisturedata);
delay(200);
}
void printWiFiStatus() {
// Cetak SSID jaringan yang tersambung
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// Cetak alamat IP perangkat
IPAddress ip = WiFi.localIP();
Serial.print("Alamat IP: ");
Serial.println(ip);
}