×

使用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条评论

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

'+ '

'+ '

'+ ''+ '
'+ ''+ ''+ '
'+ ''+ '' ); $.get('/article/vipdownload/aid/'+webid,function(data){ if(data.code ==5){ $(pop_this).attr('href',"/login/index.html"); return false } if(data.code == 2){ //跳转到VIP升级页面 window.location.href="//m.obk20.com/vip/index?aid=" + webid return false } //是会员 if (data.code > 0) { $('body').append(htmlSetNormalDownload); var getWidth=$("#poplayer").width(); $("#poplayer").css("margin-left","-"+getWidth/2+"px"); $('#tips').html(data.msg) $('.download_confirm').click(function(){ $('#dialog').remove(); }) } else { var down_url = $('#vipdownload').attr('data-url'); isBindAnalysisForm(pop_this, down_url, 1) } }); }); //是否开通VIP $.get('/article/vipdownload/aid/'+webid,function(data){ if(data.code == 2 || data.code ==5){ //跳转到VIP升级页面 $('#vipdownload>span').text("开通VIP 免费下载") return false }else{ // 待续费 if(data.code == 3) { vipExpiredInfo.ifVipExpired = true vipExpiredInfo.vipExpiredDate = data.data.endoftime } $('#vipdownload .icon-vip-tips').remove() $('#vipdownload>span').text("VIP免积分下载") } }); }).on("click",".download_cancel",function(){ $('#dialog').remove(); }) var setWeixinShare={};//定义默认的微信分享信息,页面如果要自定义分享,直接更改此变量即可 if(window.navigator.userAgent.toLowerCase().match(/MicroMessenger/i) == 'micromessenger'){ var d={ title:'使用Wemos D1 Mini使用Telegram应用程序进行智能家居控制',//标题 desc:$('[name=description]').attr("content"), //描述 imgUrl:'https://'+location.host+'/static/images/ele-logo.png',// 分享图标,默认是logo link:'',//链接 type:'',// 分享类型,music、video或link,不填默认为link dataUrl:'',//如果type是music或video,则要提供数据链接,默认为空 success:'', // 用户确认分享后执行的回调函数 cancel:''// 用户取消分享后执行的回调函数 } setWeixinShare=$.extend(d,setWeixinShare); $.ajax({ url:"//www.obk20.com/app/wechat/index.php?s=Home/ShareConfig/index", data:"share_url="+encodeURIComponent(location.href)+"&format=jsonp&domain=m", type:'get', dataType:'jsonp', success:function(res){ if(res.status!="successed"){ return false; } $.getScript('https://res.wx.qq.com/open/js/jweixin-1.0.0.js',function(result,status){ if(status!="success"){ return false; } var getWxCfg=res.data; wx.config({ //debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。 appId:getWxCfg.appId, // 必填,公众号的唯一标识 timestamp:getWxCfg.timestamp, // 必填,生成签名的时间戳 nonceStr:getWxCfg.nonceStr, // 必填,生成签名的随机串 signature:getWxCfg.signature,// 必填,签名,见附录1 jsApiList:['onMenuShareTimeline','onMenuShareAppMessage','onMenuShareQQ','onMenuShareWeibo','onMenuShareQZone'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2 }); wx.ready(function(){ //获取“分享到朋友圈”按钮点击状态及自定义分享内容接口 wx.onMenuShareTimeline({ title: setWeixinShare.title, // 分享标题 link: setWeixinShare.link, // 分享链接 imgUrl: setWeixinShare.imgUrl, // 分享图标 success: function () { setWeixinShare.success; // 用户确认分享后执行的回调函数 }, cancel: function () { setWeixinShare.cancel; // 用户取消分享后执行的回调函数 } }); //获取“分享给朋友”按钮点击状态及自定义分享内容接口 wx.onMenuShareAppMessage({ title: setWeixinShare.title, // 分享标题 desc: setWeixinShare.desc, // 分享描述 link: setWeixinShare.link, // 分享链接 imgUrl: setWeixinShare.imgUrl, // 分享图标 type: setWeixinShare.type, // 分享类型,music、video或link,不填默认为link dataUrl: setWeixinShare.dataUrl, // 如果type是music或video,则要提供数据链接,默认为空 success: function () { setWeixinShare.success; // 用户确认分享后执行的回调函数 }, cancel: function () { setWeixinShare.cancel; // 用户取消分享后执行的回调函数 } }); //获取“分享到QQ”按钮点击状态及自定义分享内容接口 wx.onMenuShareQQ({ title: setWeixinShare.title, // 分享标题 desc: setWeixinShare.desc, // 分享描述 link: setWeixinShare.link, // 分享链接 imgUrl: setWeixinShare.imgUrl, // 分享图标 success: function () { setWeixinShare.success; // 用户确认分享后执行的回调函数 }, cancel: function () { setWeixinShare.cancel; // 用户取消分享后执行的回调函数 } }); //获取“分享到腾讯微博”按钮点击状态及自定义分享内容接口 wx.onMenuShareWeibo({ title: setWeixinShare.title, // 分享标题 desc: setWeixinShare.desc, // 分享描述 link: setWeixinShare.link, // 分享链接 imgUrl: setWeixinShare.imgUrl, // 分享图标 success: function () { setWeixinShare.success; // 用户确认分享后执行的回调函数 }, cancel: function () { setWeixinShare.cancel; // 用户取消分享后执行的回调函数 } }); //获取“分享到QQ空间”按钮点击状态及自定义分享内容接口 wx.onMenuShareQZone({ title: setWeixinShare.title, // 分享标题 desc: setWeixinShare.desc, // 分享描述 link: setWeixinShare.link, // 分享链接 imgUrl: setWeixinShare.imgUrl, // 分享图标 success: function () { setWeixinShare.success; // 用户确认分享后执行的回调函数 }, cancel: function () { setWeixinShare.cancel; // 用户取消分享后执行的回调函数 } }); }); }); } }); } function openX_ad(posterid, htmlid, width, height) { if ($(htmlid).length > 0) { var randomnumber = Math.random(); var now_url = encodeURIComponent(window.location.href); var ga = document.createElement('iframe'); ga.src = 'https://www1.elecfans.com/www/delivery/myafr.php?target=_blank&cb=' + randomnumber + '&zoneid=' + posterid+'&prefer='+now_url; ga.width = width; ga.height = height; ga.frameBorder = 0; ga.scrolling = 'no'; var s = $(htmlid).append(ga); } } openX_ad(828, '#berry-300', 300, 250);