#include <WiFi.h>
#include <HTTPClient.h>
#include <ESP32Servo.h>
// Define LED pins
int pirPin = 13; // Pin connected to PIR sensor
int LedRED = 15;
int LedGREEN = 2;
int LedBLUDE = 4;
int LedYELLOW = 5;
int LedORANGE = 18;
Servo myservo; // Create a servo object
int servoPin = 21; // Pin connected to the servo signal
// Define LDR pin
int ldrPin = 34; // Change this to the actual pin you are using for the LDR
// Define authentication and Wi-Fi credentials
String LINE_TOKEN = "opWQZMOEMELpHMZQcqdUTiy9P34ZPukejP40T70m0LY"; // LINE Token
char auth[] = ""; // Blynk authentication token
char ssid[] = "Wokwi-GUEST"; // Wi-Fi SSID
char pass[] = ""; // Wi-Fi password
void setup() {
Serial.begin(9600);
pinMode(LedRED, OUTPUT);
pinMode(LedGREEN, OUTPUT);
pinMode(LedBLUDE, OUTPUT);
pinMode(LedYELLOW, OUTPUT);
pinMode(LedORANGE, OUTPUT);
pinMode(pirPin, INPUT); // Set PIR pin as input
myservo.attach(servoPin); // Attach the servo to the specified pin
myservo.write(0); // Initialize the servo position to 0 degrees
// Initialize Wi-Fi
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void loop() {
int val = analogRead(ldrPin);
int pirValue = digitalRead(pirPin);
Serial.print("ความเข้มแสง = ");
Serial.println(val);
if (val > 500) {
digitalWrite(LedRED, HIGH); // Turn on Red LED
digitalWrite(LedGREEN, LOW); // Turn off Green LED
myservo.write(0); // Set servo to 0 degrees
} else {
if (pirValue == HIGH) {
sendLineNotification("พระอาทิตย์ขึ้นแล้ว ตื่นๆๆๆๆ");
digitalWrite(LedRED, LOW); // Turn off Red LED
digitalWrite(LedGREEN, HIGH); // Turn on Green LED
myservo.write(360); // Rotate servo to 90 degrees
delay(1000); // It is better to use timers instead of delay
}
}
}
void sendLineNotification(const char* message) {
HTTPClient http;
http.begin("https://notify-api.line.me/api/notify");
http.addHeader("Authorization", "Bearer " + LINE_TOKEN);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
int httpCode = http.POST("message=" + String(message));
String payload = http.getString();
Serial.println("HTTP Response Code: " + String(httpCode));
Serial.println("Response: " + payload);
http.end();
}