Advertisement
agungpf16

telegram_bot

May 28th, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. #include <UniversalTelegramBot.h>
  2. #include <ESP8266WiFi.h>
  3. #include <WiFiClientSecure.h>
  4.  
  5. //------- WiFi Settings -------
  6. char ssid[] = "wifiarea"; // WiFi
  7. char password[] = "xxxxxxx"; // password WiFi
  8. // ------- Telegram config --------
  9. #define BOT_TOKEN "bot_token" // Bot Token Anda
  10. #define chatid "caht_id" // Chat ID Anda
  11. #define tombol D2
  12. WiFiClientSecure client;
  13. UniversalTelegramBot bot(BOT_TOKEN, client);
  14. String ipAddress = "";
  15. volatile bool tombolFlag = false;
  16. int button;
  17. //===================================
  18. void setup() {
  19. Serial.begin(115200);
  20. pinMode(tombol, INPUT);
  21. WiFi.mode(WIFI_STA);
  22. WiFi.disconnect();
  23. delay(100);
  24.  
  25. Serial.print("Connecting Wifi: ");
  26. Serial.println(ssid);
  27. WiFi.begin(ssid, password);
  28. while (WiFi.status() != WL_CONNECTED) {
  29. Serial.print(".");
  30. delay(500);
  31. }
  32. Serial.println("");
  33. Serial.println("WiFi connected");
  34. Serial.println("IP address: ");
  35. IPAddress ip = WiFi.localIP();
  36. Serial.println(ip);
  37. ipAddress = ip.toString();
  38. }
  39.  
  40. //=====================================================
  41. void sendTelegramMessage() {
  42. Serial.println("Kirim pesan ke Telegram");
  43. String message = "Tombol ditekan ....";
  44. message.concat("UJI COBA");
  45. if(bot.sendMessage(chatid, message, "Markdown")){
  46. Serial.println("Pesan telah dikirim ke TELEGRAM");
  47. }
  48. else Serial.println("gagal kirim..");
  49. tombolFlag = false;
  50.  
  51. }
  52. //=====================================================
  53. void loop() {
  54. button = digitalRead(tombol);
  55. if(button==HIGH){
  56. Serial.println("Tombol ditekan");
  57. while(button = digitalRead(tombol)); //nunggu low
  58. sendTelegramMessage();
  59. delay(500);
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement