pleasedontcode

Smart Buzzer

Nov 18th, 2025
518
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 3.76 KB | Source Code | 0 0
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Smart Buzzer
  13.     - Source Code NOT compiled for: ESP8266 NodeMCU V1.0
  14.     - Source Code created on: 2025-11-18 12:27:45
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Dimonitor ada notif ke telegram tali ditelegram ga */
  21.     /* ada notif */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24.  
  25. /* START CODE */
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <ezBuzzer.h>  //https://github.com/ArduinoGetStarted/buzzer
  29. #include <ESP8266WiFi.h>
  30. #include <WiFiClientSecure.h>
  31. #include <UniversalTelegramBot.h>
  32.  
  33. /****** FUNCTION PROTOTYPES *****/
  34. void setup(void);
  35. void loop(void);
  36. void notifyTelegram(const String& message);
  37.  
  38. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  39. const uint8_t passive_buzzer_PassiveBuzzer_Signal_PIN_D1 = 1;
  40.  
  41. /****** WiFi Credentials - To be configured for your network *****/
  42. const char* ssid = "YOUR_SSID";
  43. const char* password = "YOUR_PASSWORD";
  44.  
  45. /****** Telegram Bot Token and Chat ID - To be configured for your bot *****/
  46. const char* botToken = "YOUR_BOT_TOKEN";
  47. const String chatID = "YOUR_CHAT_ID";
  48.  
  49. WiFiClientSecure client;
  50. UniversalTelegramBot bot(botToken, client);
  51.  
  52. ezBuzzer buzzer(PASSIVE_Buzzer_PassiveBuzzer_Signal_PIN_D1); // create ezBuzzer object
  53.  
  54. // Flag to control notification state
  55. bool notificationSent = false;
  56.  
  57. void setup() {
  58.     Serial.begin(9600);
  59.     pinMode(passive_buzzer_PassiveBuzzer_Signal_PIN_D1, OUTPUT);
  60.     WiFi.begin(ssid, password);
  61.     Serial.println("Connecting to WiFi...");
  62.     while (WiFi.status() != WL_CONNECTED) {
  63.         delay(500);
  64.         Serial.print(".");
  65.     }
  66.     Serial.println("
  67. WiFi connected!");
  68.     client.setInsecure(); // For WSS
  69. }
  70.  
  71. void loop() {
  72.     // Ensure WiFi connection
  73.     if (WiFi.status() != WL_CONNECTED) {
  74.         WiFi.begin(ssid, password);
  75.         Serial.println("Reconnecting to WiFi...");
  76.         while (WiFi.status() != WL_CONNECTED) {
  77.             delay(500);
  78.             Serial.print(".");
  79.         }
  80.         Serial.println("WiFi reconnected!");
  81.         notificationSent = false; // Reset notification flag
  82.     }
  83.  
  84.     // Simulate detect event here; in real application, replace with actual detection logic
  85.     static unsigned long lastDetectTime = 0;
  86.     unsigned long currentTime = millis();
  87.  
  88.     if (currentTime - lastDetectTime > 30000) { // Simulate event every 30 seconds
  89.         lastDetectTime = currentTime;
  90.         // Send notification only once per event
  91.         if (!notificationSent) {
  92.             String message = "Alert: Detection event occurred!";
  93.             notifyTelegram(message);
  94.             notificationSent = true;
  95.         }
  96.         // Activate buzzer
  97.         buzzer.beep(2000); // Beep for 2 seconds
  98.     }
  99.  
  100.     buzzer.loop(); // Call buzzer loop
  101. }
  102.  
  103. // Function to send notification to Telegram
  104. void notifyTelegram(const String& message) {
  105.     Serial.println("Sending Telegram notification...");
  106.     if (WiFi.status() == WL_CONNECTED) {
  107.         boolean success = bot.sendMessage(chatID, message, "Markdown");
  108.         if (success) {
  109.             Serial.println("Notification sent successfully.");
  110.         } else {
  111.             Serial.println("Failed to send notification.");
  112.         }
  113.     } else {
  114.         Serial.println("WiFi not connected. Cannot send notification.");
  115.     }
  116. }
  117.  
  118. /* END CODE */
  119.  
Advertisement
Add Comment
Please, Sign In to add comment