Telegram bot is Application Programming Interface or also known as “API” which allow programmers to integrate two differentapplications simultaneously in this tutorial case, called the chat applicationTelagram with ESP8266.
With the existence ofthe TelegramBOT API token, it can be connected between telegram chat andhardware systems, for example, it will be integrated between the mini Wemos D1programmed using the Arduino IDE with this application.
This project used to thetelegram bot API and Wemos that will be used make the smart home to control thelamp and the door.
1. Installing Telegram Bot Library2. Source Code#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <Servo.h>
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<numNewMessages; 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. Configuring Telegram Bot4. Setting Up the Device
Comments