Advertisement
Guest User

Coba BOT2

a guest
Apr 25th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*******************************************************************
  2.  *  An example of bot that receives commands and turns on and off  *
  3.  *  an LED.                                                        *
  4.  *                                                                 *
  5.  *  written by Giacarlo Bacchio (Gianbacchio on Github)            *
  6.  *  adapted by Brian Lough                                         *
  7.  *******************************************************************/
  8. #include <ESP8266WiFi.h>
  9. #include <WiFiClientSecure.h>
  10. #include <UniversalTelegramBot.h>
  11.  
  12. // Initialize Wifi connection to the router
  13. char ssid[] = "@wifi.id2";     // your network SSID (name)
  14. char password[] = "qwertyuiop"; // your network key
  15.  
  16. // Initialize Telegram BOT
  17. #define BOTtoken "763366954:AAHQRyUTmwmI0N98sOPiBtFG3hAIVbEjMdM"  // your Bot Token (Get from Botfather)
  18.  
  19.  
  20. WiFiClientSecure client;
  21. UniversalTelegramBot bot(BOTtoken, client);
  22.  
  23. int Bot_mtbs = 1000; //mean time between scan messages
  24. long Bot_lasttime;   //last time messages' scan has been done
  25. bool Start = false;
  26.  
  27. const int ledPin = D6;
  28. int ledStatus = 0;
  29. int led = D7;
  30.  
  31. void handleNewMessages(int numNewMessages) {
  32.   Serial.println("handleNewMessages");
  33.   Serial.println(String(numNewMessages));
  34.  
  35.   for (int i=0; i<numNewMessages; i++) {
  36.     String chat_id = String(bot.messages[i].chat_id);
  37.     String text = bot.messages[i].text;
  38.  
  39.     String from_name = bot.messages[i].from_name;
  40.     if (from_name == "") from_name = "Guest";
  41.  
  42.     if (text == "/ledon") {
  43.       digitalWrite(ledPin, HIGH);   // turn the LED on (HIGH is the voltage level)
  44.       ledStatus = 1;
  45.       bot.sendMessage(chat_id, "Led is ON", "");
  46.     }
  47.  
  48.     if (text == "/ledoff") {
  49.       ledStatus = 0;
  50.       digitalWrite(ledPin, LOW);    // turn the LED off (LOW is the voltage level)
  51.       bot.sendMessage(chat_id, "Led is OFF", "");
  52.     }
  53.  
  54.     if (text == "/status") {
  55.       if(ledStatus){
  56.         bot.sendMessage(chat_id, "Led is ON", "");
  57.       } else {
  58.         bot.sendMessage(chat_id, "Led is OFF", "");
  59.       }
  60.     }
  61.  
  62.     if (text == "/start") {
  63.       String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
  64.       welcome += "This is Flash Led Bot example.\n\n";
  65.       welcome += "/ledon : to switch the Led ON\n";
  66.       welcome += "/ledoff : to switch the Led OFF\n";
  67.       welcome += "/status : Returns current status of LED\n";
  68.       bot.sendMessage(chat_id, welcome, "Markdown");
  69.     }
  70.   }
  71. }
  72.  
  73.  
  74. void setup() {
  75.   Serial.begin(115200);
  76.  
  77.   // Set WiFi to station mode and disconnect from an AP if it was Previously
  78.   // connected
  79.   WiFi.mode(WIFI_STA);
  80.   WiFi.disconnect();
  81.   delay(100);
  82.  
  83.   // attempt to connect to Wifi network:
  84.   Serial.print("Connecting Wifi: ");
  85.   Serial.println(ssid);
  86.   WiFi.begin(ssid, password);
  87.  
  88.   while (WiFi.status() != WL_CONNECTED) {
  89.     Serial.print(".");
  90.     delay(500);
  91.   }
  92.  
  93.   Serial.println("");
  94.   Serial.println("WiFi connected");
  95.   Serial.print("IP address: ");
  96.   Serial.println(WiFi.localIP());
  97.  
  98.   pinMode(ledPin, OUTPUT); // initialize digital ledPin as an output.
  99.   pinMode(led, OUTPUT);
  100.   delay(10);
  101.   digitalWrite(ledPin, LOW); // initialize pin as off
  102.  
  103. }
  104.  
  105. void loop() {
  106.   #define chat_id "@SampeuBot"
  107.   delay(5000);
  108.   digitalWrite(led, HIGH);
  109.   delay(5000);
  110.   digitalWrite(led, LOW);
  111.   if (led == HIGH){
  112.     bot.sendMessage(chat_id, "Led 2 is ON", "");
  113.   }
  114.   if (led == LOW){
  115.     bot.sendMessage(chat_id, "Led 2 is OFF", "");
  116.   }
  117.   if (millis() > Bot_lasttime + Bot_mtbs)  {
  118.     int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  119.  
  120.     while(numNewMessages) {
  121.       Serial.println("got response");
  122.       handleNewMessages(numNewMessages);
  123.       numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  124.     }
  125.  
  126.     Bot_lasttime = millis();
  127.   }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement