Advertisement
safwan092

Untitled

Oct 16th, 2023
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.27 KB | None | 0 0
  1. #include <Wire.h>
  2. #include "DHT.h"
  3. #include <WiFi.h>
  4. #include <WiFiClientSecure.h>
  5. #include <UniversalTelegramBot.h>
  6.  
  7. #define WIFI_SSID "Azh"
  8. #define WIFI_PASSWORD "00000000"
  9.  
  10. #define BOT_TOKEN "6141501993:AAFFT0i3dwnbgj6Pcc6aaBDgyQgpQzWI8Nw"
  11. String chat_id = "-4087833454";
  12.  
  13. WiFiClientSecure secured_client;
  14. UniversalTelegramBot bot(BOT_TOKEN, secured_client);
  15.  
  16.  
  17. const unsigned long BOT_MTBS = 500; // mean time between scan messages
  18. unsigned long bot_lasttime; // last time messages' scan has been done
  19. bool Start = false;
  20.  
  21. #define DHTPIN 18
  22. #define DHTTYPE DHT11
  23. DHT dht(DHTPIN, DHTTYPE);
  24. int SoilDigitalPin = 34;
  25. const int relyLight = 32;
  26. const int relyPump = 21;
  27. int sensorDigitalValue = 0;
  28. float t = 0;
  29. float h = 0;
  30. int flag = 0;
  31. void handleNewMessages(int numNewMessages)
  32. {
  33. Serial.println("handleNewMessages");
  34. Serial.println(String(numNewMessages));
  35. for (int i = 0; i < numNewMessages; i++)
  36. {
  37. String chat_id = bot.messages[i].chat_id;
  38. String text = bot.messages[i].text;
  39. String from_name = bot.messages[i].from_name;
  40. if (from_name == "")
  41. from_name = "Guest";
  42. if (text == "/send_test_action")
  43. {
  44. bot.sendChatAction(chat_id, "typing");
  45. delay(4000);
  46. bot.sendMessage(chat_id, "Did you see the action message?");
  47. // You can't use own message, just choose from one of bellow
  48. //typing for text messages
  49. //upload_photo for photos
  50. //record_video or upload_video for videos
  51. //record_audio or upload_audio for audio files
  52. //upload_document for general files
  53. //find_location for location data
  54. //more info here - https://core.telegram.org/bots/api#sendchataction
  55. }
  56. ///////////////////////////////////////////////////
  57. if (text == "/light_on")
  58. {
  59. bot.sendMessage(chat_id, "light is on");
  60. //turn light on
  61. digitalWrite( relyLight, LOW);
  62. }
  63. if (text == "/light_off")
  64. {
  65. bot.sendMessage(chat_id, "light is off");
  66. //turn light off
  67. digitalWrite( relyLight, HIGH);
  68. }
  69. //////////////////////////////////////////////////////////
  70.  
  71.  
  72. //////////////////////////////////////////////////////////
  73. if (text == "/data")
  74. {
  75. String temp_str = "Temp: " + String(t) + "°C \n";
  76. String hum_str = "Hum: " + String(h) + "% \n";
  77. String Soil_str = "Soil Hum:" + String(sensorDigitalValue) + "% \n";
  78. bot.sendMessage(chat_id, temp_str );
  79. bot.sendMessage(chat_id, hum_str);
  80. bot.sendMessage(chat_id, Soil_str);
  81. }
  82. //////////////////////////////////////////////////////////
  83. if (text == "/options")
  84. {
  85. String keyboardJson = "[[\"/light_on\", \"/light_off\"],[\"/data\"]]";
  86. bot.sendMessageWithReplyKeyboard(chat_id, "Choose from one of the following options", "", keyboardJson, true);
  87. }
  88. if (text == "/start")
  89. {
  90. String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
  91. welcome += "This is Chat Action Bot example.\n\n";
  92. welcome += "/send_test_action : to send test chat action message\n";
  93. bot.sendMessage(chat_id, welcome);
  94. }
  95.  
  96. }
  97. }
  98.  
  99.  
  100. void setup() {
  101. Serial.begin(9600);
  102. Serial.println(F("DHTxx test!"));
  103. dht.begin();
  104. pinMode(SoilDigitalPin, INPUT);
  105. pinMode(relyLight, OUTPUT);
  106. pinMode(relyPump, OUTPUT);
  107. connectToWiFi();
  108. bot.sendMessage(chat_id, "Project Online");
  109. digitalWrite( relyPump, HIGH );
  110. digitalWrite( relyLight, HIGH );
  111. }
  112.  
  113. void loop() {
  114. if (sensorDigitalValue > 50 && flag == 1) {
  115. flag = 0;
  116. digitalWrite( relyPump, HIGH ); //pump is off
  117. bot.sendMessage(chat_id, "High Humidity pump is off ");
  118.  
  119. }
  120. if (sensorDigitalValue < 50 && flag == 0) {
  121. flag = 1;
  122. digitalWrite( relyPump, LOW ); //pump is on
  123. bot.sendMessage(chat_id, "LOW Humidity pump is ON ");
  124. }
  125. sensorDigitalValue = analogRead(SoilDigitalPin);
  126. h = dht.readHumidity();
  127. t = dht.readTemperature();
  128. sensorDigitalValue = map(sensorDigitalValue, 0, 4095, 100, 0);
  129. Serial.print("Soil sensor digital value: ");
  130. Serial.println(sensorDigitalValue);
  131. Serial.print(F("% Temperature: "));
  132. Serial.print(t);
  133. Serial.println(F("°C "));
  134. Serial.print(F("% Humidity: "));
  135. Serial.println(h);
  136.  
  137. if (millis() - bot_lasttime > BOT_MTBS)
  138. {
  139. int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  140.  
  141. while (numNewMessages)
  142. {
  143. Serial.println("got response");
  144. handleNewMessages(numNewMessages);
  145. numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  146. }
  147. bot_lasttime = millis();
  148. }
  149.  
  150.  
  151. }//end of loop
  152.  
  153. ////////////////////////////////////////////////
  154. void connectToWiFi() {
  155. Serial.print("Connecting to Wifi SSID ");
  156. Serial.print(WIFI_SSID);
  157. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  158. secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
  159. while (WiFi.status() != WL_CONNECTED)
  160. {
  161. Serial.print(".");
  162. delay(500);
  163. }
  164. Serial.print("\n");
  165. Serial.print("WiFi connected. IP address: ");
  166. Serial.println(WiFi.localIP());
  167.  
  168.  
  169. Serial.print("Retrieving time: ");
  170. configTime(0, 0, "pool.ntp.org");
  171. time_t now = time(nullptr);
  172. while (now < 24 * 3600)
  173. {
  174. Serial.print(".");
  175. delay(100);
  176. now = time(nullptr);
  177. }
  178. Serial.println(now);
  179.  
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement