#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "Wokwi-GUEST"; // WiFi Name
const char* password = ""; // WiFi Password
const char* serverName = "http://service.ngarduino.com/sendMessage";
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int potensioPin = 34;
const int relayPin1 = 16;
const int relayPin2 = 17;
void setup() {
Wire.begin();
lcd.init();
lcd.backlight();
pinMode(relayPin1, OUTPUT);
pinMode(relayPin2, OUTPUT);
WiFi.begin(ssid, password);
Serial.begin(115200); // Untuk debugging
}
void loop() {
int sensorValue = analogRead(potensioPin);
float voltage = sensorValue * (5.0 / 1023.0);
lcd.setCursor(0, 0);
lcd.print("Voltage: ");
lcd.print(voltage);
lcd.print("V");
if (voltage < 2) {
digitalWrite(relayPin1, HIGH);
digitalWrite(relayPin2, HIGH);
sendWhatsAppNotification(voltage, "Voltage dropped");
} else if (voltage > 4) {
digitalWrite(relayPin1, HIGH);
digitalWrite(relayPin2, HIGH);
sendWhatsAppNotification(voltage, "Voltage increased");
} else {
digitalWrite(relayPin1, LOW);
digitalWrite(relayPin2, LOW);
}
delay(1000);
}
void sendWhatsAppNotification(float voltage, String message) {
if(WiFi.status() == WL_CONNECTED){
HTTPClient http;
http.begin(serverName);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
String key = "Y1h134EjC2l58Kh5NI94yzcpe9piilyV"; // Your Key
String messageToSend = " " + message + " (" + String(voltage) + "V)"; // WhatsApp notification message
String group = "5gSQ"; // WhatsApp Group
String postData = "key=" + key + "&message=" + messageToSend + "&group=" + group; // POST parameters
int httpResponseCode = http.POST(postData);
String payload = http.getString();
Serial.println(payload);
http.end();
} else {
Serial.println("WiFi Disconnected");
}
}