Arduino / Telegram机器人,编辑消息/按钮
创始人
2024-09-12 12:00:34
0

要创建一个Arduino与Telegram机器人的交互,你需要使用Arduino板和一个WiFi模块,如ESP8266。下面是一个简单的示例,展示了如何通过Telegram机器人发送消息到Arduino,并通过按钮控制Arduino的输出。

首先,你需要准备以下材料:

  • Arduino板(如Arduino Uno)
  • ESP8266 WiFi模块
  • 杜邦线若干
  • 电阻和按钮

接下来,你需要进行以下步骤:

  1. 配置Arduino板和ESP8266的连接:

    • 将ESP8266的VCC连接到Arduino的5V引脚
    • 将ESP8266的GND连接到Arduino的GND引脚
    • 将ESP8266的TX连接到Arduino的RX引脚
    • 将ESP8266的RX连接到Arduino的TX引脚
  2. 在Arduino IDE中安装并配置ESP8266库:

    • 打开Arduino IDE并选择“文件”>“首选项”
    • 在“附加开发板管理器网址”中添加以下网址:http://arduino.esp8266.com/stable/package_esp8266com_index.json
    • 点击“确定”并打开“工具”>“开发板”>“开发板管理器”
    • 在搜索框中输入“esp8266”,然后安装“esp8266 by ESP8266 Community”
  3. 编写Arduino代码:

    #include 
    
    SoftwareSerial esp8266(2, 3); // RX, TX
    
    void setup() {
      Serial.begin(9600);
      esp8266.begin(9600);
    }
    
    void loop() {
      if (esp8266.available()) {
        String command = esp8266.readStringUntil('\n');
        if (command == "on") {
          digitalWrite(LED_BUILTIN, HIGH);
        } else if (command == "off") {
          digitalWrite(LED_BUILTIN, LOW);
        }
      }
    }
    
  4. 创建一个Telegram机器人并获取API令牌:

    • 下载Telegram应用并在搜索框中搜索BotFather
    • 跟随BotFather的指示创建一个新的机器人
    • 复制生成的API令牌,稍后将用于Arduino代码
  5. 编写Arduino代码以发送和接收Telegram消息:

    #include 
    
    SoftwareSerial esp8266(2, 3); // RX, TX
    
    String ssid = "your_wifi_ssid";
    String password = "your_wifi_password";
    String botToken = "your_bot_token";
    String chatId = "your_chat_id";
    
    void setup() {
      Serial.begin(9600);
      esp8266.begin(9600);
      connectToWifi();
      sendMessage("Arduino bot started.");
    }
    
    void loop() {
      if (esp8266.available()) {
        String command = esp8266.readStringUntil('\n');
        if (command == "on") {
          digitalWrite(LED_BUILTIN, HIGH);
          sendMessage("LED turned on.");
        } else if (command == "off") {
          digitalWrite(LED_BUILTIN, LOW);
          sendMessage("LED turned off.");
        }
      }
    }
    
    void connectToWifi() {
      String command = "AT+CWJAP=\"" + ssid + "\",\"" + password + "\"";
      esp8266.println(command);
      delay(2000);
    }
    
    void sendMessage(String message) {
      String command = "AT+CMGF=1";
      esp8266.println(command);
      delay(1000);
      command = "AT+CMGS=\"" + botToken + "\"";
      esp8266.println(command);
      delay(1000);
      command = message + "\r";
      esp8266.println(command);
      delay(1000);
      esp8266.write(26);
    }
    
  6. 在Telegram机器人中配置指令按钮:

    • 打开BotFather并选择你的机器人
    • 点击“Edit Bot”
    • 点击“Edit Commands”
    • 输入指令按钮和相应的指令(例如:/on,/off)
    • 保存更改
  7. 在Telegram Bot API中获取聊天ID:

    • 在浏览器中打开以下链接,并将YOUR_BOT

相关内容

热门资讯

记者揭秘!智星菠萝辅助(透视辅... 记者揭秘!智星菠萝辅助(透视辅助)拱趴大菠萝辅助神器,扑克教程(有挂细节);模式供您选择,了解更新找...
一分钟揭秘!约局吧能能开挂(透... 一分钟揭秘!约局吧能能开挂(透视辅助)hhpoker辅助靠谱,2024新版教程(有挂教学);约局吧能...
透视辅助!wepoker模拟器... 透视辅助!wepoker模拟器哪个好用(脚本)hhpoker辅助挂是真的,科技教程(有挂技巧);囊括...
透视代打!hhpkoer辅助器... 透视代打!hhpkoer辅助器视频(辅助挂)pokemmo脚本辅助,2024新版教程(有挂教程);风...
透视了解!约局吧德州真的有透视... 透视了解!约局吧德州真的有透视挂(透视脚本)德州局HHpoker透视脚本,必胜教程(有挂分析);亲,...
六分钟了解!wepoker挂底... 六分钟了解!wepoker挂底牌(透视)德普之星开辅助,详细教程(有挂解密);德普之星开辅助是一种具...
9分钟了解!wpk私人辅助(透... 9分钟了解!wpk私人辅助(透视)hhpoker德州透视,插件教程(有挂教学);风靡全球的特色经典游...
推荐一款!wepoker究竟有... 推荐一款!wepoker究竟有透视(脚本)哈糖大菠萝开挂,介绍教程(有挂技术);囊括全国各种wepo...
每日必备!wepoker有人用... 每日必备!wepoker有人用过(脚本)wpk有那种辅助,线上教程(有挂规律);wepoker有人用...
玩家必备教程!wejoker私... 玩家必备教程!wejoker私人辅助软件(脚本)哈糖大菠萝可以开挂,可靠技巧(有挂神器)申哈糖大菠萝...