Telegram Bot
Telegram bot adalah sebuah API dari aplikasi chat Telegram yang terhubung ke chat client telegram. Dengan chat bot ini juga kita bisa mengontrol dan memonitoring sebuah system di mana pun, kapan pun selagi device yang di bangun terhubung dengan koneksi internet.
Pada Artikel kali ini kita akan mengontrol Relay 8 channel dan memonitoring suhu dan kelembaban dengan sensor DHT21 dengan modul ESP32 menggunakan Telegram Bot. Dengan fitur keyboard mark up dari library Universal Telegram Bot memudahkan kita untuk mengontrol relay mau pun menerima status suhu dan kelembaban nya.
Alat dan Bahan
Alat yang dibutuhkan hanya dua saja, sebuah modul ESP32 Relay 8ch dan usb to serial custome mikroavr. Produk nya bisa di lihat pada gambar di bawah ini.
Bahan kedua adalah library Arduino. Library yang di perlukan adalah library Universal Telegram Bot dan ArduinoJson. Pada artikel tutorial ini kita menggunakan Universal Telegram bot versi 1.1.0 dan ArduinoJson dengan versi 5.13.5. Pastikan teman-teman menggunakan library dengan versi di atas, agar terhindar dari kesalahan atau error ketika di compile. Karena bisa jadi ArduinoJson terbaru yang teman-teman instal tidak support dengan libray universal telegram bot nya.
Untuk download library nya bisa click di Arduino Ide nya di menu Sketch -> include library -> Manage Library, Selanjut nya lihat gambar di bawah ini.
Library Selanjut nya adalah sebagai berikut, cukup click link untuk download
Setelah keempat library di atas ter instal dengan sempurna di Arduino teman-teman, langkah selanjut nya adalah membuat telegram bot di aplikasi telegram chat nya. Untuk membuat ini cukup mudah, search botFather di telegram bot, atau bisa dilihat pada video di bawah ini.
Pengujian Alat
Sebelum buat program final nya, alangkah baik nya kita uji dengan program relay dan Sensor DHT.
Uji Relay
Berikut program pengujian 8 channel relay nya
const byte R1 = 16; const byte R2 = 17; const byte R3 = 23; const byte R4 = 22; const byte R5 = 21; const byte R6 = 19; const byte R7 = 18; const byte R8 = 5; void setup() { // put your setup code here, to run once: pinMode(R1, OUTPUT); pinMode(R2, OUTPUT); pinMode(R3, OUTPUT); pinMode(R4, OUTPUT); pinMode(R5, OUTPUT); pinMode(R6, OUTPUT); pinMode(R7, OUTPUT); pinMode(R8, OUTPUT); } void loop() { // put your main code here, to run repeatedly: digitalWrite(R1, HIGH);delay(1000); digitalWrite(R2, HIGH);delay(1000); digitalWrite(R3, HIGH);delay(1000); digitalWrite(R4, HIGH);delay(1000); digitalWrite(R5, HIGH);delay(1000); digitalWrite(R6, HIGH);delay(1000); digitalWrite(R7, HIGH);delay(1000); digitalWrite(R8, HIGH);delay(1000); digitalWrite(R1, LOW);delay(1000); digitalWrite(R2, LOW);delay(1000); digitalWrite(R3, LOW);delay(1000); digitalWrite(R4, LOW);delay(1000); digitalWrite(R5, LOW);delay(1000); digitalWrite(R6, LOW);delay(1000); digitalWrite(R7, LOW);delay(1000); digitalWrite(R8, LOW);delay(1000); }
Program di atas akan menghidupkan relay satu per satu selama satu detik, kemudian mematikan nya secara bergilir selama satu detik. Ketika relay hidup semua, berarti relay bekerja dengan baik.
Uji DHT21
Menguji sensor kelembaban dan Suhu. Berikut program nya
#include "DHT.h" #define DHTPIN 14 // Digital pin connected to the DHT sensor #define DHTTYPE DHT21 // DHT 21 (AM2301) DHT dht(DHTPIN, DHTTYPE); void setup() { Serial.begin(115200); Serial.println(F("DHTxx test!")); dht.begin(); } void loop() { delay(2000); float h = dht.readHumidity(); float t = dht.readTemperature(); float f = dht.readTemperature(true); // Check if any reads failed and exit early (to try again). if (isnan(h) || isnan(t) || isnan(f)) { Serial.println(F("Failed to read from DHT sensor!")); return; } // Compute heat index in Fahrenheit (the default) float hif = dht.computeHeatIndex(f, h); // Compute heat index in Celsius (isFahreheit = false) float hic = dht.computeHeatIndex(t, h, false); Serial.print(F("Humidity: ")); Serial.print(h); Serial.print(F("% Temperature: ")); Serial.print(t); Serial.print(F("°C ")); Serial.print(f); Serial.print(F("°F Heat index: ")); Serial.print(hic); Serial.print(F("°C ")); Serial.print(hif); Serial.println(F("°F")); }
Check Serial monitor kemudian lihat nilai dari sensor nya. Jika terbaca maka seluruh device kita telah bekerja dengan baik. layak untuk di lanjutkan untuk program final nya.
Program Final Telegram Bot
Program final ini cukup mudah cara kerja nya. hanya menghidupkan dan mematikan relay dan request status suhu dan kelembaban dengan telegram bot. Berikut program nya
/******************************************************************* create by: Jimmi Kardo Sitepu date : 30-Jan-2020 *******************************************************************/#include "DHT.h" #define DHTPIN 14 #define DHTTYPE DHT21 #include "WiFi.h" #include "WiFiClientSecure.h" #include "UniversalTelegramBot.h" // Initialize Wifi connection to the router char ssid[] = "your-wifi-name"; // your network SSID (name) char password[] = "your-wifi-pass"; // your network key // Initialize Telegram BOT #define BOTtoken "your-telegram-bot-token" // 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 const byte R1 = 16; const byte R2 = 17; const byte R3 = 23; const byte R4 = 22; const byte R5 = 21; const byte R6 = 19; const byte R7 = 18; const byte R8 = 5; DHT dht(DHTPIN, DHTTYPE); float temp, hum; String strDHT21; void handleNewMessages(int numNewMessages) { Serial.println("handleNewMessages"); Serial.println(String(numNewMessages)); 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 == "/r1on") { digitalWrite(R1, HIGH); // turn the LED on (HIGH is the voltage level) bot.sendMessage(chat_id, "RELAY1 is ON", ""); } if (text == "/r1off") { digitalWrite(R1, LOW); // turn the LED off (LOW is the voltage level) bot.sendMessage(chat_id, "RELAY1 is OFF", ""); } if (text == "/r2on") { digitalWrite(R2, HIGH); // turn the LED on (HIGH is the voltage level) bot.sendMessage(chat_id, "RELAY2 is ON", ""); } if (text == "/r2off") { digitalWrite(R2, LOW); // turn the LED off (LOW is the voltage level) bot.sendMessage(chat_id, "RELAY12 is OFF", ""); } if (text == "/r3on") { digitalWrite(R3, HIGH); // turn the LED on (HIGH is the voltage level) bot.sendMessage(chat_id, "RELAY3 is ON", ""); } if (text == "/r3off") { digitalWrite(R3, LOW); // turn the LED off (LOW is the voltage level) bot.sendMessage(chat_id, "RELAY3 is OFF", ""); } if (text == "/r4on") { digitalWrite(R4, HIGH); // turn the LED on (HIGH is the voltage level) bot.sendMessage(chat_id, "RELAY4 is ON", ""); } if (text == "/r4off") { digitalWrite(R4, LOW); // turn the LED off (LOW is the voltage level) bot.sendMessage(chat_id, "RELAY4 is OFF", ""); } if (text == "/r5on") { digitalWrite(R5, HIGH); // turn the LED on (HIGH is the voltage level) bot.sendMessage(chat_id, "RELAY5 is ON", ""); } if (text == "/r5off") { digitalWrite(R5, LOW); // turn the LED off (LOW is the voltage level) bot.sendMessage(chat_id, "RELAY5 is OFF", ""); } if (text == "/r6on") { digitalWrite(R6, HIGH); // turn the LED on (HIGH is the voltage level) bot.sendMessage(chat_id, "RELAY6 is ON", ""); } if (text == "/r6off") { digitalWrite(R6, LOW); // turn the LED off (LOW is the voltage level) bot.sendMessage(chat_id, "RELAY6 is OFF", ""); } if (text == "/r7on") { digitalWrite(R7, HIGH); // turn the LED on (HIGH is the voltage level) bot.sendMessage(chat_id, "RELAY7 is ON", ""); } if (text == "/r7off") { digitalWrite(R7, LOW); // turn the LED off (LOW is the voltage level) bot.sendMessage(chat_id, "RELAY7 is OFF", ""); } if (text == "/r8on") { digitalWrite(R8, HIGH); // turn the LED on (HIGH is the voltage level) bot.sendMessage(chat_id, "RELAY8 is ON", ""); } if (text == "/r8off") { digitalWrite(R8, LOW); // turn the LED off (LOW is the voltage level) bot.sendMessage(chat_id, "RELAY8 is OFF", ""); } if (text == "/onALL") { digitalWrite(R1, HIGH);digitalWrite(R2, HIGH); digitalWrite(R3, HIGH);digitalWrite(R4, HIGH); digitalWrite(R5, HIGH);digitalWrite(R6, HIGH); digitalWrite(R7, HIGH);digitalWrite(R8, HIGH); bot.sendMessage(chat_id, "all relay on", ""); } if (text == "/offALL") { digitalWrite(R1, LOW);digitalWrite(R2, LOW); digitalWrite(R3, LOW);digitalWrite(R4, LOW); digitalWrite(R5, LOW);digitalWrite(R6, LOW); digitalWrite(R7, LOW);digitalWrite(R8, LOW); bot.sendMessage(chat_id, "all relay on", ""); } if (text == "/suhu") { hum = dht.readHumidity(); temp = dht.readTemperature(); strDHT21 = String("MIKROAVR.COM \n") + "suhu sekarang adalah: " + temp + "\n kelembaban: " + hum; bot.sendMessage(chat_id, strDHT21, ""); } if (text == "/options") { String keyboardJson = "[[\"/r1on\", \"/r1off\"],[\"/r2on\", \"/r2off\"],"; keyboardJson += "[\"/r3on\", \"/r3off\"],[\"/r4on\", \"/r4off\"],"; keyboardJson += "[\"/r5on\", \"/r5off\"],[\"/r6on\", \"/r6off\"],"; keyboardJson += "[\"/r7on\", \"/r7off\"],[\"/r8on\", \"/r8off\"],"; keyboardJson += "[\"/onALL\"],[\"/offALL\"],"; keyboardJson += "[\"/suhu\"]]"; bot.sendMessageWithReplyKeyboard(chat_id, "Choose from one of the following options", "", keyboardJson, true); } if (text == "/start") { String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n"; welcome += "This is Reply for esp32 relay 8ch.\n\n"; welcome += "/options : to get button custome\n"; bot.sendMessage(chat_id, welcome, "Markdown"); } } } void setup() { Serial.begin(115200); Serial.print("Connecting Wifi: "); Serial.println(ssid); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(500); } Serial.println(""); Serial.println("WiFi connected"); Serial.print("IP address: "); Serial.println(WiFi.localIP()); pinMode(R1, OUTPUT); pinMode(R2, OUTPUT); pinMode(R3, OUTPUT); pinMode(R4, OUTPUT); pinMode(R5, OUTPUT); pinMode(R6, OUTPUT); pinMode(R7, OUTPUT); pinMode(R8, OUTPUT); dht.begin(); } void loop() { if (WiFi.status() != WL_CONNECTED) { Serial.print("Connecting Wifi: "); Serial.println(ssid); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(500); } Serial.println(""); Serial.println("WiFi connected"); Serial.print("IP address: "); Serial.println(WiFi.localIP()); } while (WiFi.status() == WL_CONNECTED) { if (millis() > Bot_lasttime + Bot_mtbs) { 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); } Bot_lasttime = millis(); } } }
Berikut video pengujian dari controller kita ini. Semoga bermanfaat
old artikel tentan telegram bot: