#include <WiFi.h>
#include <HTTPClient.h>
#define BUTTON_PIN 21 // Define the button pin
#define LED_PIN_RED 19 // Define the RED LED pin
#define LED_PIN_YELLOW 18 // Define the YELLOW LED pin
#define LED_PIN_GREEN 5 // Define the GREEN LED pin
const char* auth = "q_HhugEwXYnIFcX2KUVJilxfOOpouG13";
const char* ssid = "Wokwi-GUEST";
const char* password = "";
bool ledRedOn = false;
bool ledYellowOn = false;
bool ledGreenOn = false;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
pinMode(BUTTON_PIN, INPUT_PULLUP); // Assuming the button is connected to this pin
pinMode(LED_PIN_RED, OUTPUT);
pinMode(LED_PIN_YELLOW, OUTPUT);
pinMode(LED_PIN_GREEN, OUTPUT);
Serial.begin(9600);
}
void sendLineNotification(String token, String message) {
HTTPClient http;
String url = "https://notify-api.line.me/api/notify";
String lineAuth = "Bearer " + token;
http.begin(url);
http.addHeader("Authorization", lineAuth);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
int httpResponseCode = http.POST("message=" + message);
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println(httpResponseCode);
Serial.println(response);
} else {
Serial.print("Error on sending POST request: ");
Serial.println(httpResponseCode);
}
http.end();
}
void loop() {
// Check if the button is pressed
if (digitalRead(BUTTON_PIN) == LOW) {
// Toggle the status of each LED
ledRedOn = !ledRedOn;
digitalWrite(LED_PIN_RED, ledRedOn ? HIGH : LOW);
// Send Line notification for RED LED
if (ledRedOn) {
sendLineNotification("G7ofr1tJtE8AgJCSrUaaOPW7kfKc95uznFnZBa2wbPQ", "เจ");
}
delay(1000); // debounce delay
// Toggle the status of each LED
ledYellowOn = !ledYellowOn;
digitalWrite(LED_PIN_YELLOW, ledYellowOn ? HIGH : LOW);
// Send Line notification for YELLOW LED
if (ledYellowOn) {
sendLineNotification("G7ofr1tJtE8AgJCSrUaaOPW7kfKc95uznFnZBa2wbPQ", "4");
}
delay(1000); // debounce delay
// Toggle the status of each LED
ledGreenOn = !ledGreenOn;
digitalWrite(LED_PIN_GREEN, ledGreenOn ? HIGH : LOW);
// Send Line notification for GREEN LED
if (ledGreenOn) {
sendLineNotification("G7ofr1tJtE8AgJCSrUaaOPW7kfKc95uznFnZBa2wbPQ", "ปวส.1");
}
delay(1000); // debounce delay
}
}