#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* token = "6tGAukGYflmoLhSFd2GZ8bKVZKLri2eHvMx6eOX8lbP"; //Line token form https://notify-bot.line.me/th/
#define ECHO_PIN 27
#define TRIG_PIN 14
#define RED_LED 12
#define GREEN_LED 13
void setup() {
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(RED_LED,OUTPUT);
pinMode(GREEN_LED,OUTPUT);
Serial.begin(115200);
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
Serial.println(" READY ");
}
float readDistanceCM() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
int duration = pulseIn(ECHO_PIN, HIGH);
return duration * 0.034 / 2;
}
void loop(){
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin("https://notify-api.line.me/api/notify");
http.addHeader("Authorization", "Bearer " + String(token));
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
float distance = readDistanceCM();
digitalWrite(GREEN_LED,HIGH);
if(distance>80){
http.POST("message=ระดับWaterหน้าโรงเรียนสูงกว่า 80 ซม by tutor.");
http.POST("message=กรุณานั่งเรือเข้ามาเรียน by tutor");
digitalWrite(RED_LED,HIGH);
}else if(distance>60){
http.POST("message=ระดับWaterหน้าโรงเรียนสูงกว่า 60 ซม. by tutor");
http.POST("message=ให้นักเรียนว่ายน้ำเข้ามาเรียน by tutor");
digitalWrite(RED_LED,HIGH);
}else if(distance>40){
http.POST("message=ระดับWaterหน้าโรงเรียนสูงกว่า 40 ซม. by tutor");
http.POST("message=ให้นักเรียนลุยน้ำเข้ามาเรียน by tutor");
digitalWrite(RED_LED,HIGH);
}
else if(distance>20){
http.POST("message=ระดับWaterหน้าโรงเรียนสูงกว่า 20 ซม. by tutor");
digitalWrite(RED_LED,HIGH);
}
int httpCode = http.POST("READY");
if (httpCode > 0) String response = http.getString();
else Serial.println(".....");
http.end();
delay(5000);
}
}