#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Pin setup
const int redLedPin = 32;
const int yellowLedPin = 33;
const int greenLedPin = 25;
const int buzzerPin = 26;
// Wi-Fi credentials
const char* ssid = "Wokwi-GUEST"; // Ganti dengan SSID Wi-Fi Anda
const char* password = ""; // Ganti dengan password Wi-Fi Anda
// Telegram bot setup
const char* botToken = "7981731725:AAEkpyVt0Xm185WwXFgz-g291wxhzwn0ZJ8"; // Ganti dengan Token Bot Anda
const char* chat_id = "7411136853"; // Ganti dengan ID chat Anda
WiFiClientSecure client;
UniversalTelegramBot bot(botToken, client);
unsigned long lastNotificationTime = 0;
unsigned long lastTimeBotRan = 0;
const int botInterval = 1000; // Interval cek pesan bot
const int notificationInterval = 60000; // Interval notifikasi (1 menit)
String lastStatus = ""; // Untuk menyimpan status kelembapan sebelumnya
void setup() {
Wire.begin(23, 22);
Serial.begin(9600);
lcd.init();
lcd.backlight();
pinMode(redLedPin, OUTPUT);
pinMode(yellowLedPin, OUTPUT);
pinMode(greenLedPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
// Wi-Fi setup
Serial.print("Connecting to Wi-Fi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("Connected to Wi-Fi!");
// Telegram client configuration
client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
}
void sendNotification(String status, float humidity) {
String message = "Status Kelembapan Tanah: " + status + "\n";
message += "Presentasi: " + String(humidity) + "%";
bot.sendMessage(chat_id, message, "");
}
void handleNewMessages(int numNewMessages) {
for (int i = 0; i < numNewMessages; i++) {
String chat_id = bot.messages[i].chat_id;
String text = bot.messages[i].text;
Serial.println("New message: " + text);
if (text == "/status") {
int16_t sensorValue = analogRead(34);
float humidityPercent = map(sensorValue, 2165, 3135, 0, 100);
String status = (humidityPercent < 40) ? "BASAH" : (humidityPercent > 60) ? "KERING" : "BAIK";
String message = "Kelembapan Tanah: " + status + "\n";
message += "Presentasi: " + String(humidityPercent) + "%";
bot.sendMessage(chat_id, message, "");
}
}
}
void loop() {
int16_t sensorValue = analogRead(34);
float humidityPercent = map(sensorValue, 2165, 3135, 0, 100); // Konversi nilai analog ke persentase
String msg;
if (humidityPercent < 40) {
msg = "BASAH";
digitalWrite(redLedPin, HIGH);
digitalWrite(yellowLedPin, LOW);
digitalWrite(greenLedPin, LOW);
tone(buzzerPin, 1000);
} else if (humidityPercent > 60) {
msg = "KERING";
digitalWrite(redLedPin, LOW);
digitalWrite(yellowLedPin, LOW);
digitalWrite(greenLedPin, HIGH);
tone(buzzerPin, 2000);
} else {
msg = "BAIK";
digitalWrite(redLedPin, LOW);
digitalWrite(yellowLedPin, HIGH);
digitalWrite(greenLedPin, LOW);
noTone(buzzerPin);
}
lcd.clear();
lcd.print("Kelembapan Tanah ");
lcd.print(msg);
lcd.setCursor(0, 1);
lcd.print("Presentasi: ");
lcd.print(humidityPercent);
lcd.print("%");
delay(500);
// Send notification if status changes or at defined interval
if ((millis() > lastNotificationTime + notificationInterval) || (msg != lastStatus)) {
sendNotification(msg, humidityPercent);
lastNotificationTime = millis();
lastStatus = msg;
}
// Check Telegram messages
if (millis() > lastTimeBotRan + botInterval) {
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
while (numNewMessages) {
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
lastTimeBotRan = millis();
}
}