#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* lineToken = "92bJkHiavG7E2PnoVT9GNAO290Utkr4bShRrNBcAeUy";
const int trigPin = 5; // กรณีใช้ JSN-SR04T หรือ HC-SR04
const int echoPin = 18; // กรณีใช้ JSN-SR04T หรือ HC-SR04
long duration;
int distance;
void setup() {
Serial.begin(115200);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.println(distance);
if (distance < 5) { // ตั้งค่าระดับน้ำที่ต้องการแจ้งเตือน (5 ซม. หรือตามที่ต้องการ)
sendLineNotify("ระดับน้ำในขวดต่ำเกินค่าที่กำหนดแล้ว!");
delay(6000); // รอ 1 นาที ก่อนตรวจวัดครั้งถัดไป
}
delay(2000); // ตรวจวัดทุกๆ 2 วินาที
}
void sendLineNotify(String message) {
HTTPClient http;
http.begin("https://notify-api.line.me/api/notify");
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
http.addHeader("Authorization", "Bearer " + String(lineToken));
String postData = "message=" + message;
int httpCode = http.POST(postData);
if (httpCode > 0) {
Serial.printf("HTTP Code: %d\n", httpCode);
} else {
Serial.printf("HTTP POST Failed, Error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}