safwan092

Untitled

Nov 25th, 2023
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.25 KB | None | 0 0
  1. #ifdef ESP32
  2. #include <WiFi.h>
  3. #else
  4. #include <ESP8266WiFi.h>
  5. #endif
  6. #include <WiFiClientSecure.h>
  7. #include <UniversalTelegramBot.h>
  8. #include <ArduinoJson.h>
  9. #include <NTPClient.h>
  10. #include <WiFiUdp.h>
  11. #define motor1 23
  12. #define motor2_pin1 14
  13. #define motor2_pin2 21
  14. String formattedDate;
  15. String dayStamp;
  16. String timeStamp;
  17. String h ;
  18. String m ;
  19. String h1;
  20. String m1;
  21. #define BOTtoken "6843843585:AAFEFtbzcDKLJeTUmW0QknVSXAHtB-1guSg"
  22. #define CHAT_ID "-1001997751986"
  23. const char* ssid = "Khalidiphone";
  24. const char* password = "00000000";
  25. WiFiUDP ntpUDP;
  26. NTPClient timeClient(ntpUDP);
  27. #ifdef ESP8266
  28. X509List cert(TELEGRAM_CERTIFICATE_ROOT);
  29. #endif
  30. WiFiClientSecure client;
  31. UniversalTelegramBot bot(BOTtoken, client);
  32. int botRequestDelay = 1000;
  33. unsigned long lastTimeBotRan;
  34. void handleNewMessages(int numNewMessages) {
  35. Serial.println("handleNewMessages");
  36. Serial.println(String(numNewMessages));
  37.  
  38. for (int i=0; i<numNewMessages; i++) {
  39. // Chat id of the requester
  40. String chat_id = String(bot.messages[i].chat_id);
  41. if (chat_id != CHAT_ID){
  42. bot.sendMessage(chat_id, "Unauthorized user", "");
  43. continue;
  44. }
  45. // Print the received message
  46. String text = bot.messages[i++].text;
  47. h1=text.substring(0,2);
  48. m1=text.substring(3,5);
  49. bot.sendMessage(CHAT_ID, " time set " + String(h1 && m1));
  50. Serial.println(text);
  51. String from_name = bot.messages[i++].from_name;
  52. }
  53. }
  54. void setup() {
  55. // put your setup code here, to run once:
  56. pinMode(motor1,OUTPUT);
  57. digitalWrite(motor1,LOW);
  58. pinMode(motor2_pin2,OUTPUT);
  59. digitalWrite(motor2_pin2,1);
  60. pinMode(motor2_pin1,OUTPUT);
  61. digitalWrite(motor2_pin1,1);
  62. #ifdef ESP8266
  63. configTime(0, 0, "pool.ntp.org");
  64. client.setTrustAnchors(&cert);
  65. #endif
  66. // Initialize Serial Monitor
  67. Serial.begin(115200);
  68. Serial.print("Connecting to ");
  69. Serial.println(ssid);
  70. WiFi.begin(ssid, password);
  71. while (WiFi.status() != WL_CONNECTED) {
  72. delay(500);
  73. Serial.print(".");
  74. }
  75. // Print local IP address and start web server
  76. Serial.println("");
  77. Serial.println("WiFi connected.");
  78. Serial.println("IP address: ");
  79. Serial.println(WiFi.localIP());
  80.  
  81. // Initialize a NTPClient to get time
  82. timeClient.begin();
  83. // Set offset time in seconds to adjust for your timezone, for example:
  84. // GMT +1 = 3600
  85. // GMT +8 = 28800
  86. // GMT -1 = -3600
  87. // GMT 0 = 0
  88. timeClient.setTimeOffset(10800);
  89. WiFi.mode(WIFI_STA);
  90. WiFi.begin(ssid, password);
  91. #ifdef ESP32
  92. client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
  93. #endif
  94. while (WiFi.status() != WL_CONNECTED) {
  95. delay(1000);
  96. Serial.println("Connecting to WiFi..");
  97. }
  98. // Print ESP32 Local IP Address
  99. Serial.println(WiFi.localIP());
  100. bot.sendMessage(CHAT_ID, "Bot started up", "");
  101. }
  102.  
  103. void loop() {
  104. // put your main code here, to run repeatedly:
  105. if (millis() > lastTimeBotRan + botRequestDelay) {
  106. int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  107.  
  108. while(numNewMessages) {
  109. Serial.println("got response");
  110. handleNewMessages(numNewMessages);
  111. numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  112. }
  113. lastTimeBotRan = millis();}
  114. while(!timeClient.update()) {
  115. timeClient.forceUpdate();
  116. }
  117. // The formattedDate comes with the following format:
  118. // 2018-05-28T16:00:13Z
  119. // We need to extract date and time
  120. formattedDate = timeClient.getFormattedDate();
  121. //Serial.println(formattedDate);
  122.  
  123. // Extract date
  124. int splitT = formattedDate.indexOf("T");
  125. dayStamp = formattedDate.substring(0, splitT);
  126. //Serial.print("DATE: ");
  127. //Serial.println(dayStamp);
  128. // Extract time
  129. timeStamp = formattedDate.substring(splitT+1, formattedDate.length()-1);
  130. //Serial.print("HOUR: ");
  131. //Serial.println(timeStamp);
  132. h=timeStamp.substring(0,2);
  133. m=timeStamp.substring(3,5);
  134. Serial.println(h);
  135. Serial.println(m);
  136. delay(1000);
  137. if(h1== h && m1== m)
  138. {
  139. digitalWrite(motor1,HIGH);
  140. delay(500);
  141. digitalWrite(motor2_pin2,HIGH);
  142. digitalWrite(motor2_pin1,LOW);
  143. }
  144. else {
  145. digitalWrite(motor1,LOW);
  146. digitalWrite(motor2_pin1,1);
  147. digitalWrite(motor2_pin2,1);}
  148. }
Add Comment
Please, Sign In to add comment