Advertisement
yusath

Esp 32 telegram reset

Jul 31st, 2021
1,476
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <WiFi.h>
  2. #include <WiFiClientSecure.h>
  3. #include <UniversalTelegramBot.h>
  4. #include <ArduinoJson.h>
  5.  
  6. // Replace with your network credentials
  7. const char* ssid = "Rakhmat";
  8. const char* password = "wifirakhmat123";
  9.  
  10. // Initialize Telegram BOT
  11. #define BOTtoken "1836141078:AAGpUQW4JH-xgZ5545kLCr4G49o00MkIeBo"  // your Bot Token (Get from Botfather)
  12.  
  13. // Use @myidbot to find out the chat ID of an individual or a group
  14. // Also note that you need to click "start" on a bot before it can
  15. // message you
  16.   #define CHAT_ID "574967010"
  17.  
  18. const unsigned long BOT_MTBS = 1000; // mean time between scan messages
  19.  
  20. WiFiClientSecure client;
  21. UniversalTelegramBot bot(BOTtoken, client);
  22. unsigned long bot_lasttime; // last time messages' scan has been done
  23.  
  24.  
  25. // GANG A
  26.   const int ldrA1 = 25; // ldr sens
  27.   const int ldrA2 = 26; // ldr sens
  28.   const int ldrA3 = 27; // ldr sens
  29. // GANG B
  30.   const int ldrB1 = 18;  // ldr sens
  31.   const int ldrB2 = 19; // ldr sens
  32.   const int ldrB3 = 21; // ldr sens
  33.  
  34. void(*restart) (void) = 0;
  35.  
  36.  
  37. void setup() {
  38.   Serial.begin(115200);
  39.   Serial.println();
  40.  
  41.   // PIR Motion Sensor mode INPUT_PULLUP
  42.   pinMode(ldrA1, INPUT); //sensor ldr 1 input
  43.   pinMode(ldrA2, INPUT); //sensor ldr 2 input
  44.   pinMode(ldrA3, INPUT);
  45.   pinMode(ldrB1, INPUT);
  46.   pinMode(ldrB2, INPUT);
  47.   pinMode(ldrB3, INPUT);
  48.  
  49.  
  50.   // Attempt to connect to Wifi network:
  51. //    configTime(0, 0, "pool.ntp.org");      // get UTC time via NTP
  52.  
  53.   Serial.print("Connecting to Wifi SSID ");
  54.   WiFi.begin(ssid, password);
  55.   client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
  56.   while (WiFi.status() != WL_CONNECTED) {
  57.     Serial.print(".");
  58.     delay(500);
  59.   }
  60.  
  61.   Serial.print("\nWiFi connected. IP address: ");
  62.   Serial.println(WiFi.localIP());
  63.  
  64.   // Check NTP/Time, usually it is instantaneous and you can delete the code below.
  65. //  Serial.print("Retrieving time: ");
  66. //  time_t now = time(nullptr);
  67. //  while (now < 24 * 3600)
  68. //  {
  69. //    Serial.print(".");
  70. //    delay(100);
  71. //    now = time(nullptr);
  72. //  }
  73. //  Serial.println(now);
  74.  
  75.   bot.sendMessage(CHAT_ID, "Bot started up & Sensor Ready", "");
  76. }
  77.  
  78.   boolean cahaya = true;
  79.  
  80. void handleNewMessages(int numNewMessages)
  81. {
  82.   Serial.print("handleNewMessages ");
  83.   Serial.println(numNewMessages);
  84.  
  85.   for (int i = 0; i < numNewMessages; i++)
  86.   {
  87.     String chat_id = bot.messages[i].chat_id;
  88.     String text = bot.messages[i].text;
  89.  
  90.     String from_name = bot.messages[i].from_name;
  91.     if (from_name == "")
  92.       from_name = "Guest";
  93.  
  94.     if (text == "/done")
  95.     {
  96.       bot.sendMessage(CHAT_ID, "Sensor telah direset");
  97.      restart();    
  98.    }
  99.  
  100.     else if (text == "/start")
  101.     {
  102.       String welcome = "Welcome to Automated Message Sensor Bot" + from_name + ".\n";
  103.       welcome += "Ini adalah bot untuk mengontrol sensor. \n\n";
  104.       welcome += "/done : Untuk memberitau sensor jika lampu sudah diperbaiki.\n";
  105.       bot.sendMessage(chat_id, welcome, "Markdown");
  106.     }
  107.   }
  108. }
  109.   void loop() {
  110.  
  111.  
  112.    int sensorData1 = digitalRead (ldrA1);
  113.    int sensorData2 = digitalRead (ldrA2);
  114.    int sensorData3 = digitalRead (ldrA3);
  115.    int sensorData4 = digitalRead (ldrB1);
  116.    int sensorData5 = digitalRead (ldrB2);
  117.    int sensorData6 = digitalRead (ldrB3);
  118.  
  119.     Serial.print("Sensor value:");
  120.     Serial.println(sensorData1);
  121.     Serial.println(sensorData2);
  122.     Serial.println(sensorData3);
  123.     Serial.println(sensorData4);
  124.     Serial.println(sensorData5);
  125.     Serial.println(sensorData6);
  126.  
  127.    if (millis() - bot_lasttime > BOT_MTBS)
  128.  
  129.    if (sensorData1 ==HIGH && cahaya == true) // lampu A1
  130.   {
  131.     bot.sendMessage(CHAT_ID, "LAMPU GANG A NOMOR 1 MATI", "");
  132.     Serial.println("LAMPU GANG A NOMOR 1 MATI");
  133.     cahaya = false; // meniadakan persyaratan agar tidak berulang
  134.   }
  135.   else if (sensorData2 ==HIGH && cahaya == true) // lampu A2
  136.   {
  137.   bot.sendMessage(CHAT_ID, "LAMPU GANG A NOMOR 2 MATI", "");
  138.     Serial.println("LAMPU GANG A NOMOR 2 MATI");
  139.     cahaya = false; // meniadakan persyaratan agar tidak berulang
  140.   }
  141.   else if (sensorData3 ==HIGH && cahaya == true) // lampu A3
  142.   {
  143.   bot.sendMessage(CHAT_ID, "LAMPU GANG A NOMOR 3 MATI", "");
  144.     Serial.println("LAMPU GANG A NOMOR 3 MATI");
  145.     cahaya = false; // meniadakan persyaratan agar tidak berulang
  146.   }
  147.   else if (sensorData4 ==HIGH && cahaya == true) // lampu B1
  148.   {
  149.   bot.sendMessage(CHAT_ID, "LAMPU GANG B NOMOR 1 MATI", "");
  150.     Serial.println("LAMPU GANG B NOMOR 1 MATI");
  151.     cahaya = false; // meniadakan persyaratan agar tidak berulang
  152.   }
  153.    else if (sensorData5 ==HIGH && cahaya == true) // lampu B2
  154.   {
  155.   bot.sendMessage(CHAT_ID, "LAMPU GANG B NOMOR 2 MATI", "");
  156.     Serial.println("LAMPU GANG B NOMOR 2 MATI");
  157.     cahaya = false; // meniadakan persyaratan agar tidak berulang
  158.   }
  159.    else if (sensorData6 ==HIGH && cahaya == true) // lampu B3
  160.   {
  161.   bot.sendMessage(CHAT_ID, "LAMPU GANG B NOMOR 3 MATI", "");
  162.     Serial.println("LAMPU GANG B NOMOR 3 MATI");
  163.     cahaya = false; // meniadakan persyaratan agar tidak berulang
  164.   }
  165.   else if (sensorData1 ==LOW && sensorData2 ==LOW && sensorData3 ==LOW && sensorData4 ==LOW && sensorData5 ==LOW && sensorData6 ==LOW)
  166.   {
  167.     cahaya = true;
  168.   }
  169.   {
  170.     int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  171.  
  172.     while (numNewMessages)
  173.     {
  174.       Serial.println("got response");
  175.       handleNewMessages(numNewMessages);
  176.       numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  177.     }
  178.  
  179.     bot_lasttime = millis();
  180.   }
  181.  
  182.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement