Arduino / Telegram机器人,编辑消息/按钮
创始人
2024-09-12 12:02:00
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

相关内容

热门资讯

安卓换鸿蒙系统会卡吗,体验流畅... 最近手机圈可是热闹非凡呢!不少安卓用户都在议论纷纷,说鸿蒙系统要来啦!那么,安卓手机换上鸿蒙系统后,...
app安卓系统登录不了,解锁登... 最近是不是你也遇到了这样的烦恼:手机里那个心爱的APP,突然就登录不上了?别急,让我来帮你一步步排查...
安卓系统拦截短信在哪,安卓系统... 你是不是也遇到了这种情况:手机里突然冒出了很多垃圾短信,烦不胜烦?别急,今天就来教你怎么在安卓系统里...
安卓系统要维护多久,安卓系统维... 你有没有想过,你的安卓手机里那个陪伴你度过了无数日夜的安卓系统,它究竟要陪伴你多久呢?这个问题,估计...
windows官网系统多少钱 Windows官网系统价格一览:了解正版Windows的购买成本Windows 11官方价格解析微软...
安卓系统如何卸载app,轻松掌... 手机里的App越来越多,是不是感觉内存不够用了?别急,今天就来教你怎么轻松卸载安卓系统里的App,让...
怎么复制照片安卓系统,操作步骤... 亲爱的手机控们,是不是有时候想把自己的手机照片分享给朋友,或者备份到电脑上呢?别急,今天就来教你怎么...
安卓系统应用怎么重装,安卓应用... 手机里的安卓应用突然罢工了,是不是让你头疼不已?别急,今天就来手把手教你如何重装安卓系统应用,让你的...
iwatch怎么连接安卓系统,... 你有没有想过,那款时尚又实用的iWatch,竟然只能和iPhone好上好?别急,今天就来给你揭秘,怎...
iphone系统与安卓系统更新... 最近是不是你也遇到了这样的烦恼?手机更新系统总是失败,急得你团团转。别急,今天就来给你揭秘为什么iP...