#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "Wokwi-GUEST"; // Nama WiFi
const char* password = ""; // Kata sandi WiFi
const char* serverName = "http://service.ngarduino.com/sendMessage";
const int sensorPin = 34; // Pin AO sensor photoresistor
const int threshold = 1500; // Threshold sensor
bool notificationSent = false; // Variabel untuk melacak apakah notifikasi sudah dikirim
const int relayPin1 = 16; // Pin kontrol untuk relay 1
const int relayPin2 = 17; // Pin kontrol untuk relay 2
LiquidCrystal_I2C lcd(0x27, 16, 2); // Alamat I2C untuk LCD1602
unsigned long lastNotificationTime = 0; // Waktu terakhir notifikasi dikirim
void setup() {
Serial.begin(115200);
lcd.init(); // Inisialisasi LCD
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Connecting WiFi..");
WiFi.begin(ssid, password);
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("WiFi Connected");
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Nilai Sensor:");
pinMode(sensorPin, INPUT);
pinMode(relayPin1, OUTPUT);
pinMode(relayPin2, OUTPUT);
}
void loop() {
int sensorValue = analogRead(sensorPin);
lcd.setCursor(0, 1);
lcd.print(sensorValue);
if(sensorValue > threshold && !notificationSent) {
sendWhatsAppNotification(sensorValue);
notificationSent = true;
lcd.setCursor(0, 1);
lcd.print("pH Lagi drop ");
lcd.print(sensorValue);
digitalWrite(relayPin1, HIGH); // Aktifkan relay 1
digitalWrite(relayPin2, HIGH); // Aktifkan relay 2
}
else if(sensorValue <= threshold) {
notificationSent = false;
lcd.setCursor(0, 1);
lcd.print(" "); // Menghapus pesan notifikasi jika nilai sensor kembali di bawah threshold
digitalWrite(relayPin1, LOW); // Matikan relay 1
digitalWrite(relayPin2, LOW); // Matikan relay 2
}
// Kirim notifikasi setiap 10 detik
unsigned long currentTime = millis();
if (currentTime - lastNotificationTime >= 10000) {
sendSensorValueNotification(sensorValue);
lastNotificationTime = currentTime;
}
delay(1000);
}
void sendWhatsAppNotification(int sensorValue) {
if(WiFi.status()== WL_CONNECTED){
HTTPClient http;
http.begin(serverName);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
String key = "Y1h134EjC2l58Kh5NI94yzcpe9piilyV"; // Kunci Anda
String message = "pH Lagi drop " + String(sensorValue); // Pesan untuk notifikasi WhatsApp
String group = "5gSQ"; // Group WhatsApp
String postData = "key=" + key + "&message=" + message + "&group=" + group; // Parameter POST
int httpResponseCode = http.POST(postData);
String payload = http.getString();
Serial.println(payload);
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
}
void sendSensorValueNotification(int sensorValue) {
if(WiFi.status()== WL_CONNECTED){
HTTPClient http;
http.begin(serverName);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
String key = "Y1h134EjC2l58Kh5NI94yzcpe9piilyV"; // Kunci Anda
String message = "Nilai Sensor saat ini: " + String(sensorValue); // Pesan untuk notifikasi WhatsApp
String group = "5gSQ"; // Group WhatsApp
String postData = "key=" + key + "&message=" + message + "&group=" + group; // Parameter POST
int httpResponseCode = http.POST(postData);
String payload = http.getString();
Serial.println(payload);
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
}