Salman Abdul Jabbaar Wiharja
Portofolio ini Menjelaskan Mengenai Project Capstone Kami Kelompok 1 AI INFRA Batch 3 yaitu Door Opener with Telegram Bot
Anggota Kelompok:
- Salman Abdul Jabbaar Wiharja
- Muhammad Hisyam Nugraha Solihin
- Nurvita Alvayini Salim Potale
Setiap Project sudah pasti berawal dari sebuah masalah yang akan kami coba selesaikan. Masalah yang kami hadapi berawal dari:
Solusi yang kami tawarkan yaitu Gerbang yang bisa di buka dengan aplikasi telegram. atau kamu sebut dengan “Garage Door Opener with Telegram”
Pada project kami meggunakan beberapa Komponen atau Modul diantaranya:
untuk lebih jelas pin nya kita jabarkan sebagai berikut:
#ifdef ESP32
//#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h> // Universal Telegram Bot Library written by Brian Lough: https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot
#include <ArduinoJson.h>
// Connect NodeMCU to Wifi
const char* ssid = "POCO Salman"; //
const char* password = "12345678"; //09812374
// Initialize Telegram BOT (Get from Botfather)
#define BOTtoken "5570617795:AAHW_Onvzr4PPwe1VWwixtDGu-l9yqAoh6o"
// Initialize BotID (Get from myidbot)
#define CHAT_ID "888564739" //888564739 //897041904
#ifdef ESP8266
X509List cert(TELEGRAM_CERTIFICATE_ROOT);
#endif
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
// Checks for new messages every 1 second.
int botRequestDelay = 1000;
unsigned long lastTimeBotRan;
int ENA = D7;
int IN1 = D1;
int IN2 = D2;
/* Untuk ESP32
int ENA = 21; //D21
int IN1 = 22; //D22
int IN2 = 23; //D23
*/
String statee = "OPEN";
// Handle what happens when you receive new messages
void handleNewMessages(int numNewMessages) {
Serial.println("handleNewMessages");
Serial.println(String(numNewMessages));
for (int i = 0; i < numNewMessages; i++) {
// Chat id of the requester
String chat_id = String(bot.messages[i].chat_id);
if (chat_id != CHAT_ID) {
bot.sendMessage(chat_id, "Unauthorized user", "");
continue;
}
// Print the received message
String text = bot.messages[i].text;
Serial.println(text);
String from_name = bot.messages[i].from_name;
if (text == "/start") {
String welcome = "Welcome, " + from_name + ".\n";
welcome += "Use the following commands to control your Garage Door.\n\n";
welcome += "/open_Gate to OPEN THE GARAGE DOOR \n";
welcome += "/close_Gate to CLOSE THE GARAGE DOOR \n";
welcome += "/state to request current GPIO state \n";
bot.sendMessage(chat_id, welcome, "");
}
if (text == "/open_Gate") {
bot.sendMessage(chat_id, "GARAGE DOOR state is OPEN \n /close_Gate to CLOSE THE GARAGE DOOR", "");
// Garage Open Now
analogWrite(ENA, 220);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
delay(220);
//Stop the MotorDC
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
statee = "OPEN";
bot.sendMessage(chat_id, "status: " + statee, "");
}
if (text == "/close_Gate") {
bot.sendMessage(chat_id, "GARAGE DOOR state is CLOSE \n /open_Gate to OPEN THE GARAGE DOOR", "");
// Garage Close Now
analogWrite(ENA, 220);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
delay(220);
//Stop the MotorDC
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
statee = "CLOSE";
bot.sendMessage(chat_id, "status: " + statee, "");
}
if (text == "/state") {
bot.sendMessage(chat_id, "GARAGE DOOR is " + statee, "");
}
}
}
void setup() {
Serial.begin(115200);
#ifdef ESP8266
configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
client.setTrustAnchors(&cert); // Add root certificate for api.telegram.org
#endif
// Connect to Wi-Fi
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
#ifdef ESP32
client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
#endif
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi..");
}
// Print ESP32 Local IP Address
Serial.println(WiFi.localIP());
//DeklarasiPIN
pinMode(ENA, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
}
void loop() {
if (millis() > lastTimeBotRan + botRequestDelay) {
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
while (numNewMessages) {
Serial.println("got response");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
lastTimeBotRan = millis();
}
}