#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* token = "2BVONtoXmBh5nogutyDXZMqAktDk1cG7KitA4a9oBul"; //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
bool passing = false;
int counter = 1;
int distance = 100;
String text;
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 cm = readDistanceCM();
digitalWrite(GREEN_LED,HIGH);
if(cm < distance && !passing && cm!=0){
http.POST("message=Ehe Tutor Bot");
http.POST("message=มีนักเรียนเข้าห้องเรียน");
http.POST("message=ขณะนี้มีนักเรียนในห้องเรียน "+String(counter)+" คน");
digitalWrite(RED_LED,HIGH);
passing = true;
counter++;
}
if(cm > distance) passing = false;
int httpCode = http.POST("READY");
if (httpCode > 0) String response = http.getString();
else Serial.println(".....");
http.end();
delay(250);
}
}