You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ESP32智能保险箱项目Telegram Bot配置及编译错误求助

智能保险箱项目编译错误求助

项目需求

  • 支持通过键盘输入密码开启保险箱门
  • 支持通过RFID卡片开启保险箱门
  • 对接Telegram Bot,支持通过Telegram指令开门,并推送保险箱状态(门已开/关、密码错误等)

代码实现

#include <Keypad.h> // Include the Keypad library
#include <WiFi.h> // Include the WiFi library
#include <UniversalTelegramBot.h> // Include the UniversalTelegramBot library
#include <MFRC522.h> // Include the MFRC522 library

#define SS_PIN 5 // Define the SS pin for the MFRC522 RFID sensor
#define RST_PIN 23 // Define the RST pin for the MFRC522 RFID sensor
#define LED_PIN 2 // Define the LED pin
#define BUZZER_PIN 4 // Define the buzzer pin
#define DOOR_PIN 16 // Define the pin for the door
const byte ROWS = 4; // Define the number of rows in the keypad
const byte COLS = 4; // Define the number of columns in the keypad
char keys[ROWS][COLS] = { // Define the keys in the keypad
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {14, 27, 26, 25}; // Define the row pins for the keypad
byte colPins[COLS] = {33, 32, 35, 34}; // Define the column pins for the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); // Create a Keypad object

const char* ssid = "your_SSID"; // Replace with your WiFi network name
const char* password = "your_PASSWORD"; // Replace with your WiFi network password
const char* token = "6063247261:AAE-dCDewFBwZLQQzw_TwVpp40C8plhMhko"; // Replace with your Telegram bot token
const int chat_id = 594170852; // Replace with your Telegram chat ID
//WiFiClientSecure client; // Create a secure WiFi client
//UniversalTelegramBot bot(token, client); // Create a UniversalTelegramBot object

MFRC522 rfid(SS_PIN, RST_PIN); // Create an MFRC522 object
MFRC522::MIFARE_Key key; // Create an MFRC522 MIFARE key object

int state = 0; // Initialize the state variable
String password_str = ""; // Initialize the password string
bool door_opened = false; // Initialize the door_opened boolean

void setup() {
  Serial.begin(9600); // Start the serial communication
  pinMode(LED_PIN, OUTPUT); // Set the LED pin as an output
  pinMode(BUZZER_PIN, OUTPUT); // Set the buzzer pin as an output
  pinMode(DOOR_PIN, OUTPUT); // Set the door pin as an output
  digitalWrite(LED_PIN, LOW); // Turn off the LED
  digitalWrite(BUZZER_PIN, LOW); // Turn off the buzzer
  digitalWrite(DOOR_PIN, LOW); // Close the door
  WiFi.begin(ssid, password); // Connect to the WiFi network
  while (WiFi.status() != WL_CONNECTED) { // Wait for the WiFi connection to be established
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");
  SPI.begin(); // Initialize the SPI bus
  rfid.PCD_Init(); // Initialize the MFRC522 RFID sensor
  for (byte i = 0; i < 6; i++) { // Initialize the MIFARE key
    key.keyByte[i] = 0xFF;
  }
}

void loop() {
  switch (state) {
    case 0:
      password_str = ""; // Reset the password string
      Serial.println("Enter password:");
      state = 1; // Move to the next state
      break;
    case 1:
      char key_pressed = keypad.getKey(); // Read the key pressed on the keypad
      if (key_pressed != NO_KEY) { // If a key is pressed
        if (key_pressed == '#') { // If the '#' key is pressed
          if (password_str == "1234") { // If the password is correct
            Serial.println("Password correct");
            open_door(); // Open the door
            state = 2; // Move to the next state
          } else { // If the password is incorrect
            Serial.println("Password incorrect");
            buzz(); // Buzz the buzzer
            state = 0; // Reset the state
          }
        } else { // If a number key is pressed
          password_str += key_pressed; // Add the key to the password string
        }
      }
      break;
    case 2:
      if (door_opened) { // If the door has been opened
        send_telegram_message("Door opened via keypad"); //Send a message to the Telegram chat to notify the user that the door has been opened via the keypad
        door_opened = false; // Reset the door_opened boolean
      }
      state = 3; // Move to the next state
      break;
    case 3:
      if (bot.getUpdates(bot.last_message_received + 1)) { // Check for incoming Telegram messages
        handle_telegram_message(); // Handle the incoming message
      }
      break;
  }
  if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) { // If an RFID card is detected
    if (authenticate_card()) { // If the card is authenticated
      Serial.println("Card authenticated");
      open_door(); // Open the door
      send_telegram_message("Door opened via RFID"); // Send a message to the Telegram chat to notify the user that the door has been opened via RFID
      delay(5000); // Wait for 5 seconds
      door_opened = false; // Reset the door_opened boolean
    } else { // If the card is not authenticated
      Serial.println("Card authentication failed");
      buzz(); // Buzz the buzzer
    }
    rfid.PICC_HaltA(); // Halt the RFID tag
    rfid.PCD_StopCrypto1(); // Stop the crypto1 process
  }
}

void open_door() {
  digitalWrite(LED_PIN, HIGH); // Turn on the LED
  digitalWrite(BUZZER_PIN, HIGH); // Turn on the buzzer
  digitalWrite(DOOR_PIN, HIGH); // Open the door
  delay(5000); // Wait for 5 seconds
  digitalWrite(LED_PIN, LOW); // Turn off the LED
  digitalWrite(BUZZER_PIN, LOW); // Turn off the buzzer
  digitalWrite(DOOR_PIN, LOW); // Close the door
  door_opened = true; // Set the door_opened boolean to true
}

void buzz() {
  digitalWrite(BUZZER_PIN, HIGH); // Turn on the buzzer
  delay(500); // Wait for 0.5 seconds
  digitalWrite(BUZZER_PIN, LOW); // Turn off the buzzer
}

bool authenticate_card() {
  MFRC522::StatusCode status;
  byte buffer[18];
  byte size = sizeof(buffer);
  status = rfid.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 1, &key, &(rfid.uid)); // Authenticate the card using the MIFARE key
  if (status != MFRC522::STATUS_OK) {
    return false;
  }
  status = rfid.MIFARE_Read(1, buffer, &size); // Read the card data
  if (status != MFRC522::STATUS_OK) {
    return false;
  }
  return true;
}

void handle_telegram_message() {
  for (int i = 0; i < bot.message_count(); i++) { // Loop through all the incoming messages
    String chat_text = bot.messages[i].text; // Get the message text
    int chat_from = bot.messages[i].from_id; // Get the chat ID
    if (chat_text == "/open" && chat_from == chat_id) { // If the message is "/open" and the chat ID is correct
      Serial.println("Door opened via telegram");
      open_door(); // Open the door
      send_telegram_message("Door opened via telegram"); // Send a message to the Telegram chat to notify the user that the door has been opened via Telegram
    }
  }
}

void send_telegram_message(String message) {
  bot.sendMessage(chat_id, message); // Send a message to the Telegram chat
}

编译错误信息

D:\Study\Study\senior project 2\smartsafecode\smartsafecode.ino: In function 'void loop()':
D:\Study\Study\senior project 2\smartsafecode\smartsafecode.ino:83:10: error: jump to case label [-fpermissive]
     case 2:
          ^
D:\Study\Study\senior project 2\smartsafecode\smartsafecode.ino:66:12: note:   crosses initialization of 'char key_pressed'
       char key_pressed = keypad.getKey(); // Read the key pressed on the keypad
            ^~~~~~~~~~~
D:\Study\Study\senior project 2\smartsafecode\smartsafecode.ino:90:10: error: jump to case label [-fpermissive]
     case 3:
          ^
D:\Study\Study\senior project 2\smartsafecode\smartsafecode.ino:66:12: note:   crosses initialization of 'char key_pressed'
       char key_pressed = keypad.getKey(); // Read the key pressed on the keypad
            ^~~~~~~~~~~
D:\Study\Study\senior project 2\smartsafecode\smartsafecode.ino:91:11: error: 'bot' was not declared in this scope
       if (bot.getUpdates(bot.last_message_received + 1)) { // Check for incoming Telegram messages
           ^~~
D:\Study\Study\senior project 2\smartsafecode\smartsafecode.ino:91:11: note: suggested alternative: 'bit'
       if (bot.getUpdates(bot.last_message_received + 1)) { // Check for incoming Telegram messages
           ^~~
           bit
D:\Study\Study\senior project 2\smartsafecode\smartsafecode.ino: In function 'void handle_telegram_message()':
D:\Study\Study\senior project 2\smartsafecode\smartsafecode.ino:145:23: error: 'bot' was not declared in this scope
   for (int i = 0; i < bot.message_count(); i++) { // Loop through all the incoming messages
                       ^~~
D:\Study\Study\senior project 2\smartsafecode\smartsafecode.ino:145:23: note: suggested alternative: 'bit'
   for (int i = 0; i < bot.message_count(); i++) { // Loop through all the incoming messages
                       ^~~
                       bit
D:\Study\Study\senior project 2\smartsafecode\smartsafecode.ino: In function 'void send_telegram_message(String)':
D:\Study\Study\senior project 2\smartsafecode\smartsafecode.ino:157:3: error: 'bot' was not declared in this scope
   bot.sendMessage(chat_id, message); // Send a message to the Telegram chat
   ^~~
D:\Study\Study\senior project 2\smartsafecode\smartsafecode.ino:157:3: note: suggested alternative: 'bit'
   bot.sendMessage(chat_id, message); // Send a message to the Telegram chat
   ^~~
   bit
Compilation error: jump to case label [-fpermissive]

内容的提问来源于stack exchange,提问作者Network Eng

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 02:34:53