Advertisement
safwan092

Untitled

Jul 12th, 2023
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. #include <WiFi.h>
  2. #include <WiFiClientSecure.h>
  3. #include <UniversalTelegramBot.h>
  4. #include <ArduinoJson.h>
  5.  
  6. // -------- Define Pins -----------
  7. #define Soil_Moisture_PIN 34 // ✓
  8. #define Pump_PIN 2 // ✓
  9.  
  10. int Sensor_Value;
  11. int onFlag = 0;
  12. const unsigned long BOT_MTBS = 1000; // mean time between scan messages
  13. unsigned long bot_lasttime; // last time messages' scan has been done
  14.  
  15. // Wifi network station credentials
  16. #define WIFI_SSID "network"
  17. #define WIFI_PASSWORD "123456789"
  18. #define BOT_TOKEN "6330967028:AAFpK8lrNqvqm9sRZPAgBpJGSNEBto8ctpg"
  19. #define CHAT_ID "-833531923"
  20.  
  21. WiFiClientSecure secured_client;
  22. UniversalTelegramBot bot(BOT_TOKEN, secured_client);
  23.  
  24. //////////////////////////////////////////////////////////
  25.  
  26. void handleNewMessages(int numNewMessages) {
  27. Serial.println("handleNewMessages");
  28. Serial.println(String(numNewMessages));
  29.  
  30. for (int i = 0; i < numNewMessages; i++) {
  31. String chat_id = bot.messages[i].chat_id;
  32. String text = bot.messages[i].text;
  33.  
  34. String from_name = bot.messages[i].from_name;
  35. if (from_name == "")
  36. from_name = "Guest";
  37.  
  38. if (text == "/send_test_action") {
  39. bot.sendChatAction(chat_id, "typing");
  40. delay(4000);
  41. bot.sendMessage(chat_id, "Did you see the action message?");
  42. }
  43.  
  44. if (text == "/Sensor_Data") {
  45. int value1 = check_Soil_Moisture_Sensor();
  46. bot.sendMessage(chat_id, "Sensor reading is : " + String(value1) + "%");
  47. }
  48.  
  49. if (text == "/options") {
  50. String keyboardJson = "[[\"/Sensor_Data\"]]";
  51. bot.sendMessageWithReplyKeyboard(chat_id, "Choose from one of the following options", "", keyboardJson, true);
  52. }
  53.  
  54. if (text == "/start") {
  55. String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
  56. welcome += "This is Chat Action Bot example.\n\n";
  57. welcome += "/Sensor_Data : to return the current sensors data\n";
  58. welcome += "/options : returns a custom reply keyboard\n";
  59. bot.sendMessage(chat_id, welcome);
  60. }
  61. }
  62. }
  63. //////////////////////////////////////////////////////////
  64.  
  65.  
  66.  
  67. void setup() {
  68. onFlag = 0;
  69. Serial.begin(115200);
  70. Serial.println();
  71.  
  72. pinMode(Soil_Moisture_PIN, INPUT);
  73. pinMode(Pump_PIN, OUTPUT);
  74. digitalWrite(Pump_PIN, 0);
  75.  
  76.  
  77. // attempt to connect to Wifi network:
  78. Serial.print("Connecting to Wifi SSID ");
  79. Serial.print(WIFI_SSID);
  80. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  81. secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
  82. while (WiFi.status() != WL_CONNECTED) {
  83. Serial.print(".");
  84. delay(500);
  85. }
  86. Serial.print("\nWiFi connected. IP address: ");
  87. Serial.println(WiFi.localIP());
  88.  
  89. Serial.print("Retrieving time: ");
  90. configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
  91. time_t now = time(nullptr);
  92. while (now < 24 * 3600) {
  93. Serial.print(".");
  94. delay(100);
  95. now = time(nullptr);
  96. }
  97. Serial.println(now);
  98.  
  99. bot.sendMessage(CHAT_ID, "Bot started up", "");
  100. }
  101.  
  102. int check_Soil_Moisture_Sensor() {
  103. int value1 = analogRead(Soil_Moisture_PIN);
  104. value1 = map(value1, 0, 4095, 100, 0);
  105. Serial.print("Sensor value: ");
  106. Serial.print(value1);
  107. Serial.println("%");
  108. return value1;
  109. }
  110.  
  111. void loop() {
  112.  
  113. ///////////////////////////////////////////
  114. if (millis() - bot_lasttime > BOT_MTBS) {
  115. int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  116.  
  117. while (numNewMessages) {
  118. Serial.println("got response");
  119. handleNewMessages(numNewMessages);
  120. numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  121. }
  122.  
  123. bot_lasttime = millis();
  124. }
  125. ///////////////////////////////////////////
  126. delay(50);
  127. Sensor_Value = check_Soil_Moisture_Sensor();
  128. if (Sensor_Value < 50 && onFlag == 0) {
  129. onFlag = 1;
  130. bot.sendMessage(CHAT_ID, "Soil need Water " + String(Sensor_Value) + "% - Pump is turned [ON]");
  131. Pump_ON();
  132. }
  133. else if (Sensor_Value < 50 && onFlag == 1) {
  134. Pump_ON();
  135. }
  136. else if(Sensor_Value > 50 && onFlag == 1){
  137. onFlag = 0;
  138. bot.sendMessage(CHAT_ID, "Soil Full of Water " + String(Sensor_Value) + "% - Pump is turned [OFF]");
  139. Pump_OFF();
  140. }
  141. else {
  142. onFlag = 0;
  143. Pump_OFF();
  144. }
  145. delay(50);
  146. } //end of Loop
  147.  
  148. void Pump_ON() {
  149. digitalWrite(Pump_PIN, 1);
  150. }
  151. void Pump_OFF() {
  152. digitalWrite(Pump_PIN, 0);
  153. }
  154.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement