Advertisement
safwan092

Untitled

Jun 5th, 2023
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.88 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 "network"
  8. #define WIFI_PASSWORD "123456789"
  9.  
  10. int setTemprature = 23;
  11. #define BOT_TOKEN "6140528970:AAHV78G7miJ0Auw-uh7HYy8cVyvzYhh46jQ"G5OpIUQw0pl275pPGFeDfx2frQ6Ca4FH8"
  12. String chat_id = "895680550";
  13.  
  14. WiFiClientSecure secured_client;
  15. UniversalTelegramBot bot(BOT_TOKEN, secured_client);
  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.  
  24. DHT dht(DHTPIN, DHTTYPE);
  25. int FlamePin = 35;
  26. int SoilDigitalPin = 34;
  27. const int relyFan = 4;
  28. const int relyLight = 32;
  29. const int relyPump = 21;
  30. int flag = 0;
  31. int sensorDigitalValue = 0;
  32. float t = 0;
  33. float h = 0;
  34. int sensorReading = 0;
  35. void handleNewMessages(int numNewMessages)
  36. {
  37. Serial.println("handleNewMessages");
  38. Serial.println(String(numNewMessages));
  39. for (int i = 0; i < numNewMessages; i++)
  40. {
  41. String chat_id = bot.messages[i].chat_id;
  42. String text = bot.messages[i].text;
  43.  
  44. String from_name = bot.messages[i].from_name;
  45. if (from_name == "")
  46. from_name = "Guest";
  47.  
  48. if (text == "/send_test_action")
  49. {
  50. bot.sendChatAction(chat_id, "typing");
  51. delay(4000);
  52. bot.sendMessage(chat_id, "Did you see the action message?");
  53.  
  54. // You can't use own message, just choose from one of bellow
  55.  
  56. //typing for text messages
  57. //upload_photo for photos
  58. //record_video or upload_video for videos
  59. //record_audio or upload_audio for audio files
  60. //upload_document for general files
  61. //find_location for location data
  62.  
  63. //more info here - https://core.telegram.org/bots/api#sendchataction
  64. }
  65. ///////////////////////////////////////////////////
  66. if (text == "/fan_on")
  67. {
  68.  
  69. bot.sendMessage(chat_id, "fan is on");
  70. //turn fan on
  71. digitalWrite( relyFan, HIGH);
  72. delay(500);
  73. }
  74. if (text == "/fan_off")
  75. {
  76. bot.sendMessage(chat_id, "fan is off");
  77. //turn fan off
  78. digitalWrite( relyFan, LOW);
  79. }
  80. //////////////////////////////////////////////////////////
  81. if (text == "/light_on")
  82. {
  83. bot.sendMessage(chat_id, "light is on");
  84. //turn light on
  85. digitalWrite( relyLight, HIGH);
  86. }
  87.  
  88. if (text == "/light_off")
  89. {
  90. bot.sendMessage(chat_id, "light is off");
  91. //turn light off
  92. digitalWrite( relyLight, LOW);
  93. }
  94. //////////////////////////////////////////////////////////
  95. if (text == "/pump_on")
  96. {
  97. bot.sendMessage(chat_id, "pump is on");
  98. //turn pump on
  99. if (sensorDigitalValue < 50) {
  100. flag = 1;
  101. digitalWrite( relyPump, HIGH);
  102. }
  103. else {
  104. digitalWrite( relyPump, LOW);
  105. bot.sendMessage(chat_id, "High Humidity pump is off ");
  106. }
  107. }
  108. if (text == "/pump_off")
  109. {
  110. bot.sendMessage(chat_id, "pump is off");
  111. digitalWrite( relyPump, LOW);
  112. }
  113. //////////////////////////////////////////////////////////
  114.  
  115. if (text == "/data")
  116. {
  117. String temp_str = "Temp: " + String(t) + "°C \n";
  118. String hum_str = "Hum: " + String(h) + "% \n";
  119. String Soil_str = "Soil Hum:" + String(sensorDigitalValue) + "% \n";
  120. bot.sendMessage(chat_id, temp_str );
  121. bot.sendMessage(chat_id, hum_str);
  122. bot.sendMessage(chat_id, Soil_str);
  123. }
  124. //////////////////////////////////////////////////////////
  125. if (text == "/options")
  126. {
  127. String keyboardJson = "[[\"/light_on\", \"/light_off\"],[\"/pump_on\", \"/pump_off\"],[\"/fan_on\", \"/fan_off\"],[\"/data\"]]";
  128. bot.sendMessageWithReplyKeyboard(chat_id, "Choose from one of the following options", "", keyboardJson, true);
  129. }
  130. if (text == "/start")
  131. {
  132. String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
  133. welcome += "This is Chat Action Bot example.\n\n";
  134. welcome += "/send_test_action : to send test chat action message\n";
  135. bot.sendMessage(chat_id, welcome);
  136. }
  137.  
  138. }
  139. }
  140.  
  141. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++[ Setup Start]
  142. void setup() {
  143. Serial.begin(9600);
  144. initSensorsAndOutputs();
  145. connectToWiFi();
  146. bot.sendMessage(chat_id, "Project Online");
  147. }
  148. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++[ Setup end]
  149.  
  150. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++[ Loop Start]
  151. void loop() {
  152.  
  153. readSensorsAndSerialDisplay();
  154. receiveOrdersFromTelegramUsers();
  155.  
  156. //****************************************************************
  157. //Flame Sensor Conditions
  158. if (sensorReading < 500 ) {
  159. bot.sendMessage(chat_id, "Fire detected");
  160. digitalWrite( relyPump, HIGH);
  161. delay(500);
  162. }
  163. else if (sensorReading > 500 && flag == 0) {
  164. digitalWrite( relyPump, LOW);
  165. }
  166. //****************************************************************
  167.  
  168. //****************************************************************
  169. //Soil Moisture Sensor Conditions
  170. if (sensorDigitalValue < 50) {
  171. flag = 1;
  172. digitalWrite( relyPump, HIGH);
  173. }
  174. else if (sensorDigitalValue > 50 && flag == 1) {
  175. flag = 0;
  176. digitalWrite( relyPump, LOW);
  177. bot.sendMessage(chat_id, "High Humidity pump is off ");
  178. }
  179. //****************************************************************
  180.  
  181.  
  182. //****************************************************************
  183. //DHT11 Temprature Sensor Conditions
  184. if (t > setTemprature) {
  185. digitalWrite(relyFan, HIGH);
  186. delay(500);
  187. }
  188. else if (t <= setTemprature) {
  189. digitalWrite(relyFan, LOW);
  190. }
  191. //****************************************************************
  192.  
  193. }//end of loop
  194. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++[ Loop end]
  195.  
  196. ////////////////////////////////////////////////
  197. void connectToWiFi() {
  198. Serial.print("Connecting to Wifi SSID ");
  199. Serial.print(WIFI_SSID);
  200. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  201. secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
  202. while (WiFi.status() != WL_CONNECTED)
  203. {
  204. Serial.print(".");
  205. delay(500);
  206. }
  207. Serial.print("\n");
  208. Serial.print("WiFi connected. IP address: ");
  209. Serial.println(WiFi.localIP());
  210.  
  211.  
  212. Serial.print("Retrieving time: ");
  213. configTime(0, 0, "pool.ntp.org");
  214. time_t now = time(nullptr);
  215. while (now < 24 * 3600)
  216. {
  217. Serial.print(".");
  218. delay(100);
  219. now = time(nullptr);
  220. }
  221. Serial.println(now);
  222.  
  223. }
  224.  
  225. void readSensorsAndSerialDisplay() {
  226. sensorDigitalValue = analogRead(SoilDigitalPin);
  227. h = dht.readHumidity();
  228. t = dht.readTemperature();
  229. sensorReading = analogRead(FlamePin);
  230. sensorDigitalValue = map(sensorDigitalValue, 0, 4095, 100, 0);
  231.  
  232. Serial.print("Soil sensor digital value: ");
  233. Serial.println(sensorDigitalValue);
  234. Serial.print(F("% Temperature: "));
  235. Serial.print(t);
  236. Serial.println(F("°C "));
  237. Serial.print(F("% Humidity: "));
  238. Serial.println(h);
  239. Serial.print(F("Flame sensor reading: "));
  240. Serial.println(sensorReading);
  241. }
  242.  
  243.  
  244. void receiveOrdersFromTelegramUsers() {
  245. if (millis() - bot_lasttime > BOT_MTBS)
  246. {
  247. int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  248.  
  249. while (numNewMessages)
  250. {
  251. Serial.println("got response");
  252. handleNewMessages(numNewMessages);
  253. numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  254. }
  255.  
  256. bot_lasttime = millis();
  257. }
  258. }
  259.  
  260.  
  261. void initSensorsAndOutputs() {
  262. dht.begin();
  263. pinMode (FlamePin, INPUT);
  264. pinMode(SoilDigitalPin, INPUT);
  265. pinMode(relyFan, OUTPUT);
  266. pinMode(relyLight, OUTPUT);
  267. pinMode(relyPump, OUTPUT);
  268. digitalWrite(relyPump, LOW);
  269. digitalWrite(relyLight, LOW);
  270. digitalWrite(relyFan, LOW);
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement