CCCPaste

Smart Buzzer

/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: Smart Buzzer
	- Source Code NOT compiled for: ESP8266 NodeMCU V1.0
	- Source Code created on: 2025-11-18 12:27:45

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* Dimonitor ada notif ke telegram tali ditelegram ga */
	/* ada notif */
/****** END SYSTEM REQUIREMENTS *****/


/* START CODE */

/****** DEFINITION OF LIBRARIES *****/
#include   //https://github.com/ArduinoGetStarted/buzzer
#include 
#include 
#include 

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void notifyTelegram(const String& message);

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t passive_buzzer_PassiveBuzzer_Signal_PIN_D1 = 1;

/****** WiFi Credentials - To be configured for your network *****/
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";

/****** Telegram Bot Token and Chat ID - To be configured for your bot *****/
const char* botToken = "YOUR_BOT_TOKEN";
const String chatID = "YOUR_CHAT_ID";

WiFiClientSecure client;
UniversalTelegramBot bot(botToken, client);

ezBuzzer buzzer(PASSIVE_Buzzer_PassiveBuzzer_Signal_PIN_D1); // create ezBuzzer object

// Flag to control notification state
bool notificationSent = false;

void setup() {
    Serial.begin(9600);
    pinMode(passive_buzzer_PassiveBuzzer_Signal_PIN_D1, OUTPUT);
    WiFi.begin(ssid, password);
    Serial.println("Connecting to WiFi...");
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println("
WiFi connected!");
    client.setInsecure(); // For WSS
}

void loop() {
    // Ensure WiFi connection
    if (WiFi.status() != WL_CONNECTED) {
        WiFi.begin(ssid, password);
        Serial.println("Reconnecting to WiFi...");
        while (WiFi.status() != WL_CONNECTED) {
            delay(500);
            Serial.print(".");
        }
        Serial.println("WiFi reconnected!");
        notificationSent = false; // Reset notification flag
    }

    // Simulate detect event here; in real application, replace with actual detection logic
    static unsigned long lastDetectTime = 0;
    unsigned long currentTime = millis();

    if (currentTime - lastDetectTime > 30000) { // Simulate event every 30 seconds
        lastDetectTime = currentTime;
        // Send notification only once per event
        if (!notificationSent) {
            String message = "Alert: Detection event occurred!";
            notifyTelegram(message);
            notificationSent = true;
        }
        // Activate buzzer
        buzzer.beep(2000); // Beep for 2 seconds
    }

    buzzer.loop(); // Call buzzer loop
}

// Function to send notification to Telegram
void notifyTelegram(const String& message) {
    Serial.println("Sending Telegram notification...");
    if (WiFi.status() == WL_CONNECTED) {
        boolean success = bot.sendMessage(chatID, message, "Markdown");
        if (success) {
            Serial.println("Notification sent successfully.");
        } else {
            Serial.println("Failed to send notification.");
        }
    } else {
        Serial.println("WiFi not connected. Cannot send notification.");
    }
}

/* END CODE */