×

使用Wemos D1 Mini使用Telegram应用程序进行智能家居控制

消耗积分:0 | 格式:zip | 大小:0.00 MB | 2022-12-29

分享资料个

描述

Telegram bot 是应用程序编程接口或也称为“API” ,它允许程序员在本教程案例中同时集成两个不同的应用程序,称为带有 ESP8266 的聊天应用程序 Telagram。

由于 TelegramBOT API 令牌的存在,它可以连接在电报聊天和硬件系统之间,例如,它将集成在使用 Arduino IDE 编程的迷你 Wemos D1 与此应用程序之间。

该项目使用了电报机器人 API 和 Wemos,将用于制作智能家居来控制灯和门。

1. 安装 Telegram 机器人库

2.源代码

#include 
#include 
#include 
#include 
Servo myservo;
char ssid[] = "KAMAR KOS 7";     // your network SSID (name)
char password[] = "dasarlemahkaunak"; // your network key
#define BOTtoken "883873080:AAGkP-oNlzMgOS44X1jhE43C0_9JwwMCBPw"  // your Bot Token (Get from Botfather)
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
int Bot_mtbs = 1000; //mean time between scan messages
long Bot_lasttime;   //last time messages’ scan has been done
bool Start = false;
  
const int ledPin1 = 0;
const int ledPin2 = 2;
int ledStatus = 0;
void handleNewMessages(int numNewMessages) {
 Serial.print("status pesan = ");
 Serial.print(String(numNewMessages));
 Serial.println(",pesan berhasil diterima\n");
 for (int i=0; i   String chat_id = String(bot.messages[i].chat_id);
   String text = bot.messages[i].text;
   String from_name = bot.messages[i].from_name;
   if (from_name == "") from_name = "Guest";
   if (text == "/led1on") {
     digitalWrite(ledPin1, HIGH);   // turn the LED on (HIGH is the voltage level)
     digitalWrite(ledPin2, LOW);
     ledStatus = 1;
     bot.sendMessage(chat_id, "Led 1 is ON", "");
   }
   if (text == "/led1off") {
     ledStatus = 0;
     digitalWrite(ledPin1, LOW);    // turn the LED off (LOW is the voltage level)
     bot.sendMessage(chat_id, "Led 1 is OFF", "");
   }
   if (text == "/led2on") {
     digitalWrite(ledPin2, HIGH);
     digitalWrite(ledPin1, LOW);// turn the LED on (HIGH is the voltage level)
     ledStatus = 1;
     bot.sendMessage(chat_id, "Led 2 is ON", "");
   }
   if (text == "/led2off") {
     ledStatus = 0;
     digitalWrite(ledPin2, LOW);    // turn the LED off (LOW is the voltage level)
     bot.sendMessage(chat_id, "Led 2 is OFF", "");
   }
   if (text == "/alloff") {
     digitalWrite(ledPin1, LOW);
     digitalWrite(ledPin2, LOW);    // turn the LED off (LOW is the voltage level)
     bot.sendMessage(chat_id, "All led is OFF", "");
   }
   if (text == "/allon") {
     digitalWrite(ledPin1, HIGH);
     digitalWrite(ledPin2, HIGH);    // turn the LED off (LOW is the voltage level)
     bot.sendMessage(chat_id, "All led is ON", "");
   }
   if (text == "/rules") {
     bot.sendMessage(chat_id, "Perintah yang dapat diproses adalah perintah yang tertera dalam menu /help, Akep_bot juga tidak mentolerir adanya kesalahan pengetikan.","");
   }
   if (text == "Akep") {
     bot.sendMessage(chat_id, "Hai " + from_name + " aku disini, ketikan /start untuk memulai bot");
   }
   if (text == "/status1") {
     if(ledStatus){
       bot.sendMessage(chat_id, "Led is ON", "");
     } else {
       bot.sendMessage(chat_id, "Led is OFF", "");
     }
   }
      if (text == "/status2") {
     if(ledStatus){
       bot.sendMessage(chat_id, "Led is ON", "");
     } else {
       bot.sendMessage(chat_id, "Led is OFF", "");
     }
   }
   if (text == "/start") {
     bot.sendMessage(chat_id,"Selamat datang di Aplikasi Bot Telegram\n hai "+ from_name +", ketikkan /help untuk melihat perintah apa saja yang tersedia pada bot");
   }
   if (text == "/help") {
     String welcome = "Hai " + from_name + " :) \n";
     welcome += "berikut perintah yang tersedia pada bot\n";
     welcome += "/rules     : aturan memakai bot\n";
     welcome += "/led1on   : menyalakan led 1\n";
     welcome += "/led1off  : mematikan led 1\n";
     welcome += "/led2on   : menyalakan led 2\n";
     welcome += "/led2off  : mematikan led 2\n";
     welcome += "/status1  : cek status led 1\n";
     welcome += "/status2  : cek status led 2\n";
     welcome += "/allon     : menyalakan kedua led\n";
     welcome += "/alloff    : mematikan kedua led\n";
     bot.sendMessage(chat_id, welcome, "Markdown");
   }
   }
 }
void setup() {
 Serial.begin(115200);
 // Set WiFi to station mode and disconnect from an AP if it was Previously
 // connected
 WiFi.mode(WIFI_STA);
 WiFi.disconnect();
 delay(100);
 // attempt to connect to Wifi network:
 Serial.print("Connecting Wifi: ");
 Serial.println(ssid);
 Serial.println("ketikan nama bot anda pada telegram");
 WiFi.begin(ssid, password);
 while (WiFi.status() != WL_CONNECTED) {
   Serial.print(".");
   delay(500);
 }
 Serial.println("");
 Serial.println("WiFi terhubung");
 Serial.print("IP address: ");
 Serial.println(WiFi.localIP());
 Serial.println("ketikan nama bot anda pada telegram ");
 Serial.println("");
 pinMode(pir, INPUT);
 pinMode(ledPin1, OUTPUT); // initialize digital ledPin as an output.
 pinMode(ledPin2, OUTPUT);
 delay(10);
 digitalWrite(ledPin1, LOW); // initialize pin as off
 digitalWrite(ledPin2, LOW);
 myservo.attach(12);
 myservo.write(180);
}
void loop() {
 if (millis() > Bot_lasttime + Bot_mtbs)  {
   int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
   while(numNewMessages) {
     Serial.println("Pesan Diterima");
     Serial.println("sedang diproses.....");
     handleNewMessages(numNewMessages);
     numNewMessages = bot.getUpdates(bot.last_message_received + 1);
   }
   Bot_lasttime = millis();
 }
}
;>

3.配置电报机器人

4. 设置设备

 

 
pYYBAGOrz3uAbU7pAAFmBbmzSYM911.png
 

5. 测试

 

 

 

 


声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

评论(0)
发评论

下载排行榜

全部0条评论

快来发表一下你的评论吧 !