/*
File : Kesuburan Tanah R0
Author : leveletech.com / wa 081214584114
Date : 2024 09 12
Description : Alat Ukur Kesuburan Tanah
*/
//--------------------------------- Include Libraries-----------------------------------------------
#include <LiquidCrystal_I2C.h>
#include "DHT.h"
//--------------------------------- Include Libraries-----------------------------------------------
//--------------------------------- Hardware Setting------------------------------------------------
//--------------------------------- Hardware Setting------------------------------------------------
//--------------------------------- Define and Constant---------------------------------------------
//Blynk
#define BLYNK_TEMPLATE_ID "TMPL6r7cK5BIJ"
#define BLYNK_TEMPLATE_NAME "Pengukur Kesuburan Tanah"
#define BLYNK_AUTH_TOKEN "RA_FHoDZkZ3mqu9nhXWqDTqEEbnhSJoG"
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
//#include <ESP8266WiFi.h>
//#include <BlynkSimpleEsp8266.h>
char auth[] = BLYNK_AUTH_TOKEN; //Auth Token
char ssid[] = "Wokwi-GUEST"; //nama hotspot yang digunakan
char pass[] = ""; //password hotspot yang digunakan
//ESP32
const int DHT11_pin = 4;
const int Analog_pin = 34;
const int PH_en_pin = 17;
const int Soil_en_pin = 16;
const int Relay_pin = 2;
//ESP8266
//const int DHT11_pin = 13; //ok
//const int Analog_pin = A0; //ok
//const int PH_en_pin = 14; //ok D5
//const int Soil_en_pin = 12; //ok D6
//const int Relay_pin = 15; //OK D8
//--------------------------------- Define and Constant---------------------------------------------
//--------------------------------- User Define Data Type-------------------------------------------
//--------------------------------- User Define Data Type-------------------------------------------
//----------------------------------Global Variable-------------------------------------------------
bool B[8] = {false, false, false, false, false, false, false, false};
/* B map
B[0] = auto / manual relay
B[1] = relay status
B[2] = interlock relay
B[3] =
B[4] =
*/
//Blynk
int value0;
//lcd
LiquidCrystal_I2C lcd(0x27, 16, 2);
//DHT11
float humidity, temperature;
DHT dht(DHT11_pin, DHT22); //simulation using DHT22
//PH tanah
float PH;
float PH_volt;
float PH_offset = 41.02740741;
float PH_k = -19.18518519;
//Soil tanah
float Soil;
float Soil_offset = 0.0;
float Soil_k = 1.0;
float Soil_volt;
//----------------------------------Global Variable-------------------------------------------------
//----------------------------------User Defined Function Stage 1-----------------------------------
BLYNK_WRITE(V3) {
value0 = param.asInt(); B[0] = value0;
}
BLYNK_WRITE(V4) {
if (B[0]) {
value0 = param.asInt(); B[1] = value0;
}
}
String format1(float data, String unit, int lengt) {
String formattedData = String(data, 0); // Convert float to String, no decimal places
formattedData += " " + unit; // Add space and unit
// Ensure the result is exactly lengt characters long by padding spaces if needed
while (formattedData.length() < lengt) {
formattedData = " " + formattedData;
}
return formattedData;
}
String format2(int data, String unit, int lengt) {
String formattedData = String(data); // Convert float to String, no decimal places
formattedData += " " + unit; // Add space and unit
// Ensure the result is exactly lengt characters long by padding spaces if needed
while (formattedData.length() < lengt) {
formattedData = " " + formattedData;
}
return formattedData;
}
//----------------------------------User Defined Function Stage 1-----------------------------------
void DHT11_update() {
humidity = dht.readHumidity();
temperature = dht.readTemperature();
}
void Analog_update() {
//update PH Sensor
digitalWrite(PH_en_pin, HIGH);
digitalWrite(Soil_en_pin, LOW);
delay(100);
int val1 = analogRead(Analog_pin); //get raw data
delay(100);
//do calibration below
PH_volt = val1 * (3.3 / 1024.0);
PH = (PH_volt * PH_k) + PH_offset;
//update Soil Sensor
digitalWrite(PH_en_pin, LOW);
digitalWrite(Soil_en_pin, HIGH);
delay(100);
int val2 = analogRead(Analog_pin); //get raw data
delay(100);
//do calibration below
Soil_volt = val2 * (3.3 / 1024.0);
Soil = (Soil_volt * Soil_k) + Soil_offset;
}
void lcd_update() {
lcd.setCursor(0, 0);
lcd.print("T=");
lcd.setCursor(8, 0);
lcd.print("H=");
lcd.setCursor(2, 0);
lcd.print(format1(temperature, "C", 5));
lcd.setCursor(10, 0);
lcd.print(format1(humidity, "%", 5));
lcd.setCursor(0, 1);
lcd.print("Ph=");
lcd.setCursor(3, 1);
lcd.print(format1(PH, "ph", 3));
lcd.setCursor(8, 1);
lcd.print("S=");
lcd.setCursor(10, 1);
lcd.print(format1(Soil, "ph", 3));
}
void blynk_update() {
Blynk.virtualWrite(V0, temperature);
Blynk.virtualWrite(V1, humidity);
Blynk.virtualWrite(V2, PH);
}
void RelayControl_update() {
digitalWrite(Relay_pin, ((!B[0] & B[2]) || (B[0] & B[1])));
}
//----------------------------------User Defined Function Stage 2-----------------------------------
//----------------------------------User Defined Function Stage 2-----------------------------------
//----------------------------------User Defined Function Stage 3-----------------------------------
//----------------------------------User Defined Function Stage 3-----------------------------------
//----------------------------------Testing Function------------------------------------------------
void testing() {
//write everyting for testing
if (0) {
Serial.print(B[0]);
Serial.print(" ");
Serial.print(B[1]);
Serial.print(" ");
Serial.print(B[2]);
Serial.print(" ");
Serial.println(digitalRead(Relay_pin));
}
if (1) {
Serial.print("PH volt : ");
Serial.print(PH_volt);
Serial.print(" PH : ");
Serial.println(PH);
Serial.print("Soil volt : ");
Serial.print(Soil_volt);
Serial.print(" Soil : ");
Serial.println(Soil);
}
}
//----------------------------------Testing Function------------------------------------------------
//--------------------------------- Setup----------------------------------------------
void setup() {
//Pin Initialization
pinMode(PH_en_pin, OUTPUT);
pinMode(Soil_en_pin, OUTPUT);
pinMode(Relay_pin, OUTPUT);
//Start Services
dht.begin();
lcd.init();
lcd.backlight(); // turn on LCD backlight
//serial
Serial.begin(9600);
delay(1000);
//first run
//TAMPIL LCD
lcd.setCursor(0, 0);
lcd.print("Kesuburan Tanah");
lcd.setCursor(0, 1);
lcd.print(" SKRIPSI 2024");
delay(2000);
lcd.clear();
//Blynk
Blynk.begin(auth, ssid, pass); //memulai Blynk
delay(2000);
}
//--------------------------------- Setup----------------------------------------------
//--------------------------------- Loop-----------------------------------------------
void loop() {
Blynk.run();
DHT11_update();
Analog_update();
RelayControl_update();
lcd_update();
blynk_update();
testing();
}
//--------------------------------- Loop-----------------------------------------------
//--------------------------------- Note-----------------------------------------------
/*
*/
//--------------------------------- Note-----------------------------------------------
PH sensor
Soil Sensor