Guest User

Untitled

a guest
Jul 31st, 2024
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.04 KB | None | 0 0
  1.   #include <ESP8266WiFi.h>
  2.    #include <WiFiClientSecure.h>
  3.    #include <UniversalTelegramBot.h>
  4.    #include <SD.h>
  5.    #include <TMRpcm.h>
  6.  
  7.    const char* ssid = "Ваш_SSID";
  8.    const char* password = "Ваш_пароль_WiFi";
  9.    const char* telegramToken = "Ваш_Telegram_Bot_Token";
  10.  
  11.    WiFiClientSecure client;
  12.    UniversalTelegramBot bot(telegramToken, client);
  13.  
  14.    const int speakerPin = D1; // Пін для динаміка
  15.    const int sdCardPin = D2; // Пін для модуля SD-картки
  16.  
  17.    TMRpcm tmrpcm;
  18.  
  19.    void setup() {
  20.      Serial.begin(115200);
  21.      pinMode(speakerPin, OUTPUT);
  22.  
  23.      // Підключення до Wi-Fi
  24.      WiFi.begin(ssid, password);
  25.      while (WiFi.status() != WL_CONNECTED) {
  26.        delay(1000);
  27.        Serial.println("Connecting to WiFi...");
  28.      }
  29.      Serial.println("Connected to WiFi");
  30.      
  31.      // Налаштування SD-картки
  32.      if (!SD.begin(sdCardPin)) {
  33.        Serial.println("SD-картка не підключена!");
  34.        return;
  35.      }
  36.  
  37.      // Налаштування TMRpcm
  38.      tmrpcm.speakerPin = speakerPin;
  39.  
  40.      // Налаштування безпечного з'єднання для бота
  41.      client.setInsecure();
  42.    }
  43.  
  44.    void loop() {
  45.      int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  46.  
  47.      while (numNewMessages) {
  48.        handleNewMessages(numNewMessages);
  49.        numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  50.      }
  51.    }
  52.  
  53.    void handleNewMessages(int numNewMessages) {
  54.      for (int i = 0; i < numNewMessages; i++) {
  55.        String chat_id = String(bot.messages[i].chat_id);
  56.        String text = bot.messages[i].text;
  57.  
  58.        if (text == "/play") {
  59.          playSound();
  60.          bot.sendMessage(chat_id, "Відтворення звуку розпочато!", "");
  61.        }
  62.      }
  63.    }
  64.  
  65.    void playSound() {
  66.      if (SD.exists("sound.wav")) {
  67.        tmrpcm.play("sound.wav");
  68.      } else {
  69.        Serial.println("Файл не знайдено!");
  70.      }
  71.    }
  72.  
Advertisement
Add Comment
Please, Sign In to add comment