#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>
#define oneWireBus 4
// Define relay pins
#define RELAY1_PIN 26
#define RELAY2_PIN 27
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(oneWireBus);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensor(&oneWire);
// Setup LCD 16x2
LiquidCrystal_I2C LCD(0x27, 16, 2);
// Setup Servo
Servo myServo;
int servoPin = 25; // Connect the servo signal pin to this pin
void setup() {
Serial.begin(115200);
sensor.begin();
LCD.init();
LCD.backlight();
LCD.begin(16, 2);
// Display initialization message
LCD.setCursor(0, 0);
LCD.print("Sistem IoT");
LCD.setCursor(0, 1);
LCD.print("Kolam LAT");
delay(3000); // 3-second delay to show the message
// Clear the LCD after showing the message
LCD.clear();
delay(100); // Short delay to ensure clear
// Attach the servo on pin 25
myServo.attach(servoPin);
// Initialize relay pins
pinMode(RELAY1_PIN, OUTPUT);
pinMode(RELAY2_PIN, OUTPUT);
// Start with relays off
digitalWrite(RELAY1_PIN, LOW);
digitalWrite(RELAY2_PIN, LOW);
}
void loop() {
sensor.requestTemperatures();
float temperatureC = sensor.getTempCByIndex(0);
// Placeholder for pH value, replace with actual sensor reading
float pHValue = 8.0; // Example pH value, change this for testing
// Placeholder for voltage value, replace with actual voltage reading
float voltage = 3.3; // Example voltage value, change this for testing
// Create a buffer to store the formatted strings
char tempString[16];
char phString[16];
// Format the temperature and servo status as a string
snprintf(tempString, sizeof(tempString), "Suhu: %.2fC ", temperatureC); // Added spaces for separation
// Format the pH value and voltage as a string
snprintf(phString, sizeof(phString), "pH: %.2f V:%.1f", pHValue, voltage);
// Print the temperature and servo status on the LCD
LCD.setCursor(0, 0);
LCD.print(tempString);
// Move servo from 0 to 180 degrees
myServo.write(0);
LCD.setCursor(12, 0); // Position to display servo status next to temperature
LCD.print("Move"); // "Move" for Active
delay(2000); // 2-second delay
LCD.setCursor(12, 0); // Position to clear servo status
LCD.print(" "); // Clear status
delay(100); // Short delay to ensure clear
myServo.write(180);
delay(2000); // 2-second delay
LCD.setCursor(12, 0); // Position to display servo status next to temperature
LCD.print("Back"); // "Back" for Active
delay(2000); // 2-second delay
LCD.setCursor(12, 0); // Position to clear servo status
LCD.print(" "); // Clear status
delay(100); // Short delay to ensure clear
// Print the pH value and voltage on the LCD
LCD.setCursor(0, 1);
LCD.print(phString);
// Control relays based on pH value
if (pHValue > 7.0) {
digitalWrite(RELAY1_PIN, HIGH); // Turn on blue LED
digitalWrite(RELAY2_PIN, LOW); // Turn off red LED
} else if (pHValue < 7.0) {
digitalWrite(RELAY1_PIN, LOW); // Turn off blue LED
digitalWrite(RELAY2_PIN, HIGH); // Turn on red LED
} else {
digitalWrite(RELAY1_PIN, LOW); // Turn off both LEDs if pH is exactly 7
digitalWrite(RELAY2_PIN, LOW);
}
delay(2000); // 2-second delay
}