#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* webhookURL = "https://webhook.site/70b00c0e-01c3-4dcd-926c-6be65716619a";
const int buttonPin = 13;
const int ledPin = 12;
const int switchPin = 15;
int buttonState = HIGH;
int lastButtonState = HIGH;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
int switchState = HIGH;
void setup() {
Serial.begin(115200);
Serial.println("Starting...");
pinMode(buttonPin, INPUT_PULLUP);
pinMode(switchPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
}
void loop() {
int reading = digitalRead(buttonPin);
switchState = digitalRead(switchPin);
if (switchState == HIGH) {
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == LOW) {
Serial.println("Button pressed!");
sendWebhookRequest(buttonState);
}
}
}
lastButtonState = reading;
blinkLED();
}
else {
digitalWrite(ledPin, LOW);
}
}
void sendWebhookRequest(int switchStatus) {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
Serial.println("Sending webhook request...");
HTTPClient http;
String url = String(webhookURL) + "?random=" + String(random(30));
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
Serial.print("Webhook request sent. Response code: ");
Serial.println(httpResponseCode);
} else {
Serial.print("Error sending webhook request. HTTP response code: ");
Serial.println(httpResponseCode);
}
http.end();
WiFi.disconnect();
Serial.println("WiFi Disconnected");
}
void blinkLED() {
static unsigned long previousMillis = 0;
static int ledState = LOW;
unsigned long currentMillis = millis();
if (currentMillis - previousMillis > 1000 && currentMillis - previousMillis <= 10000) {
ledState = LOW;
}
else if (currentMillis - previousMillis <= 1000) {
ledState = HIGH;
}
else if (currentMillis - previousMillis > 10000) {
ledState = HIGH;
previousMillis = currentMillis;
}
digitalWrite(ledPin, ledState);
}