Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <WiFiClientSecure.h>
- #include <UniversalTelegramBot.h>
- #include <SD.h>
- #include <TMRpcm.h>
- const char* ssid = "Ваш_SSID";
- const char* password = "Ваш_пароль_WiFi";
- const char* telegramToken = "Ваш_Telegram_Bot_Token";
- WiFiClientSecure client;
- UniversalTelegramBot bot(telegramToken, client);
- const int speakerPin = D1; // Пін для динаміка
- const int sdCardPin = D2; // Пін для модуля SD-картки
- TMRpcm tmrpcm;
- void setup() {
- Serial.begin(115200);
- pinMode(speakerPin, OUTPUT);
- // Підключення до Wi-Fi
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(1000);
- Serial.println("Connecting to WiFi...");
- }
- Serial.println("Connected to WiFi");
- // Налаштування SD-картки
- if (!SD.begin(sdCardPin)) {
- Serial.println("SD-картка не підключена!");
- return;
- }
- // Налаштування TMRpcm
- tmrpcm.speakerPin = speakerPin;
- // Налаштування безпечного з'єднання для бота
- client.setInsecure();
- }
- void loop() {
- int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
- while (numNewMessages) {
- handleNewMessages(numNewMessages);
- numNewMessages = bot.getUpdates(bot.last_message_received + 1);
- }
- }
- void handleNewMessages(int numNewMessages) {
- for (int i = 0; i < numNewMessages; i++) {
- String chat_id = String(bot.messages[i].chat_id);
- String text = bot.messages[i].text;
- if (text == "/play") {
- playSound();
- bot.sendMessage(chat_id, "Відтворення звуку розпочато!", "");
- }
- }
- }
- void playSound() {
- if (SD.exists("sound.wav")) {
- tmrpcm.play("sound.wav");
- } else {
- Serial.println("Файл не знайдено!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment