//**********BLYNK, identitas device*****************************
#define BLYNK_TEMPLATE_ID "TMPL69kEQwPBP"
#define BLYNK_TEMPLATE_NAME "Self Watering System ESP32 and Soil Moisture"
#define BLYNK_AUTH_TOKEN "iFBv4P2fzFR5mXdUeb74u-H5Dh9s0jZT"
//**********BLYNK, library**************************************
#include <BlynkSimpleEsp32.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>
//**********BLYNK, ssid, pass, token****************************
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
// Deklarasi PIN yang akan digunakan
#define SDA_PIN 18
#define SCL_PIN 19
#define SOIL_SENSOR_PIN 34
#define LED_RED_PIN 2
#define LED_BLUE_PIN 4
#define SERVO_PIN 15
#define SENSOR_POWER_PIN 21
BlynkTimer timer;
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo servo;
void setup() {
// Kode ini akan dijalankan sekali
// Setting pin led menjadi output
pinMode(LED_RED_PIN, OUTPUT);
pinMode(LED_BLUE_PIN, OUTPUT);
pinMode(SENSOR_POWER_PIN, OUTPUT);
// Inisialisasi untuk memastikan sensor OFF
digitalWrite(SENSOR_POWER_PIN, LOW);
// Setting pin LCD
Wire.begin(SDA_PIN, SCL_PIN);
Serial.begin(115200);
Serial.println("Hello, start configuration!");
// Setting servo pin
servo.attach(SERVO_PIN);
servo.write(0);
// Inisialisasi lcd
lcd.init();
lcd.backlight();
//********BLYNK, mulai koneksi ke blynk***********************
Blynk.begin(auth, ssid, pass);
timer.setInterval(2500L, sendSensor);
}
//*********BLYNK, syncron data terakhir saat koneksi tersambung*
BLYNK_CONNECTED() {
Blynk.syncAll();
}
//**********BLYNK, mendapat input dr blynk lewat datastream V0**
BLYNK_WRITE(V0) {
int saklar=param.asInt();
digitalWrite(SENSOR_POWER_PIN, saklar);
}
String createMessage() {
int16_t moisture = analogRead(SOIL_SENSOR_PIN);
String msg = "";
if(moisture < 2165) {
msg = "BASAH";
digitalWrite(LED_RED_PIN, LOW);
digitalWrite(LED_BLUE_PIN, LOW);
servo.write(0);
Blynk.virtualWrite(V2, "Air terlalu banyak, kurangi air");
} else if( moisture > 3135) {
msg = "KERING";
digitalWrite(LED_RED_PIN, HIGH);
digitalWrite(LED_BLUE_PIN, LOW);
servo.write(90);
Blynk.virtualWrite(V2, "Air kering, tambahkan air");
} else {
msg = "BAIK";
digitalWrite(LED_RED_PIN, LOW);
digitalWrite(LED_BLUE_PIN, HIGH);
servo.write(0);
Blynk.virtualWrite(V2, "Air dalam kondisi baik");
}
return msg;
}
void sendSensor() {
int activeSensor = digitalRead(SENSOR_POWER_PIN);
String message = "";
if(activeSensor == 1) {
message = createMessage();
Serial.println("Keadaan: "+message);
lcd.clear();
lcd.print("Soil: ");
lcd.print(message);
Blynk.virtualWrite(V1, message);
} else {
message = "Sensor Mati";
lcd.clear();
lcd.print(message);
Blynk.virtualWrite(V1, message);
Blynk.virtualWrite(V2, message);
digitalWrite(LED_RED_PIN, HIGH);
digitalWrite(LED_BLUE_PIN, LOW);
servo.write(0);
delay(500);
digitalWrite(LED_RED_PIN, LOW);
}
delay(1000);
}
void loop() {
//********BLYNK, menjalankan blynk****************************
Blynk.run();
timer.run();
sendSensor();
}